|
|
1.1 root 1: /*
2: * Sum bytes in file mod 2^16
3: */
4:
5: char xxxnotused[] = "@(#)sum.c 1.5";
6:
7: #define WDMSK 0177777L
8: #define BUFSIZE 512
9: #include <stdio.h>
10: struct part {
11: short unsigned hi,lo;
12: };
13: union hilo { /* this only works right in case short is 1/2 of long */
14: struct part hl;
15: long lg;
16: } tempa, suma;
17:
18: main(argc,argv)
19: char **argv;
20: {
21: register unsigned sum;
22: register i, c;
23: register FILE *f;
24: register long nbytes;
25: int alg, ca, errflg = 0;
26: unsigned lsavhi,lsavlo;
27:
28: alg = 0;
29: i = 1;
30: if(argv[1][0]=='-' && argv[1][1]=='r') {
31: alg = 1;
32: i = 2;
33: }
34:
35: do {
36: if(i < argc) {
37: if((f = fopen(argv[i], "r")) == NULL) {
38: (void) fprintf(stderr, "sum: Can't open %s\n", argv[i]);
39: errflg += 10;
40: continue;
41: }
42: } else
43: f = stdin;
44: sum = 0;
45: suma.lg = 0;
46: nbytes = 0;
47: if(alg == 1) {
48: while((c = getc(f)) != EOF) {
49: nbytes++;
50: if(sum & 01)
51: sum = (sum >> 1) + 0x8000;
52: else
53: sum >>= 1;
54: sum += c;
55: sum &= 0xFFFF;
56: }
57: } else {
58: while((ca = getc(f)) != EOF) {
59: nbytes++;
60: suma.lg += ca & WDMSK;
61: }
62: }
63: if(ferror(f)) {
64: errflg++;
65: (void) fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
66: }
67: if (alg == 1)
68: (void) printf("%.5u%6ld", sum, (nbytes+BUFSIZE-1)/BUFSIZE);
69: else {
70: tempa.lg = (suma.hl.lo & WDMSK) + (suma.hl.hi & WDMSK);
71: lsavhi = (unsigned) tempa.hl.hi;
72: lsavlo = (unsigned) tempa.hl.lo;
73: (void) printf("%u %ld", (unsigned)(lsavhi + lsavlo), (nbytes+BUFSIZE-1)/BUFSIZE);
74: }
75: if(argc > 1)
76: (void) printf(" %s", argv[i]==(char *)0?"":argv[i]);
77: (void) printf("\n");
78: (void) fclose(f);
79: } while(++i < argc);
80: exit(errflg);
81: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.