xref: /titanic_51/usr/src/ucbcmd/sum/sum.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983,1984,1985,1986,1987,1988,1996, by Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17 
18 /*
19  * Sum bytes in file mod 2^16
20  */
21 
22 #include <stdio.h>
23 
24 main(argc,argv)
25 char **argv;
26 {
27 	register unsigned sum;
28 	register i, c;
29 	register FILE *f;
30 	register long long nbytes;
31 	int errflg = 0;
32 
33 	i = 1;
34 	do {
35 		if(i < argc) {
36 			if ((f = fopen(argv[i], "r")) == NULL) {
37 				fprintf(stderr, "sum: Can't open %s\n", argv[i]);
38 				errflg += 10;
39 				continue;
40 			}
41 		} else
42 			f = stdin;
43 		sum = 0;
44 		nbytes = 0;
45 		while ((c = getc(f)) != EOF) {
46 			nbytes++;
47 			if (sum&01)
48 				sum = (sum>>1) + 0x8000;
49 			else
50 				sum >>= 1;
51 			sum += c;
52 			sum &= 0xFFFF;
53 		}
54 		if (ferror(f)) {
55 			errflg++;
56 			fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
57 		}
58 		printf("%05u %5lld", sum, (nbytes+BUFSIZ-1)/BUFSIZ);
59 		if(argc > 2)
60 			printf(" %s", argv[i]);
61 		printf("\n");
62 		fclose(f);
63 	} while(++i < argc);
64 	exit(errflg);
65 }
66