|
|
1.1 root 1: #include <stdio.h>
2: #include <stddef.h>
3:
4: #define NSUMS 100
5: int BSIZE = 1024;
6:
7: char *myname;
8: typedef long Sumfn(long, char *, int);
9: extern Sumfn sumr, sum5, sum32;
10: static int sumfile(char *, Sumfn *);
11:
12: static void
13: usage()
14: {
15: fprintf(stderr, "Usage: %s [-ir5] [files]\n", myname);
16: exit(1);
17: }
18:
19: main(int argc, char **argv)
20: {
21: int iflag = 0;
22: int exitcode = 0;
23: int c;
24: char buf[4096];
25: Sumfn *fn = sum32;
26: extern int optind;
27:
28: myname = argv[0];
29: while((c = getopt(argc, argv, "ir5")) != -1)
30: switch(c)
31: {
32: case 'i': iflag = 1; break;
33: case 'r': fn = sumr; break;
34: case '5': fn = sum5; break;
35: case '?': usage(); break;
36: }
37:
38: if(iflag){
39: while(fgets(buf, sizeof buf, stdin) != NULL){
40: if(c = strlen(buf)){
41: buf[c-1] = 0;
42: exitcode |= sumfile(buf, fn);
43: }
44: }
45: } else if(optind < argc){
46: while(optind < argc)
47: exitcode |= sumfile(argv[optind++], fn);
48: } else
49: exitcode |= sumfile((char *)0, fn);
50: exit(exitcode);
51: }
52:
53: static int
54: sumfile(char *path, Sumfn *fn)
55: {
56: int fd;
57: int n;
58: long sum;
59: int fsize;
60: char buf[64*1024];
61:
62: if(path){
63: if((fd = open(path, 0)) < 0){
64: fprintf(stderr, "%s: ", myname);
65: perror(path);
66: return(1);
67: }
68: } else
69: fd = 0;
70: fsize = 0;
71: sum = 0;
72: while((n = read(fd, buf, sizeof buf)) > 0){
73: fsize += n;
74: sum = (*fn)(sum, buf, n);
75: }
76: if(n < 0){
77: fprintf(stderr, "%s: %s: ", myname, path? path:"<standard input>");
78: perror("read");
79: if(path)
80: close(fd);
81: return(1);
82: }
83: if(path)
84: close(fd);
85: (*fn)(sum, (char *)0, fsize);
86: if(path)
87: printf(" %s", path);
88: putchar('\n');
89: fflush(stdout);
90: return(0);
91: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.