1
0
mirror of git://git.suckless.org/sbase synced 2024-12-23 23:43:33 +00:00
sbase/tee.c

52 lines
917 B
C
Raw Normal View History

2011-05-23 01:36:34 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
2011-05-24 00:13:34 +00:00
#include <unistd.h>
2011-05-23 01:36:34 +00:00
#include "util.h"
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-a] [file...]\n", argv0);
}
2011-05-23 01:36:34 +00:00
int
main(int argc, char *argv[])
{
bool aflag = false;
2013-06-14 18:20:47 +00:00
char buf[BUFSIZ];
int i, nfps;
2011-05-23 01:36:34 +00:00
size_t n;
FILE **fps = NULL;
2011-05-23 01:36:34 +00:00
2013-06-14 18:20:47 +00:00
ARGBEGIN {
case 'a':
aflag = true;
break;
default:
usage();
} ARGEND;
nfps = argc + 1;
if(!(fps = calloc(nfps, sizeof *fps)))
eprintf("calloc:");
for(i = 0; argc > 0; argc--, argv++, i++)
if(!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
eprintf("fopen %s:", *argv);
fps[i] = stdout;
2011-05-23 01:36:34 +00:00
2013-06-14 18:20:47 +00:00
while((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
for(i = 0; i < nfps; i++) {
2011-05-23 01:36:34 +00:00
if(fwrite(buf, 1, n, fps[i]) != n)
eprintf("%s: write error:", buf);
2013-06-14 18:20:47 +00:00
}
}
2011-05-23 01:36:34 +00:00
if(ferror(stdin))
eprintf("<stdin>: read error:");
free(fps);
2013-06-14 18:20:47 +00:00
return EXIT_SUCCESS;
2011-05-23 01:36:34 +00:00
}