1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2009 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* David Korn
* AT&T Bell Laboratories
*
* tee
*/
static const char usage[] =
"[-?\n@(#)$Id: tee (AT&T Research) 2009-06-19 $\n]"
USAGE_LICENSE
"[+NAME?tee - duplicate standard input]"
"[+DESCRIPTION?\btee\b copies standard input to standard output "
"and to zero or more files. The options determine whether "
"the specified files are overwritten or appended to. The "
"\btee\b utility does not buffer output. If writes to any "
"\afile\a fail, writes to other files continue although \btee\b "
"will exit with a non-zero exit status.]"
"[+?The number of \afile\a operands that can be specified is limited "
"by the underlying operating system.]"
"[a:append?Append the standard input to the given files rather "
"than overwriting them.]"
"[i:ignore-interrupts?Ignore SIGINT signal.]"
"[l:linebuffer?Set the standard output to be line buffered.]"
"\n"
"\n[file ...]\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?All files copies successfully.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bcat\b(1), \bsignal\b(3)]"
;
#include <cmd.h>
#include <ls.h>
#include <sig.h>
typedef struct Tee_s
{
Sfdisc_t disc;
int line;
int fd[1];
} Tee_t;
/*
* This discipline writes to each file in the list given in handle
*/
static ssize_t
tee_write(Sfio_t* fp, const void* buf, size_t n, Sfdisc_t* handle)
{
register const char* bp;
register const char* ep;
register int* hp = ((Tee_t*)handle)->fd;
register int fd = sffileno(fp);
register ssize_t r;
do
{
bp = (const char*)buf;
ep = bp + n;
while (bp < ep)
{
if ((r = write(fd, bp, ep - bp)) <= 0)
return -1;
bp += r;
}
} while ((fd = *hp++) >= 0);
return n;
}
static void
tee_cleanup(register Tee_t* tp)
{
register int* hp;
register int n;
if (tp)
{
sfdisc(sfstdout, NiL);
if (tp->line >= 0)
sfset(sfstdout, SF_LINE, tp->line);
for (hp = tp->fd; (n = *hp) >= 0; hp++)
close(n);
}
}
int
b_tee(int argc, register char** argv, void* context)
{
register Tee_t* tp = 0;
register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY;
register int n;
register int* hp;
register char* cp;
int line;
Sfdisc_t tee_disc;
if (argc <= 0)
{
if (context && (tp = (Tee_t*)sh_context(context)->data))
{
sh_context(context)->data = 0;
tee_cleanup(tp);
}
return 0;
}
cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_CALLBACK);
line = -1;
for (;;)
{
switch (optget(argv, usage))
{
case 'a':
oflag &= ~O_TRUNC;
oflag |= O_APPEND;
continue;
case 'i':
signal(SIGINT, SIG_IGN);
continue;
case 'l':
line = sfset(sfstdout, 0, 0) & SF_LINE;
if ((line == 0) == (opt_info.num == 0))
line = -1;
else
sfset(sfstdout, SF_LINE, !!opt_info.num);
continue;
case ':':
error(2, "%s", opt_info.arg);
break;
case '?':
error(ERROR_usage(2), "%s", opt_info.arg);
break;
}
break;
}
if (error_info.errors)
error(ERROR_usage(2), "%s", optusage(NiL));
argv += opt_info.index;
argc -= opt_info.index;
#if _ANCIENT_BSD_COMPATIBILITY
if (*argv && streq(*argv, "-"))
{
signal(SIGINT, SIG_IGN);
argv++;
argc--;
}
#endif
if (argc > 0)
{
if (tp = (Tee_t*)stakalloc(sizeof(Tee_t) + argc * sizeof(int)))
{
memset(&tp->disc, 0, sizeof(tp->disc));
tp->disc.writef = tee_write;
if (context)
sh_context(context)->data = (void*)tp;
tp->line = line;
hp = tp->fd;
while (cp = *argv++)
{
if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
error(ERROR_system(0), "%s: cannot create", cp);
else
hp++;
}
if (hp == tp->fd)
tp = 0;
else
{
*hp = -1;
sfdisc(sfstdout, &tp->disc);
}
}
else
error(ERROR_exit(0), "out of space");
}
if ((sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin)) && errno != EPIPE)
error(ERROR_system(0), "read error");
if (sfsync(sfstdout))
error(ERROR_system(0), "write error");
tee_cleanup(tp);
return error_info.errors;
}
|