sbase/tee.c
FRIGN aaac1c8800 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.
2015-03-04 23:05:11 +01:00

54 lines
956 B
C

/* See LICENSE file for copyright and license details. */
#include <signal.h>
#include <stdio.h>
#include "util.h"
static void
usage(void)
{
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];
ARGBEGIN {
case 'a':
aflag = 1;
break;
case 'i':
iflag = 1;
break;
default:
usage();
} ARGEND;
if (iflag && signal(SIGINT, SIG_IGN) == SIG_ERR)
eprintf("signal:");
nfps = argc + 1;
fps = ecalloc(nfps, sizeof(*fps));
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))) {
for (i = 0; i < nfps; i++) {
if (fwrite(buf, 1, n, fps[i]) == n)
continue;
eprintf("fwrite %s:", (i != argc) ? argv[i] : "<stdout>");
}
}
if (ferror(stdin))
eprintf("fread <stdin>:");
return 0;
}