# 2.11BSD#431 - /usr/src/usr.bin/sum.c

##### Functions

    main              X

       1: static char *sccsid = "@(#)sum.c	4.1 (Berkeley) 10/1/80";
       2: /*
       3:  * Sum bytes in file mod 2^16
       4:  */
       5:
       6: #include <stdio.h>
       7:
       8: main(argc,argv)
       9: char **argv;
      10: {
      11:     register unsigned sum;
      12:     register i, c;
      13:     register FILE *f;
      14:     register long nbytes;
      15:     int errflg = 0;
      16:
      17:     i = 1;
      18:     do {
      19:         if(i < argc) {
      20:             if ((f = fopen(argv[i], "r")) == NULL) {
      21:                 fprintf(stderr, "sum: Can't open %s\n", argv[i]);
      22:                 errflg += 10;
      23:                 continue;
      24:             }
      25:         } else
      26:             f = stdin;
      27:         sum = 0;
      28:         nbytes = 0;
      29:         while ((c = getc(f)) != EOF) {
      30:             nbytes++;
      31:             if (sum&01)
      32:                 sum = (sum>>1) + 0x8000;
      33:             else
      34:                 sum >>= 1;
      35:             sum += c;
      36:             sum &= 0xFFFF;
      37:         }
      38:         if (ferror(f)) {
      39:             errflg++;
      40:             fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-");
      41:         }
      42:         printf("%05u%6ld", sum, (nbytes+BUFSIZ-1)/BUFSIZ);
      43:         if(argc > 2)
      44:             printf(" %s", argv[i]);
      45:         printf("\n");
      46:         fclose(f);
      47:     } while(++i < argc);
      48:     exit(errflg);
      49: }

#### Defined functions

main defined in line 8; never used

#### Defined variables

sccsid defined in line 1; never used

Last modified: 1987-02-17 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 980 |