2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-22 21:08:25 +00:00
|
|
|
#include <signal.h>
|
2011-05-23 01:36:34 +00:00
|
|
|
#include <stdio.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-04 22:05:11 +00:00
|
|
|
eprintf("usage: %s [-ai] [file ...]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-04 22:05:11 +00:00
|
|
|
FILE **fps = NULL;
|
|
|
|
size_t i, n, nfps;
|
2015-05-24 23:33:19 +00:00
|
|
|
int ret = 0, aflag = 0, iflag = 0;
|
2013-06-14 18:20:47 +00:00
|
|
|
char buf[BUFSIZ];
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
2014-11-13 20:24:47 +00:00
|
|
|
aflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2015-01-22 21:08:25 +00:00
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2015-01-22 21:08:25 +00:00
|
|
|
if (iflag && signal(SIGINT, SIG_IGN) == SIG_ERR)
|
|
|
|
eprintf("signal:");
|
2014-06-02 22:15:52 +00:00
|
|
|
nfps = argc + 1;
|
2015-03-04 22:05:11 +00:00
|
|
|
fps = ecalloc(nfps, sizeof(*fps));
|
2014-06-02 22:15:52 +00:00
|
|
|
|
2015-03-04 22:05:11 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if (!(fps[i] = fopen(argv[i], aflag ? "a" : "w")))
|
|
|
|
eprintf("fopen %s:", argv[i]);
|
2014-06-02 22:15:52 +00:00
|
|
|
fps[i] = stdout;
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2015-03-04 22:05:11 +00:00
|
|
|
while ((n = fread(buf, 1, sizeof(buf), stdin))) {
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < nfps; i++) {
|
2015-03-04 22:05:11 +00:00
|
|
|
if (fwrite(buf, 1, n, fps[i]) == n)
|
|
|
|
continue;
|
|
|
|
eprintf("fwrite %s:", (i != argc) ? argv[i] : "<stdout>");
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
|
|
|
|
|
|
|
|
return ret;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|