packages: don't put oss on cd.
[minix.git] / commands / sum / sum.c
blob7b0d6699d52ffb7e4faa4a6b3820a12496f7acdb
1 /* sum - checksum a file Author: Martin C. Atkins */
3 /*
4 * This program was written by:
5 * Martin C. Atkins,
6 * University of York,
7 * Heslington,
8 * York. Y01 5DD
9 * England
10 * and is released into the public domain, on the condition
11 * that this comment is always included without alteration.
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <minix/minlib.h>
19 #include <stdio.h>
21 #define BUFFER_SIZE (512)
23 int rc = 0;
25 char *defargv[] = {"-", 0};
27 _PROTOTYPE(int main, (int argc, char **argv));
28 _PROTOTYPE(void error, (char *s, char *f));
29 _PROTOTYPE(void sum, (int fd, char *fname));
30 _PROTOTYPE(void putd, (int number, int fw, int zeros));
32 int main(argc, argv)
33 int argc;
34 char *argv[];
36 register int fd;
38 if (*++argv == 0) argv = defargv;
39 for (; *argv; argv++) {
40 if (argv[0][0] == '-' && argv[0][1] == '\0')
41 fd = 0;
42 else
43 fd = open(*argv, O_RDONLY);
45 if (fd == -1) {
46 error("can't open ", *argv);
47 rc = 1;
48 continue;
50 sum(fd, (argc > 2) ? *argv : (char *) 0);
51 if (fd != 0) close(fd);
53 return(rc);
56 void error(s, f)
57 char *s, *f;
60 std_err("sum: ");
61 std_err(s);
63 if (f) std_err(f);
64 std_err("\n");
67 void sum(fd, fname)
68 int fd;
69 char *fname;
71 char buf[BUFFER_SIZE];
72 register int i, n;
73 long size = 0;
74 unsigned crc = 0;
75 unsigned tmp, blks;
77 while ((n = read(fd, buf, BUFFER_SIZE)) > 0) {
78 for (i = 0; i < n; i++) {
79 crc = (crc >> 1) + ((crc & 1) ? 0x8000 : 0);
80 tmp = buf[i] & 0377;
81 crc += tmp;
82 crc &= 0xffff;
83 size++;
87 if (n < 0) {
88 if (fname)
89 error("read error on ", fname);
90 else
91 error("read error", (char *) 0);
92 rc = 1;
93 return;
95 putd(crc, 5, 1);
96 blks = (size + (long) BUFFER_SIZE - 1L) / (long) BUFFER_SIZE;
97 putd(blks, 6, 0);
98 if (fname) printf(" %s", fname);
99 printf("\n");
102 void putd(number, fw, zeros)
103 int number, fw, zeros;
105 /* Put a decimal number, in a field width, to stdout. */
107 char buf[10];
108 int n;
109 unsigned num;
111 num = (unsigned) number;
112 for (n = 0; n < fw; n++) {
113 if (num || n == 0) {
114 buf[fw - n - 1] = '0' + num % 10;
115 num /= 10;
116 } else
117 buf[fw - n - 1] = zeros ? '0' : ' ';
119 buf[fw] = 0;
120 printf("%s", buf);