Annotation of 3BSD/cmd/tee.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * tee-- pipe fitting
                      3:  */
                      4: 
                      5: #include <signal.h>
                      6: #include <sys/types.h>
                      7: #include <sys/stat.h>
                      8: #include <errno.h>
                      9: #include <pagsiz.h>
                     10: 
                     11: #define        BUFSIZ  BSIZE
                     12: int openf[20] = { 1 };
                     13: int n = 1;
                     14: int t = 0;
                     15: int aflag;
                     16: 
                     17: char in[BUFSIZ];
                     18: 
                     19: char out[BUFSIZ];
                     20: 
                     21: extern errno;
                     22: long   lseek();
                     23: 
                     24: main(argc,argv)
                     25: char **argv;
                     26: {
                     27:        int register r,w,p;
                     28:        struct stat buf;
                     29:        while(argc>1&&argv[1][0]=='-') {
                     30:                switch(argv[1][1]) {
                     31:                case 'a':
                     32:                        aflag++;
                     33:                        break;
                     34:                case 'i':
                     35:                case 0:
                     36:                        signal(SIGINT, SIG_IGN);
                     37:                }
                     38:                argv++;
                     39:                argc--;
                     40:        }
                     41:        fstat(1,&buf);
                     42:        t = (buf.st_mode&S_IFMT)==S_IFCHR;
                     43:        if(lseek(1,0L,1)==-1&&errno==ESPIPE)
                     44:                t++;
                     45:        while(argc-->1) {
                     46:                if(aflag) {
                     47:                        openf[n] = open(argv[1],1);
                     48:                        if(openf[n] < 0)
                     49:                                openf[n] = creat(argv[1],0666);
                     50:                        lseek(openf[n++],0L,2);
                     51:                } else
                     52:                        openf[n++] = creat(argv[1],0666);
                     53:                if(stat(argv[1],&buf)>=0) {
                     54:                        if((buf.st_mode&S_IFMT)==S_IFCHR)
                     55:                                t++;
                     56:                } else {
                     57:                        puts("tee: cannot open ");
                     58:                        puts(argv[1]);
                     59:                        puts("\n");
                     60:                        n--;
                     61:                }
                     62:                argv++;
                     63:        }
                     64:        r = w = 0;
                     65:        for(;;) {
                     66:                for(p=0;p<BUFSIZ;) {
                     67:                        if(r>=w) {
                     68:                                if(t>0&&p>0) break;
                     69:                                w = read(0,in,BUFSIZ);
                     70:                                r = 0;
                     71:                                if(w<=0) {
                     72:                                        stash(p);
                     73:                                        return;
                     74:                                }
                     75:                        }
                     76:                        out[p++] = in[r++];
                     77:                }
                     78:                stash(p);
                     79:        }
                     80: }
                     81: 
                     82: stash(p)
                     83: {
                     84:        int k;
                     85:        int i;
                     86:        int d;
                     87:        d = t ? 16 : p;
                     88:        for(i=0; i<p; i+=d)
                     89:                for(k=0;k<n;k++)
                     90:                        write(openf[k], out+i, d<p-i?d:p-i);
                     91: }
                     92: 
                     93: puts(s)
                     94: char *s;
                     95: {
                     96:        while(*s)
                     97:                write(2,s++,1);
                     98: }

unix.superglobalmegacorp.com

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