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