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