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

Defined functions

main defined in line 26; never used
puts defined in line 95; used 3 times
stash defined in line 84; used 2 times

Defined variables

aflag defined in line 17; used 2 times
in defined in line 19; used 2 times
n defined in line 15; used 7 times
openf defined in line 14; used 6 times
out defined in line 21; used 2 times
sccsid defined in line 2; never used
t defined in line 16; used 5 times

Defined macros

BUFSIZ defined in line 13; used 4 times
Last modified: 1987-02-08
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3176
Valid CSS Valid XHTML 1.0 Strict