[PATCH] remove unnecessary calls to realloc from tee

From: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Tue, 3 Jun 2014 00:15:52 +0200

---
 tee.c |   19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/tee.c b/tee.c
index d78602b..d88e49c 100644
--- a/tee.c
+++ b/tee.c
_AT_@ -16,7 +16,7 @@ main(int argc, char *argv[])
 {
 	bool aflag = false;
 	char buf[BUFSIZ];
-	int i, nfps = 1;
+	int i, nfps;
 	size_t n;
 	FILE **fps = NULL;
 
_AT_@ -28,16 +28,15 @@ main(int argc, char *argv[])
 		usage();
 	} ARGEND;
 
-	if(!(fps = malloc(sizeof *fps)))
-		eprintf("malloc:");
-	fps[nfps-1] = stdout;
+	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;
 
-	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) {
 		for(i = 0; i < nfps; i++) {
 			if(fwrite(buf, 1, n, fps[i]) != n)
-- 
1.7.10.4
--------------050307090604090104080207--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Jun 03 2014 - 02:36:02 CEST