1
0
mirror of git://git.suckless.org/sbase synced 2024-12-23 15:34:38 +00:00

remove unnecessary calls to realloc from tee

This commit is contained in:
Jakob Kramer 2014-06-03 00:15:52 +02:00 committed by sin
parent 7d1fd2621e
commit 2e1580ed2d

19
tee.c
View File

@ -16,7 +16,7 @@ main(int argc, char *argv[])
{ {
bool aflag = false; bool aflag = false;
char buf[BUFSIZ]; char buf[BUFSIZ];
int i, nfps = 1; int i, nfps;
size_t n; size_t n;
FILE **fps = NULL; FILE **fps = NULL;
@ -28,16 +28,15 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(!(fps = malloc(sizeof *fps))) nfps = argc + 1;
eprintf("malloc:"); if(!(fps = calloc(nfps, sizeof *fps)))
fps[nfps-1] = stdout; 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;
for(; argc > 0; argc--, argv++) {
if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
eprintf("realloc:");
if(!(fps[nfps-1] = fopen(argv[0], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[0]);
}
while((n = fread(buf, 1, sizeof buf, stdin)) > 0) { while((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
for(i = 0; i < nfps; i++) { for(i = 0; i < nfps; i++) {
if(fwrite(buf, 1, n, fps[i]) != n) if(fwrite(buf, 1, n, fps[i]) != n)