2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2017-01-01 20:00:33 -05:00
|
|
|
#include <fcntl.h>
|
2015-01-22 16:08:25 -05:00
|
|
|
#include <signal.h>
|
2017-01-01 20:00:33 -05:00
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-04 17:05:11 -05:00
|
|
|
eprintf("usage: %s [-ai] [file ...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2017-01-01 20:00:33 -05:00
|
|
|
int *fds = NULL;
|
|
|
|
size_t i, nfds;
|
|
|
|
ssize_t n;
|
|
|
|
int ret = 0, aflag = O_TRUNC, iflag = 0;
|
2013-06-14 14:20:47 -04:00
|
|
|
char buf[BUFSIZ];
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
2017-01-01 20:00:33 -05:00
|
|
|
aflag = O_APPEND;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
2015-01-22 16:08:25 -05:00
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2015-01-22 16:08:25 -05:00
|
|
|
if (iflag && signal(SIGINT, SIG_IGN) == SIG_ERR)
|
|
|
|
eprintf("signal:");
|
2017-01-01 20:00:33 -05:00
|
|
|
nfds = argc + 1;
|
|
|
|
fds = ecalloc(nfds, sizeof(*fds));
|
2014-06-02 18:15:52 -04:00
|
|
|
|
2016-06-23 14:37:23 -04:00
|
|
|
for (i = 0; i < argc; i++) {
|
2017-01-01 20:00:33 -05:00
|
|
|
if ((fds[i] = open(argv[i], O_WRONLY|O_CREAT|aflag, 0666)) < 0) {
|
|
|
|
weprintf("open %s:", argv[i]);
|
2016-06-23 14:37:23 -04:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 20:00:33 -05:00
|
|
|
fds[i] = 1;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2017-01-01 20:00:33 -05:00
|
|
|
while ((n = read(0, buf, sizeof(buf))) > 0) {
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
if (fds[i] >= 0 && writeall(fds[i], buf, n) < 0) {
|
|
|
|
weprintf("write %s:", (i != argc) ? argv[i] : "<stdout>");
|
|
|
|
fds[i] = -1;
|
2016-06-23 14:37:23 -04:00
|
|
|
ret = 1;
|
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-01 20:00:33 -05:00
|
|
|
if (n < 0)
|
|
|
|
eprintf("read <stdin>:");
|
2015-05-24 19:33:19 -04:00
|
|
|
|
|
|
|
return ret;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|