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