[project @ 2001-06-12 17:19:34 by rrt]
authorrrt <unknown>
Tue, 12 Jun 2001 17:19:34 +0000 (17:19 +0000)
committerrrt <unknown>
Tue, 12 Jun 2001 17:19:34 +0000 (17:19 +0000)
Reimplement system() properly on Windows. This avoids two problems with
Windows system(): first, the maximum command-line length, and secondly, the
fact that system() returns before the command has completed, and sleep() was
used to hack around this.

This doesn't affect the use of kludgedSystem, which needs to be removed for
GHC not to need a shell at all.

ghc/lib/std/cbits/system.c

index 5b8047b..b35b2b5 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: system.c,v 1.13 2001/05/30 16:39:22 sewardj Exp $
+ * $Id: system.c,v 1.14 2001/06/12 17:19:34 rrt Exp $
  *
  * system Runtime Support
  */
 
 #include "HsStd.h"
 
+#if defined(mingw32_TARGET_OS)
+#include <windows.h>
+#endif
+
 HsInt
 systemCmd(HsAddr cmd)
 {
 #if defined(mingw32_TARGET_OS)
-   /* There's no fork() under Windows, so we fall back on using libc's
-      system() instead. (It in turn has problems, as it does not wait
-      until the sub shell has finished before returning. Using Sleep()
-      works around that.) */
-  if (system(cmd) < 0) {
-     return -1;
-  }
-  Sleep(1000);
-  return 0;
+  STARTUPINFO sInfo;
+  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;
+
+  if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo))
+    return -1;
+  WaitForSingleObject(pInfo.hProcess, INFINITE);
+  if (GetExitCodeProcess(pInfo.hProcess, &retCode) == 0) return -1;
+  CloseHandle(pInfo.hProcess);
+  CloseHandle(pInfo.hThread);
+  return retCode;
 #else
     int pid;
     int wstat;