1: /*
2: * Sum bytes in file mod 2^16
3: */
4:
5: #include <stdio.h>
6:
7: main(argc,argv)
8: char **argv;
9: {
10: register unsigned sum;
11: register i, c;
12: register FILE *f;
13: register long nbytes;
14: int errflg = 0;
15:
16: i = 1;
17: do {
18: if(i < argc) {
19: if ((f = fopen(argv[i], "r")) == NULL) {
20: fprintf(stderr, "sum: Can't open %s\n", argv[i]);
21: errflg += 10;
22: continue;
23: }
24: } else
25: f = stdin;
26: sum = 0;
27: nbytes = 0;
28: while ((c = getc(f)) != EOF) {
29: nbytes++;
30: if (sum&01)
31: sum = (sum>>1) + 0x8000;
32: else
33: sum >>= 1;
34: sum += c;
35: sum &= 0xFFFF;
36: }
37: if (ferror(f)) {
38: errflg++;
39: fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
40: }
41: printf("%05u%6ld", sum, (nbytes+BUFSIZ-1)/BUFSIZ);
42: if(argc > 2)
43: printf(" %s", argv[i]);
44: printf("\n");
45: fclose(f);
46: } while(++i < argc);
47: exit(errflg);
48: }
Defined functions
main
defined in line
7;
never used