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>
|
|
|
|
#include <stdlib.h>
|
2011-05-24 00:13:34 +00:00
|
|
|
#include <unistd.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-01-22 21:08:25 +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-01-22 21:08:25 +00:00
|
|
|
int aflag = 0, iflag = 0;
|
2013-06-14 18:20:47 +00:00
|
|
|
char buf[BUFSIZ];
|
2014-06-02 22:15:52 +00:00
|
|
|
int i, nfps;
|
2011-05-23 01:36:34 +00:00
|
|
|
size_t n;
|
2014-06-01 12:59:47 +00:00
|
|
|
FILE **fps = NULL;
|
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();
|
|
|
|
} ARGEND;
|
|
|
|
|
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;
|
2014-11-16 10:07:26 +00:00
|
|
|
fps = ecalloc(nfps, sizeof *fps);
|
2014-06-02 22:15:52 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; argc > 0; argc--, argv++, i++)
|
|
|
|
if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
|
2014-06-02 22:15:52 +00:00
|
|
|
eprintf("fopen %s:", *argv);
|
|
|
|
fps[i] = stdout;
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
|
|
|
|
for (i = 0; i < nfps; i++) {
|
|
|
|
if (fwrite(buf, 1, n, fps[i]) != n)
|
2011-05-23 01:36:34 +00:00
|
|
|
eprintf("%s: write error:", buf);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ferror(stdin))
|
2011-05-23 01:36:34 +00:00
|
|
|
eprintf("<stdin>: read error:");
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|