[hackers] [sbase] Audit tee(1) || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 4 Mar 2015 23:54:08 +0100 (CET)

commit adf16927239afcde67a0101330201399594ba004
Author: FRIGN <dev_AT_frign.de>
Date: Wed Mar 4 23:05:11 2015 +0100

    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.

diff --git a/README b/README
index 962865f..7745254 100644
--- a/README
+++ b/README
_AT_@ -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
_AT_@ -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
_AT_@ -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':
_AT_@ -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] : "<stdout>");
                 }
         }
         if (ferror(stdin))
- eprintf("<stdin>: read error:");
+ eprintf("fread <stdin>:");
 
         return 0;
 }
Received on Wed Mar 04 2015 - 23:54:08 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 05 2015 - 00:00:26 CET