This commit is contained in:
Connor Lane Smith
2011-05-24 01:13:34 +01:00
parent 687e5411ee
commit 9714d7b1d3
8 changed files with 103 additions and 90 deletions

20
tee.c
View File

@@ -2,29 +2,35 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
int
main(int argc, char *argv[])
{
bool aflag = false;
char buf[BUFSIZ];
char buf[BUFSIZ], c;
int i, nfps = 1;
size_t n;
FILE **fps;
if(argc > 1 && !strcmp(argv[1], "-a"))
aflag = true;
while((c = getopt(argc, argv, "a")) != -1)
switch(c) {
case 'a':
aflag = true;
break;
default:
exit(EXIT_FAILURE);
}
if(!(fps = malloc(sizeof *fps)))
eprintf("malloc:");
fps[nfps-1] = stdout;
for(i = aflag ? 2 : 1; i < argc; i++) {
for(; optind < argc; optind++) {
if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
eprintf("realloc:");
if(!(fps[nfps-1] = fopen(argv[i], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[i]);
if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[optind]);
}
while((n = fread(buf, 1, sizeof buf, stdin)) > 0)
for(i = 0; i < nfps; i++)