BSD 3 development
[unix-history] / usr / src / cmd / tra.c
CommitLineData
2eee550a
BJ
1#include <sys/types.h>
2#include <stat.h>
3
4/*
5 * transcribe [ - ] [ -delay ] file
6 * Bill Joy UCB May 1977
7 *
8 * watch a file and copy it out
9 * as it grows. default interval for
10 * copying is 15 seconds.
11 * option - suppresses complete file from being copied out
12 * and only gives new stuff.
13 */
14struct stat stbuf;
15char buf[512];
16int i;
17off_t offset;
18int interval 15;
19int timeleft 32000;
20char *progname;
21int whole 1;
22
23main(argc, argv)
24 int argc;
25 char *argv[];
26{
27 progname = *argv++;
28 argc--;
29nxtarg:
30 if (argc > 1 && argv[0][0] == '-' && argv[0][1] == 0) {
31 argc--;
32 argv++;
33 whole = 0;
34 goto nxtarg;
35 }
36 if (argc > 1 && argv[0][0] == '+') {
37 timeleft = getdel(argv[0] + 1);
38 argc--;
39 argv++;
40 goto nxtarg;
41 }
42 if (argc > 1 && argv[0][0] == '-') {
43 interval = getdel(argv[0] + 1);
44 argv++;
45 argc--;
46 goto nxtarg;
47 }
48 if (argc != 1) {
49 printf("Usage: %s [ - ] [ -interval ] file\n", progname);
50 exit(1);
51 }
52 if (interval <= 0) {
53 printf("Unreasonable interval\n");
54 exit(1);
55 }
56 close(0);
57 if (open(argv[0], 0) < 0) {
58 perror(argv[0]);
59 exit(1);
60 }
61 if (whole == 0) {
62 fstat(0, &stbuf);
63 offset = stbuf.st_size;
64 }
65 do {
66 fstat(0, &stbuf);
67 if (stbuf.st_size > offset) {
68 lseek(0, (long) offset, 0);
69 while ((i = read(0, buf, sizeof buf)) > 0) {
70 offset =+ i;
71 write(1, buf, i);
72 }
73 }
74 sleep(interval);
75 timeleft =- interval;
76 } while (timeleft > 0);
77}
78
79getdel(cp)
80 char *cp;
81{
82 register int j;
83
84 j = 0;
85 do {
86 number(*cp);
87 j = j * 10 + *cp++ - '0';
88 } while (*cp);
89 return (j);
90}
91
92number(c)
93 char c;
94{
95 if (c < '0' || c > '9') {
96 printf("Bad number for interval\n");
97 exit(1);
98 }
99}