aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2020-11-22 08:36:12 -0600
committerRob Landley <rob@landley.net>2020-11-22 08:36:12 -0600
commitfdfffae6da237b861ff3ef7394a9fc539e5a6e8c (patch)
tree7aa27a1e4bba606880ced8f8fa86d7fc91e45abe /toys
parentc251e32521229b7bd89a765481e2cd3b1ef02357 (diff)
downloadtoybox-fdfffae6da237b861ff3ef7394a9fc539e5a6e8c.tar.gz
Minor cleanup.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/tee.c27
1 files changed, 11 insertions, 16 deletions
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;
}
}