[project @ 2005-11-11 12:01:58 by simonmar]
[haskell-directory.git] / cbits / runProcess.c
index d117e36..a0c2453 100644 (file)
@@ -47,7 +47,9 @@ runProcess (char *const args[], char *workingDirectory, char **environment,
        pPrPr_disableITimers();
        
        if (workingDirectory) {
-           chdir (workingDirectory);
+           if (chdir (workingDirectory) < 0) {
+               return -1;
+           }
        }
        
        /* Set the SIGINT/SIGQUIT signal handlers in the child, if requested 
@@ -107,19 +109,29 @@ runInteractiveProcess (char *const args[],
        pPrPr_disableITimers();
        
        if (workingDirectory) {
-           chdir (workingDirectory);
+           if (chdir (workingDirectory) < 0) {
+               return -1;
+           }
        }
        
-       dup2 (fdStdInput[0], STDIN_FILENO);
-       dup2 (fdStdOutput[1], STDOUT_FILENO);
-       dup2 (fdStdError[1], STDERR_FILENO);
+       if (fdStdInput[0] != STDIN_FILENO) {
+           dup2 (fdStdInput[0], STDIN_FILENO);
+           close(fdStdInput[0]);
+       }
+
+       if (fdStdOutput[1] != STDOUT_FILENO) {
+           dup2 (fdStdOutput[1], STDOUT_FILENO);
+           close(fdStdOutput[1]);
+       }
+
+       if (fdStdError[1] != STDERR_FILENO) {
+           dup2 (fdStdError[1], STDERR_FILENO);
+           close(fdStdError[1]);
+       }
        
-       close(fdStdInput[0]);
        close(fdStdInput[1]);
        close(fdStdOutput[0]);
-       close(fdStdOutput[1]);
        close(fdStdError[0]);
-       close(fdStdError[1]);
        
        /* the child */
        if (environment) {
@@ -506,8 +518,6 @@ terminateProcess (ProcHandle handle)
        maperrno();
        return -1;
     }
-
-    CloseHandle((HANDLE) handle);
     return 0;
 }
 
@@ -523,8 +533,6 @@ getProcessExitCode (ProcHandle handle, int *pExitCode)
            maperrno();
            return -1;
        }
-       
-       CloseHandle((HANDLE) handle);
        return 1;
     }
     
@@ -543,8 +551,6 @@ waitForProcess (ProcHandle handle)
            maperrno();
            return -1;
        }
-       
-       CloseHandle((HANDLE) handle);
        return retCode;
     }