From: rrt Date: Tue, 12 Jun 2001 17:19:34 +0000 (+0000) Subject: [project @ 2001-06-12 17:19:34 by rrt] X-Git-Tag: Approximately_9120_patches~1769 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=a1948ea150401c87fa673fc266a4306f885e6609;p=ghc-hetmet.git [project @ 2001-06-12 17:19:34 by rrt] 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. --- diff --git a/ghc/lib/std/cbits/system.c b/ghc/lib/std/cbits/system.c index 5b8047b..b35b2b5 100644 --- a/ghc/lib/std/cbits/system.c +++ b/ghc/lib/std/cbits/system.c @@ -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 */ @@ -11,19 +11,33 @@ #include "HsStd.h" +#if defined(mingw32_TARGET_OS) +#include +#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;