Tcl Source Code

Artifact [4747801b08]
Login

Artifact 4747801b080344d0d69a7413f3defe15a2a122a7:

Attachment "dup.patch" to ticket [2827015fff] added by ferrieux 2009-07-26 06:09:36.
Index: unix/tclUnixPipe.c
===================================================================
RCS file: /cvsroot/tcl/tcl/unix/tclUnixPipe.c,v
retrieving revision 1.48
diff -u -r1.48 tclUnixPipe.c
--- unix/tclUnixPipe.c	9 Jan 2009 11:21:46 -0000	1.48
+++ unix/tclUnixPipe.c	25 Jul 2009 23:03:50 -0000
@@ -451,23 +451,50 @@
     pid = fork();
     if (pid == 0) {
 	int joinThisError = errorFile && (errorFile == outputFile);
+	int fd0,fd1,fd2;
+	int closeOut=0,closeErr=0;
 
 	fd = GetFd(errPipeOut);
 
 	/*
 	 * Set up stdio file handles for the child process.
 	 */
+	fd0 = GetFd(inputFile);
+	fd1 = GetFd(outputFile);
+	fd2 = GetFd(outputFile);
+	
+	if (fd1 == 0) {
+	    outputFile = MakeFile(dup(0));
+	    closeOut = 1;
+        }
+	if (fd2 == 0) {
+	    errorFile = MakeFile(dup(0));
+	    closeErr = 1;
+        }
+	if (!SetupStdFile(inputFile, TCL_STDIN)) goto error;
+
+	if (fd2 == 1) {
+	    errorFile = MakeFile(dup(1));
+	    closeErr = 1;
+	}
+	if (!SetupStdFile(outputFile, TCL_STDOUT)) goto error;
 
-	if (!SetupStdFile(inputFile, TCL_STDIN)
-		|| !SetupStdFile(outputFile, TCL_STDOUT)
-		|| (!joinThisError && !SetupStdFile(errorFile, TCL_STDERR))
+	if (!joinThisError && !SetupStdFile(errorFile, TCL_STDERR)
 		|| (joinThisError &&
-			((dup2(1,2) == -1) || (fcntl(2, F_SETFD, 0) != 0)))) {
-	    sprintf(errSpace,
-		    "%dforked process couldn't set up input/output: ", errno);
-	    (void)write(fd, errSpace, (size_t) strlen(errSpace));
-	    _exit(1);
-	}
+		    ((dup2(1,2) == -1) || (fcntl(2, F_SETFD, 0) != 0))))
+	    goto error;
+
+	while(0)
+	    {
+	    error:
+		sprintf(errSpace,
+			"%dforked process couldn't set up input/output: ", errno);
+		(void)write(fd, errSpace, (size_t) strlen(errSpace));
+		_exit(1);
+	    }
+
+	if (closeOut) close(GetFd(outputFile));
+	if (closeErr) close(GetFd(errorFile));
 
 	/*
 	 * Close the input side of the error pipe.