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; }