xref: /titanic_51/usr/src/ucbcmd/sum/sum.c (revision 956e8222f10bf55e45b41d8b56084f72ebc113c9)
1*956e8222Scf46844 /*
2*956e8222Scf46844  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*956e8222Scf46844  * Use is subject to license terms.
4*956e8222Scf46844  */
5*956e8222Scf46844 
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 
16*956e8222Scf46844 #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 
24*956e8222Scf46844 int
25*956e8222Scf46844 main(int argc, char **argv)
267c478bd9Sstevel@tonic-gate {
27*956e8222Scf46844 	unsigned int sum;
28*956e8222Scf46844 	int i, c;
29*956e8222Scf46844 	FILE *f;
30*956e8222Scf46844 	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) {
377c478bd9Sstevel@tonic-gate 				fprintf(stderr, "sum: Can't open %s\n", argv[i]);
387c478bd9Sstevel@tonic-gate 				errflg += 10;
397c478bd9Sstevel@tonic-gate 				continue;
407c478bd9Sstevel@tonic-gate 			}
417c478bd9Sstevel@tonic-gate 		} else
427c478bd9Sstevel@tonic-gate 			f = stdin;
437c478bd9Sstevel@tonic-gate 		sum = 0;
447c478bd9Sstevel@tonic-gate 		nbytes = 0;
457c478bd9Sstevel@tonic-gate 		while ((c = getc(f)) != EOF) {
467c478bd9Sstevel@tonic-gate 			nbytes++;
477c478bd9Sstevel@tonic-gate 			if (sum&01)
487c478bd9Sstevel@tonic-gate 				sum = (sum>>1) + 0x8000;
497c478bd9Sstevel@tonic-gate 			else
507c478bd9Sstevel@tonic-gate 				sum >>= 1;
517c478bd9Sstevel@tonic-gate 			sum += c;
527c478bd9Sstevel@tonic-gate 			sum &= 0xFFFF;
537c478bd9Sstevel@tonic-gate 		}
547c478bd9Sstevel@tonic-gate 		if (ferror(f)) {
557c478bd9Sstevel@tonic-gate 			errflg++;
567c478bd9Sstevel@tonic-gate 			fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 		printf("%05u %5lld", sum, (nbytes+BUFSIZ-1)/BUFSIZ);
597c478bd9Sstevel@tonic-gate 		if(argc > 2)
607c478bd9Sstevel@tonic-gate 			printf(" %s", argv[i]);
617c478bd9Sstevel@tonic-gate 		printf("\n");
627c478bd9Sstevel@tonic-gate 		fclose(f);
637c478bd9Sstevel@tonic-gate 	} while(++i < argc);
64*956e8222Scf46844 	return (errflg);
657c478bd9Sstevel@tonic-gate }
66