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