From 3269a81ce20fcdc4bfb196f45585a3c632896f05 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 31 Jul 2017 20:04:07 +0100 Subject: win32: rewrite implementation of system(3) Rewrite mingw_system using mingw_spawn_proc. Also fix return values. --- win32/system.c | 77 +++++++++------------------------------------------------- 1 file changed, 11 insertions(+), 66 deletions(-) diff --git a/win32/system.c b/win32/system.c index c3e2c316e..02aaaa0a1 100644 --- a/win32/system.c +++ b/win32/system.c @@ -2,76 +2,21 @@ int mingw_system(const char *cmd) { - STARTUPINFO siStartInfo; - PROCESS_INFORMATION piProcInfo; - int success; - int len, count; - char *cmd_buff = NULL; - const char *s; - char *t; - DWORD ret; + const char *argv[4] = { "sh", "-c", cmd, NULL }; + intptr_t proc; + HANDLE h; + DWORD ret = 0; - if ( cmd == NULL ) { + if (cmd == NULL) return 1; - } - /* count double quotes */ - count = 0; - for ( s=cmd; *s; ++s ) { - if ( *s == '"' ) { - ++count; - } - } - - len = strlen(cmd) + 10 + count; - if ( (cmd_buff=malloc(len)) == NULL ) { + if ((proc=mingw_spawn_proc((char **)argv)) == -1) return -1; - } - - /* escape double quotes */ - strcpy(cmd_buff, "sh -c \""); - for ( s=cmd,t=cmd_buff+strlen(cmd_buff); *s; ++s ) { - if ( *s == '"' ) { - *t++ = '\\'; - } - *t++ = *s; - } - *t++ = '"'; - *t = '\0'; - - /* Now create the child process */ - ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); - siStartInfo.cb = sizeof(STARTUPINFO); - siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); - siStartInfo.dwFlags = STARTF_USESTDHANDLES; - - success = CreateProcess(NULL, - (LPTSTR)cmd_buff, /* command line */ - NULL, /* process security attributes */ - NULL, /* primary thread security attributes */ - TRUE, /* handles are inherited */ - 0, /* creation flags */ - NULL, /* use parent's environment */ - NULL, /* use parent's current directory */ - &siStartInfo, /* STARTUPINFO pointer */ - &piProcInfo); /* receives PROCESS_INFORMATION */ - - if ( !success ) { - free(cmd_buff); - return 127; - } - - free(cmd_buff); - - WaitForSingleObject(piProcInfo.hProcess, INFINITE); - - ret = 0; - GetExitCodeProcess(piProcInfo.hProcess, &ret); - CloseHandle(piProcInfo.hProcess); - CloseHandle(piProcInfo.hThread); + h = (HANDLE)proc; + WaitForSingleObject(h, INFINITE); + GetExitCodeProcess(h, &ret); + CloseHandle(h); - return ret; + return ret << 8; } -- cgit v1.2.3-55-g6feb