add Control.Monad.Instances to nhc98 build
[haskell-directory.git] / cbits / runProcess.c
index dd33b66..39597b7 100644 (file)
@@ -6,7 +6,7 @@
 
 #include "HsBase.h"
 
-#if defined(mingw32_HOST_OS)
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
 #include <windows.h>
 #include <stdlib.h>
 #endif
@@ -23,7 +23,7 @@
 #include <signal.h>
 #endif
 
-#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
+#if !(defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32))
 /* ----------------------------------------------------------------------------
    UNIX versions
    ------------------------------------------------------------------------- */
@@ -114,16 +114,24 @@ runInteractiveProcess (char *const args[],
            }
        }
        
-       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) {
@@ -208,7 +216,7 @@ int waitForProcess (ProcHandle handle)
     else
        if (WIFSIGNALED(wstat))
        {
-           errno = EINTR;
+           return wstat;
        }
        else
        {
@@ -510,8 +518,6 @@ terminateProcess (ProcHandle handle)
        maperrno();
        return -1;
     }
-
-    CloseHandle((HANDLE) handle);
     return 0;
 }
 
@@ -527,8 +533,6 @@ getProcessExitCode (ProcHandle handle, int *pExitCode)
            maperrno();
            return -1;
        }
-       
-       CloseHandle((HANDLE) handle);
        return 1;
     }
     
@@ -547,8 +551,6 @@ waitForProcess (ProcHandle handle)
            maperrno();
            return -1;
        }
-       
-       CloseHandle((HANDLE) handle);
        return retCode;
     }