From 82af8c9f04947964a012f0d1adee8d2bd40b822a Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:51:36 +0800 Subject: [PATCH] tee: syscall return values * Consistently check return val of syscalls open()/read()/write()/close() for -1 instead of <1 * The OpenBSD version already follows this pattern * While here, remove switch case for -?; the default case is good enough for printing usage string --- usr.bin/tee/tee.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/usr.bin/tee/tee.c b/usr.bin/tee/tee.c index f534e7ea5b7eb..0c06f0205f9ef 100644 --- a/usr.bin/tee/tee.c +++ b/usr.bin/tee/tee.c @@ -84,7 +84,6 @@ main(int argc, char *argv[]) case 'i': (void)signal(SIGINT, SIG_IGN); break; - case '?': default: (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n"); exit(1); @@ -99,13 +98,13 @@ main(int argc, char *argv[]) for (exitval = 0; *argv; ++argv) if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND : - O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) { + O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) { warn("%s", *argv); exitval = 1; } else add(fd, *argv); - while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0) + while ((rval = read(STDIN_FILENO, buf, BSIZE)) != 0 && rval != -1) for (p = head; p; p = p->next) { const char *bp = buf; size_t n = rval; @@ -120,7 +119,7 @@ main(int argc, char *argv[]) bp += wval; } while (n -= wval); } - if (rval < 0) { + if (rval == -1) { warn("read"); exitval = 1; }