Annotation of 3BSD/cmd/tra.c, revision 1.1

1.1     ! root        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:  */
        !            14: struct stat stbuf;
        !            15: char buf[512];
        !            16: int i;
        !            17: off_t offset;
        !            18: int interval 15;
        !            19: int timeleft   32000;
        !            20: char *progname;
        !            21: int whole 1;
        !            22: 
        !            23: main(argc, argv)
        !            24:        int argc;
        !            25:        char *argv[];
        !            26: {
        !            27:        progname = *argv++;
        !            28:        argc--;
        !            29: nxtarg:
        !            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: 
        !            79: getdel(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: 
        !            92: number(c)
        !            93:        char c;
        !            94: {
        !            95:        if (c < '0' || c > '9') {
        !            96:                printf("Bad number for interval\n");
        !            97:                exit(1);
        !            98:        }
        !            99: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.