From fdfffae6da237b861ff3ef7394a9fc539e5a6e8c Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 22 Nov 2020 08:36:12 -0600 Subject: Minor cleanup. --- toys/posix/tee.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/tee.c b/toys/posix/tee.c index 43529426..88f73618 100644 --- a/toys/posix/tee.c +++ b/toys/posix/tee.c @@ -24,6 +24,7 @@ config TEE GLOBALS( void *outputs; + int out; ) struct fd_list { @@ -39,33 +40,27 @@ static void do_tee_open(int fd, char *name) temp = xmalloc(sizeof(struct fd_list)); temp->next = TT.outputs; - temp->fd = fd; + if (1 == (temp->fd = fd)) TT.out++; TT.outputs = temp; } void tee_main(void) { + struct fd_list *fdl; + int len; + if (FLAG(i)) xsignal(SIGINT, SIG_IGN); - // Open output files + // Open output files (plus stdout if not already in output list) loopfiles_rw(toys.optargs, O_RDWR|O_CREAT|WARN_ONLY|(FLAG(a)?O_APPEND:O_TRUNC), 0666, do_tee_open); + if (!TT.out) do_tee_open(1, 0); + // Read data from stdin, write to each output file. for (;;) { - struct fd_list *fdl; - int len, out = 0; - - // Read data from stdin - len = xread(0, toybuf, sizeof(toybuf)); - if (len<1) break; - - // Write data to each output file, plus stdout. - for (fdl = TT.outputs; ;fdl = fdl->next) { - if (!fdl && out) break; - if (len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1; - if (!fdl) break; - if (fdl->fd == 1) out++; - } + if (1>(len = xread(0, toybuf, sizeof(toybuf)))) break; + for (fdl = TT.outputs; fdl;fdl = fdl->next) + if (len != writeall(fdl->fd, toybuf, len)) toys.exitval = 1; } } -- cgit v1.2.3