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