xref: /titanic_44/usr/src/ucbcmd/sum/sum.c (revision f22acdfff536d452df49dd85c5ecd42092b8fcad)
1956e8222Scf46844 /*
2956e8222Scf46844  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3956e8222Scf46844  * Use is subject to license terms.
4956e8222Scf46844  */
5956e8222Scf46844 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate /*
117c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
127c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
137c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
16956e8222Scf46844 #pragma ident	"%Z%%M%	%I%	%E% SMI"
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate  * Sum bytes in file mod 2^16
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <stdio.h>
237c478bd9Sstevel@tonic-gate 
24956e8222Scf46844 int
main(int argc,char ** argv)25956e8222Scf46844 main(int argc, char **argv)
267c478bd9Sstevel@tonic-gate {
27956e8222Scf46844 	unsigned int sum;
28956e8222Scf46844 	int i, c;
29956e8222Scf46844 	FILE *f;
30956e8222Scf46844 	long long nbytes;
317c478bd9Sstevel@tonic-gate 	int errflg = 0;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 	i = 1;
347c478bd9Sstevel@tonic-gate 	do {
357c478bd9Sstevel@tonic-gate 		if (i < argc) {
367c478bd9Sstevel@tonic-gate 			if ((f = fopen(argv[i], "r")) == NULL) {
37*f22acdffSgbrunett 				(void) fprintf(stderr,
38*f22acdffSgbrunett 					"sum: Can't open %s\n", argv[i]);
397c478bd9Sstevel@tonic-gate 				errflg += 10;
407c478bd9Sstevel@tonic-gate 				continue;
417c478bd9Sstevel@tonic-gate 			}
427c478bd9Sstevel@tonic-gate 		} else
437c478bd9Sstevel@tonic-gate 			f = stdin;
447c478bd9Sstevel@tonic-gate 		sum = 0;
457c478bd9Sstevel@tonic-gate 		nbytes = 0;
467c478bd9Sstevel@tonic-gate 		while ((c = getc(f)) != EOF) {
477c478bd9Sstevel@tonic-gate 			nbytes++;
487c478bd9Sstevel@tonic-gate 			if (sum&01)
497c478bd9Sstevel@tonic-gate 				sum = (sum>>1) + 0x8000;
507c478bd9Sstevel@tonic-gate 			else
517c478bd9Sstevel@tonic-gate 				sum >>= 1;
527c478bd9Sstevel@tonic-gate 			sum += c;
537c478bd9Sstevel@tonic-gate 			sum &= 0xFFFF;
547c478bd9Sstevel@tonic-gate 		}
557c478bd9Sstevel@tonic-gate 		if (ferror(f)) {
567c478bd9Sstevel@tonic-gate 			errflg++;
57*f22acdffSgbrunett 			(void) fprintf(stderr,
58*f22acdffSgbrunett 				"sum: read error on %s\n",
59*f22acdffSgbrunett 				argc > 1 ? argv[i] : "-");
607c478bd9Sstevel@tonic-gate 		}
61*f22acdffSgbrunett 
62*f22acdffSgbrunett 		(void) printf("%05u %5lld", sum,
63*f22acdffSgbrunett 			(nbytes + BUFSIZ - 1) / BUFSIZ);
647c478bd9Sstevel@tonic-gate 		if (argc > 2)
65*f22acdffSgbrunett 			(void) printf(" %s", argv[i]);
66*f22acdffSgbrunett 		(void) printf("\n");
67*f22acdffSgbrunett 		(void) fclose(f);
687c478bd9Sstevel@tonic-gate 	} while (++i < argc);
69*f22acdffSgbrunett 
70956e8222Scf46844 	return (errflg);
717c478bd9Sstevel@tonic-gate }
72