|
|
1.1 root 1: #include <stdio.h>
2: /*
3: * Sum bytes in file mod 2^16
4: */
5:
6: #include <stdio.h>
7: #define BUFSIZE 1024
8: #define MAXSTR 512
9: /* disk block size for backward compatability */
10: #define BLOKSIZE 1024
11:
12: extern char *fgets();
13:
14: char *ego;
15:
16: main (argc, argv)
17: int argc;
18: char *argv[];
19: {
20: char s[MAXSTR];
21: int i, errcode = 0;
22:
23: ego = argv[0];
24:
25: if (argc < 2)
26: sumfile ((char *) 0);
27: else for (i = 1; i < argc; i++)
28: if (*argv[i] != '-')
29: errcode += sumfile (argv[i]);
30: else switch (argv[i][1]) {
31: case 0:
32: while(fgets(s,sizeof s, stdin)) {
33: s[strlen(s)-1] = 0;
34: errcode += sumfile (s);
35: }
36: break;
37: default:
38: goto usage;
39: }
40:
41: exit (errcode);
42:
43: usage:
44: fprintf (stderr, "usage: %s [-] [file]...\n", ego);
45: exit (1);
46: }
47:
48:
49: sumfile (path)
50: char *path;
51: {
52: static unsigned char buf[BUFSIZE];
53: register unsigned char *s;
54: register int n, fd;
55: register long nbytes = 0;
56: register unsigned int sum = 0;
57:
58: if (!path)
59: fd = 0;
60: else if (0 > (fd = open (path, 0))) {
61: fprintf (stderr, "%s: %s: can't open\n", ego, path);
62: return (1);
63: }
64:
65:
66: for (;;)
67: if (0 >= (n = read (fd, buf, BUFSIZE))) {
68: (void) close (fd);
69: if (n < 0) {
70: fprintf (stderr, "%s: %s: read error\n",
71: ego, path? path: "<standard input>");
72: return (1);
73: }
74: /* should produce same format as old version always */
75: printf ("%05u%6ld", sum,
76: (nbytes + (BLOKSIZE - 1)) / BLOKSIZE);
77: if (path)
78: printf(" %s", path);
79: putchar('\n');
80: fflush(stdout);
81: return (0);
82: }
83: else for (nbytes += n, s = buf; n; n--, s++)
84: if (sum & 1)
85: sum = 0xffff & ((sum>>1) + *s + 0x8000);
86: else sum = 0xffff & ((sum>>1) + *s);
87: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.