summaryrefslogtreecommitdiff
path: root/usr.bin/tee
diff options
context:
space:
mode:
authorScott Soule Cheloha <cheloha@cvs.openbsd.org>2021-11-21 16:15:44 +0000
committerScott Soule Cheloha <cheloha@cvs.openbsd.org>2021-11-21 16:15:44 +0000
commit4486e88a598aa5caac39601c01d17437597c8da1 (patch)
treecbace2f824a46e1a7f52579f7ba913a3ff943f11 /usr.bin/tee
parent0d64508fb8cb1f23540478aebf83577ea9dcb1ae (diff)
tee(1): use idiomatic write loop
tee(1) handles partial writes correctly, but the more idiomatic write loop is shorter and easier to audit than this heterodox approach. ok millert@
Diffstat (limited to 'usr.bin/tee')
-rw-r--r--usr.bin/tee/tee.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/usr.bin/tee/tee.c b/usr.bin/tee/tee.c
index f0cd6f2daba..826aeaa9ca8 100644
--- a/usr.bin/tee/tee.c
+++ b/usr.bin/tee/tee.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tee.c,v 1.12 2017/07/11 13:14:59 bluhm Exp $ */
+/* $OpenBSD: tee.c,v 1.13 2021/11/21 16:15:43 cheloha Exp $ */
/* $NetBSD: tee.c,v 1.5 1994/12/09 01:43:39 jtc Exp $ */
/*
@@ -68,7 +68,6 @@ main(int argc, char *argv[])
struct list *p;
int fd;
ssize_t n, rval, wval;
- char *bp;
int append, ch, exitval;
char buf[8192];
@@ -112,16 +111,14 @@ main(int argc, char *argv[])
while ((rval = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
SLIST_FOREACH(p, &head, next) {
- n = rval;
- bp = buf;
- do {
- if ((wval = write(p->fd, bp, n)) == -1) {
+ for (n = 0; n < rval; n += wval) {
+ wval = write(p->fd, buf + n, rval - n);
+ if (wval == -1) {
warn("%s", p->name);
exitval = 1;
break;
}
- bp += wval;
- } while (n -= wval);
+ }
}
}
if (rval == -1) {