1
0
mirror of git://git.suckless.org/sbase synced 2024-12-25 16:32:37 +00:00
sbase/tee.c

43 lines
938 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"
int
main(int argc, char *argv[])
{
bool aflag = false;
2011-05-24 00:13:34 +00:00
char buf[BUFSIZ], c;
2011-05-23 01:36:34 +00:00
int i, nfps = 1;
size_t n;
FILE **fps;
2011-05-24 00:13:34 +00:00
while((c = getopt(argc, argv, "a")) != -1)
switch(c) {
case 'a':
aflag = true;
break;
default:
exit(EXIT_FAILURE);
}
2011-05-23 01:36:34 +00:00
if(!(fps = malloc(sizeof *fps)))
eprintf("malloc:");
fps[nfps-1] = stdout;
2011-05-24 00:13:34 +00:00
for(; optind < argc; optind++) {
2011-05-23 01:36:34 +00:00
if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
eprintf("realloc:");
2011-05-24 00:13:34 +00:00
if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[optind]);
2011-05-23 01:36:34 +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)
eprintf("%s: write error:", buf);
if(ferror(stdin))
eprintf("<stdin>: read error:");
return EXIT_SUCCESS;
}