From aaac1c880039564c87913ba1bbb08aaf28bf7ad7 Mon Sep 17 00:00:00 2001 From: FRIGN Date: Wed, 4 Mar 2015 23:05:11 +0100 Subject: [PATCH] Audit tee(1) 1) "duplicate" implies that you can only specify two outputs, "multiply" is a better word describing the functionality. 2) fix other wording in the manpage 3) fix usage() 4) reorder local variables 5) fix sizeof() style 6) we need argv later, don't increment argv and rather iterate over argc. 7) Improve error messages, print the filename which the write failed to instead of printing the buffer itself (how much sense does that make, printing 1024 Bytes of garbage?). Also, give the name of the function which failed. --- README | 2 +- tee.1 | 6 +++--- tee.c | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README b/README index 962865f..7745254 100644 --- a/README +++ b/README @@ -73,7 +73,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support, =* sync non-posix none =* tail yes none =* tar non-posix none -=* tee yes none +=*| tee yes none =* test yes none = time yes none =* touch yes none diff --git a/tee.1 b/tee.1 index 0a551cb..7fc6d85 100644 --- a/tee.1 +++ b/tee.1 @@ -1,16 +1,16 @@ -.Dd January 23, 2015 +.Dd March 4, 2015 .Dt TEE 1 .Os sbase .Sh NAME .Nm tee -.Nd duplicate stdin +.Nd multiply stdin .Sh SYNOPSIS .Nm .Op Fl ai .Op Ar file ... .Sh DESCRIPTION .Nm -writes from stdin to stdout and each +reads from stdin and writes to stdout and each .Ar file . .Sh OPTIONS .Bl -tag -width Ds diff --git a/tee.c b/tee.c index 98f9095..3bf1946 100644 --- a/tee.c +++ b/tee.c @@ -7,17 +7,16 @@ static void usage(void) { - eprintf("usage: %s [-ai] [file...]\n", argv0); + eprintf("usage: %s [-ai] [file ...]\n", argv0); } int main(int argc, char *argv[]) { + FILE **fps = NULL; + size_t i, n, nfps; int aflag = 0, iflag = 0; char buf[BUFSIZ]; - int i, nfps; - size_t n; - FILE **fps = NULL; ARGBEGIN { case 'a': @@ -33,21 +32,22 @@ main(int argc, char *argv[]) if (iflag && signal(SIGINT, SIG_IGN) == SIG_ERR) eprintf("signal:"); nfps = argc + 1; - fps = ecalloc(nfps, sizeof *fps); + fps = ecalloc(nfps, sizeof(*fps)); - for (i = 0; argc > 0; argc--, argv++, i++) - if (!(fps[i] = fopen(*argv, aflag ? "a" : "w"))) - eprintf("fopen %s:", *argv); + for (i = 0; i < argc; i++) + if (!(fps[i] = fopen(argv[i], aflag ? "a" : "w"))) + eprintf("fopen %s:", argv[i]); fps[i] = stdout; - while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) { + while ((n = fread(buf, 1, sizeof(buf), stdin))) { for (i = 0; i < nfps; i++) { - if (fwrite(buf, 1, n, fps[i]) != n) - eprintf("%s: write error:", buf); + if (fwrite(buf, 1, n, fps[i]) == n) + continue; + eprintf("fwrite %s:", (i != argc) ? argv[i] : ""); } } if (ferror(stdin)) - eprintf(": read error:"); + eprintf("fread :"); return 0; }