[project @ 2004-10-14 14:58:50 by simonmar]
[ghc-base.git] / cbits / rawSystem.c
index e9ead67..7af7747 100644 (file)
@@ -1,5 +1,10 @@
 /* 
- * (c) The University of Glasgow 1994-2003
+ * (c) The University of Glasgow 1994-2004
+ *
+ * WARNING: this file is here for backwards compatibility only.  It is
+ * not included as part of the base package, but is #included into the
+ * compiler and the runghc utility when building either of these with 
+ * an old version of GHC.
  *
  * shell-less system Runtime Support (see System.Cmd.rawSystem).
  */
@@ -7,7 +12,16 @@
 /* The itimer stuff in this module is non-posix */
 /* #include "PosixSource.h" */
 
+/* This ifdef is required because this source might be compiled by an
+ * external compiler.  See ghc/utils/runghc/rawSystem.c for example.
+ */
+#ifdef __GLASGOW_HASKELL__
+#if __GLASGOW_HASKELL__ < 603
 #include "config.h"
+#else
+#include "ghcconfig.h"
+#endif
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -57,24 +71,25 @@ rawSystem(HsAddr cmd)
   PROCESS_INFORMATION pInfo;
   DWORD retCode;
 
-  sInfo.cb              = sizeof(STARTUPINFO);
-  sInfo.lpReserved      = NULL;
-  sInfo.lpReserved2     = NULL;
-  sInfo.cbReserved2     = 0;
-  sInfo.lpDesktop       = NULL;
-  sInfo.lpTitle         = NULL;
-  sInfo.dwFlags         = 0;
+  ZeroMemory(&sInfo, sizeof(sInfo));
+  sInfo.cb = sizeof(sInfo);
 
-  if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &sInfo, &pInfo))
+  if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &sInfo, &pInfo)) {
     /* The 'TRUE' says that the created process should share
        handles with the current process.  This is vital to ensure
        that error messages sent to stderr actually appear on the screen.
        Since we are going to wait for the process to terminate anyway,
        there is no problem with such sharing. */
 
-    return -1;
+      errno = EINVAL; // ToDo: wrong, caller should use GetLastError()
+      return -1;
+  }
   WaitForSingleObject(pInfo.hProcess, INFINITE);
-  if (GetExitCodeProcess(pInfo.hProcess, &retCode) == 0) return -1;
+  if (GetExitCodeProcess(pInfo.hProcess, &retCode) == 0) {
+      errno = EINVAL; // ToDo: wrong, caller should use GetLastError()
+      return -1;
+  }
+
   CloseHandle(pInfo.hProcess);
   CloseHandle(pInfo.hThread);
   return retCode;
@@ -91,7 +106,7 @@ rawSystem(HsAddr cmd, HsAddr args)
 
     switch(pid = fork()) {
     case -1:
-       if (errno != EINTR) {
+       {
            return -1;
        }
     case 0: