1 /* 2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 7 /* All Rights Reserved */ 8 9 10 /* 11 * Copyright (c) 1980 Regents of the University of California. 12 * All rights reserved. The Berkeley software License Agreement 13 * specifies the terms and conditions for redistribution. 14 */ 15 16 #pragma ident "%Z%%M% %I% %E% SMI" 17 18 /* 19 * Sum bytes in file mod 2^16 20 */ 21 22 #include <stdio.h> 23 24 int 25 main(int argc, char **argv) 26 { 27 unsigned int sum; 28 int i, c; 29 FILE *f; 30 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 return (errflg); 65 } 66