source: trunk/minix/commands/simple/sum.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 2.1 KB
Line 
1/* sum - checksum a file Author: Martin C. Atkins */
2
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.
12 */
13
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>
20
21#define BUFFER_SIZE (512)
22
23int rc = 0;
24
25char *defargv[] = {"-", 0};
26
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));
31
32int main(argc, argv)
33int argc;
34char *argv[];
35{
36 register int fd;
37
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);
44
45 if (fd == -1) {
46 error("can't open ", *argv);
47 rc = 1;
48 continue;
49 }
50 sum(fd, (argc > 2) ? *argv : (char *) 0);
51 if (fd != 0) close(fd);
52 }
53 return(rc);
54}
55
56void error(s, f)
57char *s, *f;
58{
59
60 std_err("sum: ");
61 std_err(s);
62
63 if (f) std_err(f);
64 std_err("\n");
65}
66
67void sum(fd, fname)
68int fd;
69char *fname;
70{
71 char buf[BUFFER_SIZE];
72 register int i, n;
73 long size = 0;
74 unsigned crc = 0;
75 unsigned tmp, blks;
76
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++;
84 }
85 }
86
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;
94 }
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");
100}
101
102void putd(number, fw, zeros)
103int number, fw, zeros;
104{
105/* Put a decimal number, in a field width, to stdout. */
106
107 char buf[10];
108 int n;
109 unsigned num;
110
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' : ' ';
118 }
119 buf[fw] = 0;
120 printf("%s", buf);
121}
Note: See TracBrowser for help on using the repository browser.