xref: /illumos-gate/usr/src/ucbcmd/sum/sum.c (revision 8c0b080c)
1956e8222Scf /*
2956e8222Scf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3956e8222Scf  * Use is subject to license terms.
4956e8222Scf  */
5956e8222Scf 
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 
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate  * Sum bytes in file mod 2^16
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <stdio.h>
217c478bd9Sstevel@tonic-gate 
22956e8222Scf int
main(int argc,char ** argv)23956e8222Scf main(int argc, char **argv)
247c478bd9Sstevel@tonic-gate {
25956e8222Scf 	unsigned int sum;
26956e8222Scf 	int i, c;
27956e8222Scf 	FILE *f;
28956e8222Scf 	long long nbytes;
297c478bd9Sstevel@tonic-gate 	int errflg = 0;
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 	i = 1;
327c478bd9Sstevel@tonic-gate 	do {
33*f22acdffSgbrunett 		if (i < argc) {
347c478bd9Sstevel@tonic-gate 			if ((f = fopen(argv[i], "r")) == NULL) {
35*f22acdffSgbrunett 				(void) fprintf(stderr,
36*f22acdffSgbrunett 					"sum: Can't open %s\n", argv[i]);
377c478bd9Sstevel@tonic-gate 				errflg += 10;
387c478bd9Sstevel@tonic-gate 				continue;
397c478bd9Sstevel@tonic-gate 			}
407c478bd9Sstevel@tonic-gate 		} else
417c478bd9Sstevel@tonic-gate 			f = stdin;
427c478bd9Sstevel@tonic-gate 		sum = 0;
437c478bd9Sstevel@tonic-gate 		nbytes = 0;
447c478bd9Sstevel@tonic-gate 		while ((c = getc(f)) != EOF) {
457c478bd9Sstevel@tonic-gate 			nbytes++;
467c478bd9Sstevel@tonic-gate 			if (sum&01)
477c478bd9Sstevel@tonic-gate 				sum = (sum>>1) + 0x8000;
487c478bd9Sstevel@tonic-gate 			else
497c478bd9Sstevel@tonic-gate 				sum >>= 1;
507c478bd9Sstevel@tonic-gate 			sum += c;
517c478bd9Sstevel@tonic-gate 			sum &= 0xFFFF;
527c478bd9Sstevel@tonic-gate 		}
537c478bd9Sstevel@tonic-gate 		if (ferror(f)) {
547c478bd9Sstevel@tonic-gate 			errflg++;
55*f22acdffSgbrunett 			(void) fprintf(stderr,
56*f22acdffSgbrunett 				"sum: read error on %s\n",
57*f22acdffSgbrunett 				argc > 1 ? argv[i] : "-");
587c478bd9Sstevel@tonic-gate 		}
59*f22acdffSgbrunett 
60*f22acdffSgbrunett 		(void) printf("%05u %5lld", sum,
61*f22acdffSgbrunett 			(nbytes + BUFSIZ - 1) / BUFSIZ);
62*f22acdffSgbrunett 		if (argc > 2)
63*f22acdffSgbrunett 			(void) printf(" %s", argv[i]);
64*f22acdffSgbrunett 		(void) printf("\n");
65*f22acdffSgbrunett 		(void) fclose(f);
66*f22acdffSgbrunett 	} while (++i < argc);
67*f22acdffSgbrunett 
68956e8222Scf 	return (errflg);
697c478bd9Sstevel@tonic-gate }
70