shithub: riscv

Download patch

ref: 127f8f048047e03d74001c4bbd50b7c0c09d7c0e
parent: 468851cde6d0a2c160f1d4beec49ca11eb5506c1
author: cinap_lenrek <[email protected]>
date: Sun Dec 21 00:12:56 EST 2014

tee: get rid of openf[100] array and just dup() filedescriptors to 3+[0..n-1]

--- a/sys/src/cmd/tee.c
+++ b/sys/src/cmd/tee.c
@@ -5,9 +5,12 @@
 #include <u.h>
 #include <libc.h>
 
+enum {
+	FDSTART = 3,
+};
+
 int	uflag;
 int	aflag;
-int	openf[100];
 
 char in[8192];
 
@@ -42,19 +45,21 @@
 	n = 0;
 	while(*argv) {
 		if(aflag) {
-			openf[n] = open(argv[0], OWRITE);
-			if(openf[n] < 0)
-				openf[n] = create(argv[0], OWRITE, 0666);
-			seek(openf[n], 0L, 2);
+			i = open(argv[0], OWRITE);
+			if(i < 0)
+				i = create(argv[0], OWRITE, 0666);
+			seek(i, 0L, 2);
 		} else
-			openf[n] = create(argv[0], OWRITE, 0666);
-		if(openf[n] < 0) {
+			i = create(argv[0], OWRITE, 0666);
+		if(i < 0) {
 			fprint(2, "tee: cannot open %s: %r\n", argv[0]);
-		} else
+		} else {
+			if(i != n+FDSTART)
+				dup(i, n+FDSTART);
 			n++;
+		}
 		argv++;
 	}
-	openf[n++] = 1;
 
 	for(;;) {
 		r = read(0, in, sizeof in);
@@ -61,7 +66,8 @@
 		if(r <= 0)
 			exits(nil);
 		for(i=0; i<n; i++)
-			write(openf[i], in, r);
+			write(i+FDSTART, in, r);
+		write(1, in, r);
 	}
 }