aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-12-22 20:54:03 -0600
committerRob Landley <rob@landley.net>2019-12-22 20:54:03 -0600
commitf3d4a2c72f315fd3bb0a939f6331ee0465fcfe2f (patch)
tree7f0d29dbd46cd015c1e4d965b3504ac8b8b81a84
parent058471577a73a693c681ce9059809c2e444a4bd7 (diff)
downloadtoybox-f3d4a2c72f315fd3bb0a939f6331ee0465fcfe2f.tar.gz
Bugfix: tee with no arguments was writing to stdout twice.
Add basic smoketest while we're at it.
-rwxr-xr-xtests/tee.test9
-rw-r--r--toys/posix/tee.c10
2 files changed, 14 insertions, 5 deletions
diff --git a/tests/tee.test b/tests/tee.test
new file mode 100755
index 00000000..fd3b9447
--- /dev/null
+++ b/tests/tee.test
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+testing "" "tee" "one" "" "one"
+testing "" "tee -" "two\n" "" "two\n"
+testing "" "tee one > two && cmp one two && echo that" "that\n" "" "three"
diff --git a/toys/posix/tee.c b/toys/posix/tee.c
index 78139a8d..ff8a29dc 100644
--- a/toys/posix/tee.c
+++ b/toys/posix/tee.c
@@ -54,18 +54,18 @@ void tee_main(void)
for (;;) {
struct fd_list *fdl;
- int len;
+ 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.
- fdl = TT.outputs;
- for (;;) {
- if(len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1;
+ 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;
- fdl = fdl->next;
+ if (fdl->fd == 1) out++;
}
}
}