1 /* sum - checksum a file Author: Martin C. Atkins */
4 * This program was written by:
10 * and is released into the public domain, on the condition
11 * that this comment is always included without alteration.
14 #include <sys/types.h>
18 #include <minix/minlib.h>
21 #define BUFFER_SIZE (512)
25 char *defargv
[] = {"-", 0};
27 _PROTOTYPE(int main
, (int argc
, char **argv
));
28 _PROTOTYPE(void error
, (char *s
, char *f
));
29 _PROTOTYPE(void sum
, (int fd
, char *fname
));
30 _PROTOTYPE(void putd
, (int number
, int fw
, int zeros
));
38 if (*++argv
== 0) argv
= defargv
;
39 for (; *argv
; argv
++) {
40 if (argv
[0][0] == '-' && argv
[0][1] == '\0')
43 fd
= open(*argv
, O_RDONLY
);
46 error("can't open ", *argv
);
50 sum(fd
, (argc
> 2) ? *argv
: (char *) 0);
51 if (fd
!= 0) close(fd
);
71 char buf
[BUFFER_SIZE
];
77 while ((n
= read(fd
, buf
, BUFFER_SIZE
)) > 0) {
78 for (i
= 0; i
< n
; i
++) {
79 crc
= (crc
>> 1) + ((crc
& 1) ? 0x8000 : 0);
89 error("read error on ", fname
);
91 error("read error", (char *) 0);
96 blks
= (size
+ (long) BUFFER_SIZE
- 1L) / (long) BUFFER_SIZE
;
98 if (fname
) printf(" %s", fname
);
102 void putd(number
, fw
, zeros
)
103 int number
, fw
, zeros
;
105 /* Put a decimal number, in a field width, to stdout. */
111 num
= (unsigned) number
;
112 for (n
= 0; n
< fw
; n
++) {
114 buf
[fw
- n
- 1] = '0' + num
% 10;
117 buf
[fw
- n
- 1] = zeros
? '0' : ' ';