[MSVCRT] Follow-up of #5032 (f172503)

Use _cwait() instead of WaitForSingleObject and GetExitCodeProcess.
Use malloc/free instead of LocalAlloc/LocalFree.
This commit is contained in:
Katayama Hirofumi MZ 2023-03-05 23:20:12 +09:00
parent f172503d57
commit 5a7dbd6064

View file

@ -14,7 +14,6 @@
#include <string.h> #include <string.h>
#include <process.h> #include <process.h>
/* /*
* @implemented * @implemented
*/ */
@ -26,7 +25,7 @@ int system(const char *command)
PROCESS_INFORMATION ProcessInformation; PROCESS_INFORMATION ProcessInformation;
STARTUPINFOA StartupInfo; STARTUPINFOA StartupInfo;
BOOL result; BOOL result;
DWORD exit_code; int status;
char cmd_exe[MAX_PATH]; char cmd_exe[MAX_PATH];
szComSpec = getenv("COMSPEC"); szComSpec = getenv("COMSPEC");
@ -44,7 +43,7 @@ int system(const char *command)
szComSpec = cmd_exe; szComSpec = cmd_exe;
} }
szCmdLine = LocalAlloc(LPTR, 1 + strlen(szComSpec) + 5 + strlen(command) + 1); szCmdLine = malloc(1 + strlen(szComSpec) + 5 + strlen(command) + 1);
if (szCmdLine == NULL) if (szCmdLine == NULL)
{ {
_dosmaperr(GetLastError()); _dosmaperr(GetLastError());
@ -78,7 +77,7 @@ int system(const char *command)
NULL, NULL,
&StartupInfo, &StartupInfo,
&ProcessInformation); &ProcessInformation);
LocalFree(szCmdLine); free(szCmdLine);
if (result == FALSE) if (result == FALSE)
{ {
@ -89,23 +88,22 @@ int system(const char *command)
CloseHandle(ProcessInformation.hThread); CloseHandle(ProcessInformation.hThread);
/* Wait for the process to exit */ /* Wait for the process to exit */
WaitForSingleObject(ProcessInformation.hProcess, INFINITE); _cwait(&status, (intptr_t)ProcessInformation.hProcess, 0);
GetExitCodeProcess(ProcessInformation.hProcess, &exit_code);
CloseHandle(ProcessInformation.hProcess); CloseHandle(ProcessInformation.hProcess);
_set_errno(0); _set_errno(0);
return (int)exit_code; return status;
} }
int CDECL _wsystem(const wchar_t* cmd) int CDECL _wsystem(const wchar_t *cmd)
{ {
wchar_t *cmdline = NULL; wchar_t *cmdline = NULL;
wchar_t *comspec = NULL; wchar_t *comspec = NULL;
PROCESS_INFORMATION process_info; PROCESS_INFORMATION process_info;
STARTUPINFOW startup_info; STARTUPINFOW startup_info;
BOOL result; BOOL result;
DWORD exit_code; int status;
wchar_t cmd_exe[MAX_PATH]; wchar_t cmd_exe[MAX_PATH];
comspec = _wgetenv(L"COMSPEC"); comspec = _wgetenv(L"COMSPEC");
@ -123,7 +121,7 @@ int CDECL _wsystem(const wchar_t* cmd)
comspec = cmd_exe; comspec = cmd_exe;
} }
cmdline = LocalAlloc(LPTR, (1 + wcslen(comspec) + 5 + wcslen(cmd) + 1) * sizeof(wchar_t)); cmdline = malloc((1 + wcslen(comspec) + 5 + wcslen(cmd) + 1) * sizeof(wchar_t));
if (cmdline == NULL) if (cmdline == NULL)
{ {
_dosmaperr(GetLastError()); _dosmaperr(GetLastError());
@ -158,7 +156,7 @@ int CDECL _wsystem(const wchar_t* cmd)
NULL, NULL,
&startup_info, &startup_info,
&process_info); &process_info);
LocalFree(cmdline); free(cmdline);
if (!result) if (!result)
{ {
@ -169,11 +167,10 @@ int CDECL _wsystem(const wchar_t* cmd)
CloseHandle(process_info.hThread); CloseHandle(process_info.hThread);
/* Wait for the process to exit */ /* Wait for the process to exit */
WaitForSingleObject(process_info.hProcess, INFINITE); _cwait(&status, (intptr_t)process_info.hProcess, 0);
GetExitCodeProcess(process_info.hProcess, &exit_code);
CloseHandle(process_info.hProcess); CloseHandle(process_info.hProcess);
_set_errno(0); _set_errno(0);
return (int)exit_code; return status;
} }