|
|
1.1 root 1: #include "stdio.h"
2: #include "errno.h"
3: #include "signal.h"
4:
5: extern int errno;
6: char *tapes[] = {
7: "/dev/rmt2",
8: "/dev/rmt1",
9: "/dev/rmt0",
10: 0
11: };
12: long recnum, reccnt;
13: int broken();
14: long filenum;
15: int eof;
16: unsigned recsz;
17: char buf[60*1024];
18: int fd;
19:
20: timeout()
21: {
22: gprintf(stdout, "timeout\n");
23: }
24:
25: dens()
26: { int fd, i, n;
27: for(i = 0; tapes[i]; i++) {
28: close(fd);
29: errno = 0;
30: alarm(60);
31: fd = open(tapes[i], 0);
32: alarm(0);
33: if(fd < 0) {
34: perror(tapes[i]);
35: continue;
36: }
37: alarm(60);
38: n = read(fd, buf, sizeof(buf));
39: alarm(0);
40: if( n >= 0) {
41: gprintf(stdout, "%s:\n", tapes[i]);
42: close(fd);
43: return(i);
44: }
45: if(errno == ENXIO) continue;
46: else perror(tapes[i]);
47: }
48: gprintf(stdout, "I can't read anything from your tape\n");
49: return(-1);
50: }
51:
52: main()
53: { int i;
54: int n;
55: signal(SIGALRM, timeout);
56: signal(SIGINT, broken);
57: i = dens();
58: if(i == -1) exit(1);
59: alarm(10);
60: fd = open(tapes[i], 0);
61: alarm(0);
62: if(fd < 0) {
63: perror("Where did your tape go?");
64: exit(1);
65: }
66: newfile();
67: for(;;) {
68: alarm(30);
69: n = read(fd, buf, sizeof(buf));
70: alarm(0);
71: if(n == -1) {
72: if(errno == 0) {
73: if(reccnt > 0) recdata();
74: if(eof == 2) {
75: gprintf(stdout, "2 eofs read\n");
76: exit(0);
77: }
78: filedata();
79: newfile();
80: }
81: else {
82: recdata();
83: filedata();
84: perror("Bailing out");
85: exit(1);
86: }
87: }
88: else if(n == 0) {
89: eof++;
90: if(eof == 2) {
91: gprintf(stdout, "2 eofs read\n");
92: exit(0);
93: }
94: if(reccnt > 0) recdata();
95: filedata();
96: newfile();
97: }
98: else {
99: eof = 0;
100: recnum++;
101: if(n == recsz) reccnt++;
102: else {
103: if(reccnt > 0) recdata();
104: recsz = n;
105: reccnt = 1;
106: }
107: }
108: }
109: }
110:
111: newfile()
112: {
113: filenum++;
114: recnum = 0;
115: reccnt = 0;
116: recsz = 0;
117: errno = 0;
118: }
119:
120: filedata()
121: {
122: gprintf(stdout, "file %ld had %ld records\n", filenum, recnum);
123: }
124:
125: recdata()
126: {
127: if(recsz < sizeof(buf))
128: gprintf(stdout, "file %ld, %ld records of size %u\n", filenum, reccnt, recsz);
129: else gprintf(stdout, "file %ld, %ld records of size %u (or larger)\n",
130: filenum, reccnt, recsz);
131: }
132:
133: broken()
134: {
135: gprintf(stderr, "interrupted\n");
136: recdata();
137: filedata();
138: exit(0);
139: }
140:
141: gprintf(s,f,a,b,c,d)
142: FILE *s;
143: char *f;
144: {
145: fprintf(s,f,a,b,c,d);
146: fflush(stdout);
147: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.