Annotation of coherent/d/bin/tee.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * tee.c
                      3:  * 2/12/92
                      4:  * Pipe redirection.
                      5:  * Usage: tee [ -a ] [ -i ] [ file ] ...
                      6:  * Rec'd from Lauren Weinstein, 7-16-84.
                      7:  * Hacked by rec to use stdio so that everything doesn't come out buffered.
                      8:  * Slightly rewritten by steve 4/30/91 for clarity; added "isatty" check.
                      9:  */
                     10: 
                     11: #include       <stdio.h>
                     12: #include       <errno.h>
                     13: #include       <signal.h>
                     14: 
                     15: #define        NUFILE  _NFILE                  /* max # open files */
                     16: #define        USAGE   "Usage: tee [ -a ] [ -i ] [ file ] ...\n"
                     17: 
                     18: /* External. */
                     19: extern int     exit();
                     20: 
                     21: /* Forward. */
                     22: FILE   *openf();
                     23: void   fatal();
                     24: 
                     25: /* Global. */
                     26: int    aflag;
                     27: 
                     28: main(argc, argv) int argc; register char *argv[];
                     29: {
                     30:        register int c;
                     31:        register FILE **fpp;
                     32:        FILE *fp[NUFILE];
                     33: 
                     34:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                     35:                signal(SIGINT, exit);
                     36: 
                     37:        /* Process option arguments. */
                     38:        while (*++argv && argv[0][0]=='-') {
                     39:                switch (argv[0][1]) {
                     40:                case 'a':
                     41:                        ++aflag;
                     42:                        break;
                     43:                case 'i':
                     44:                        signal(SIGINT, SIG_IGN);
                     45:                        break;
                     46:                default:
                     47:                        fprintf(stderr, USAGE);
                     48:                        exit(1);
                     49:                }
                     50:        }
                     51: 
                     52:        /* Open file arguments. */
                     53:        for (fpp = fp; *argv; ) {
                     54:                if (fpp >= &fp[NUFILE])
                     55:                        fatal("too many files");
                     56:                *fpp++ = openf(*argv++);
                     57:        }
                     58:        *fpp = NULL;
                     59: 
                     60:        /* Make i/o unbuffered if interactive. */
                     61:        if (isatty(fileno(stdin)))
                     62:                setbuf(stdout, NULL);
                     63:        if (isatty(fileno(stdout)))
                     64:                setbuf(stdin, NULL);
                     65: 
                     66:        /* Copy stdin to stdout, duplicate to each specified file. */
                     67:        while ((c = getchar()) != EOF) {
                     68:                putchar(c);
                     69:                for (fpp = fp; *fpp != NULL; fpp++) {
                     70:                        if (aflag)
                     71:                                fseek(*fpp, 0L, SEEK_END);
                     72:                        putc(c, *fpp);
                     73:                }
                     74:        }
                     75: 
                     76:        /* Done. */
                     77:        exit(0);
                     78: }
                     79: 
                     80: /*
                     81:  * Cry and die.
                     82:  */
                     83: /* VARARGS */
                     84: void
                     85: fatal(s) char *s;
                     86: {      fprintf(stderr, "tee: %r\n", &s);
                     87:        exit(1);
                     88: }
                     89: 
                     90: /*
                     91:  * Open a file.
                     92:  */
                     93: FILE *
                     94: openf(file) char *file;
                     95: {
                     96:        register FILE *fp;
                     97: 
                     98:        if ((fp = fopen(file, (aflag) ? "a" : "w")) != NULL) {
                     99:                if (aflag) {
                    100:                        setbuf(fp, NULL);
                    101:                        fseek(fp, 0L, SEEK_END);
                    102:                }
                    103:                return fp;
                    104:        }
                    105:        switch (errno) {
                    106:        case EMFILE:
                    107:        case ENFILE:
                    108:                fatal("too many files");
                    109:                break;
                    110:        default:
                    111:                fatal("cannot create %s", file);
                    112:        }
                    113: }
                    114: 
                    115: /* end of tee.c */

unix.superglobalmegacorp.com

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