1
0
mirror of git://git.suckless.org/sbase synced 2024-12-23 15:34:38 +00:00
sbase/tee.c
sin 2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00:00

54 lines
979 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 ret = 0, 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>");
}
}
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
}