mirror of
https://github.com/reactos/reactos.git
synced 2025-07-31 12:02:02 +00:00
[MSVCRT][CRT_APITEST] Implement _wsystem (#5032)
Implement _wsystem(), by referring system(). Improve system(). Use WaitForSingleObject in system() and _wsystem(). Check existence of COMSPEC. Thanks ChatGPT.
This commit is contained in:
parent
72974d2bac
commit
f172503d57
6 changed files with 255 additions and 47 deletions
|
@ -4,6 +4,7 @@
|
|||
* FILE: lib/sdk/crt/process/_system.c
|
||||
* PURPOSE: Excutes a shell command
|
||||
* PROGRAMER: Ariadne
|
||||
* Katayama Hirofumi MZ
|
||||
* UPDATE HISTORY:
|
||||
* 04/03/99: Created
|
||||
*/
|
||||
|
@ -24,70 +25,48 @@ int system(const char *command)
|
|||
|
||||
PROCESS_INFORMATION ProcessInformation;
|
||||
STARTUPINFOA StartupInfo;
|
||||
char *s;
|
||||
BOOL result;
|
||||
|
||||
int nStatus;
|
||||
DWORD exit_code;
|
||||
char cmd_exe[MAX_PATH];
|
||||
|
||||
szComSpec = getenv("COMSPEC");
|
||||
|
||||
// system should return 0 if command is null and the shell is found
|
||||
|
||||
if (command == NULL) {
|
||||
if (szComSpec == NULL)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
return (szComSpec == NULL) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (szComSpec == NULL)
|
||||
return -1;
|
||||
|
||||
// should return 127 or 0 ( MS ) if the shell is not found
|
||||
// _set_errno(ENOENT);
|
||||
|
||||
if (szComSpec == NULL)
|
||||
if (!szComSpec || GetFileAttributesA(szComSpec) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
szComSpec = "cmd.exe";
|
||||
GetSystemDirectoryA(cmd_exe, _countof(cmd_exe));
|
||||
strcat(cmd_exe, "\\cmd.exe");
|
||||
szComSpec = cmd_exe;
|
||||
}
|
||||
|
||||
/* split the path from shell command */
|
||||
s = max(strrchr(szComSpec, '\\'), strrchr(szComSpec, '/'));
|
||||
if (s == NULL)
|
||||
s = szComSpec;
|
||||
else
|
||||
s++;
|
||||
|
||||
szCmdLine = malloc(strlen(s) + 4 + strlen(command) + 1);
|
||||
szCmdLine = LocalAlloc(LPTR, 1 + strlen(szComSpec) + 5 + strlen(command) + 1);
|
||||
if (szCmdLine == NULL)
|
||||
{
|
||||
_set_errno(ENOMEM);
|
||||
_dosmaperr(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(szCmdLine, s);
|
||||
s = strrchr(szCmdLine, '.');
|
||||
if (s)
|
||||
*s = 0;
|
||||
strcat(szCmdLine, " /C ");
|
||||
strcpy(szCmdLine, "\"");
|
||||
strcat(szCmdLine, szComSpec);
|
||||
strcat(szCmdLine, "\" /C ");
|
||||
strcat(szCmdLine, command);
|
||||
|
||||
//command file has invalid format ENOEXEC
|
||||
|
||||
memset (&StartupInfo, 0, sizeof(StartupInfo));
|
||||
memset(&StartupInfo, 0, sizeof(StartupInfo));
|
||||
StartupInfo.cb = sizeof(StartupInfo);
|
||||
StartupInfo.lpReserved= NULL;
|
||||
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
|
||||
StartupInfo.wShowWindow = SW_SHOWDEFAULT;
|
||||
StartupInfo.lpReserved2 = NULL;
|
||||
StartupInfo.cbReserved2 = 0;
|
||||
|
||||
// According to ansi standards the new process should ignore SIGINT and SIGQUIT
|
||||
// In order to disable ctr-c the process is created with CREATE_NEW_PROCESS_GROUP,
|
||||
// thus SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process.
|
||||
// In order to disable Ctrl+C, the process is created with CREATE_NEW_PROCESS_GROUP.
|
||||
// Thus, SetConsoleCtrlHandler(NULL, TRUE) is made on behalf of the new process.
|
||||
|
||||
|
||||
//SIGCHILD should be blocked aswell
|
||||
//SIGCHILD should be blocked as well
|
||||
|
||||
result = CreateProcessA(szComSpec,
|
||||
szCmdLine,
|
||||
|
@ -99,7 +78,7 @@ int system(const char *command)
|
|||
NULL,
|
||||
&StartupInfo,
|
||||
&ProcessInformation);
|
||||
free(szCmdLine);
|
||||
LocalFree(szCmdLine);
|
||||
|
||||
if (result == FALSE)
|
||||
{
|
||||
|
@ -109,15 +88,92 @@ int system(const char *command)
|
|||
|
||||
CloseHandle(ProcessInformation.hThread);
|
||||
|
||||
// system should wait untill the calling process is finished
|
||||
_cwait(&nStatus,(intptr_t)ProcessInformation.hProcess,0);
|
||||
/* Wait for the process to exit */
|
||||
WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
|
||||
GetExitCodeProcess(ProcessInformation.hProcess, &exit_code);
|
||||
|
||||
CloseHandle(ProcessInformation.hProcess);
|
||||
|
||||
return nStatus;
|
||||
_set_errno(0);
|
||||
return (int)exit_code;
|
||||
}
|
||||
|
||||
int CDECL _wsystem(const wchar_t* cmd)
|
||||
{
|
||||
FIXME("_wsystem stub\n");
|
||||
return -1;
|
||||
wchar_t *cmdline = NULL;
|
||||
wchar_t *comspec = NULL;
|
||||
PROCESS_INFORMATION process_info;
|
||||
STARTUPINFOW startup_info;
|
||||
BOOL result;
|
||||
DWORD exit_code;
|
||||
wchar_t cmd_exe[MAX_PATH];
|
||||
|
||||
comspec = _wgetenv(L"COMSPEC");
|
||||
|
||||
/* _wsystem should return 0 if cmd is null and the shell is found */
|
||||
if (cmd == NULL)
|
||||
{
|
||||
return (comspec == NULL) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (comspec == NULL || GetFileAttributesW(comspec) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
GetSystemDirectoryW(cmd_exe, _countof(cmd_exe));
|
||||
wcscat(cmd_exe, L"\\cmd.exe");
|
||||
comspec = cmd_exe;
|
||||
}
|
||||
|
||||
cmdline = LocalAlloc(LPTR, (1 + wcslen(comspec) + 5 + wcslen(cmd) + 1) * sizeof(wchar_t));
|
||||
if (cmdline == NULL)
|
||||
{
|
||||
_dosmaperr(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
wcscpy(cmdline, L"\"");
|
||||
wcscat(cmdline, comspec);
|
||||
wcscat(cmdline, L"\" /C ");
|
||||
wcscat(cmdline, cmd);
|
||||
|
||||
/* command file has invalid format ENOEXEC */
|
||||
|
||||
memset(&startup_info, 0, sizeof(startup_info));
|
||||
startup_info.cb = sizeof(startup_info);
|
||||
startup_info.dwFlags = STARTF_USESHOWWINDOW;
|
||||
startup_info.wShowWindow = SW_SHOWDEFAULT;
|
||||
|
||||
/* In order to disable Ctrl+C, the process is created with CREATE_NEW_PROCESS_GROUP.
|
||||
Thus, SetConsoleCtrlHandler(NULL, TRUE) is made on behalf of the new process. */
|
||||
|
||||
/* SIGCHILD should be blocked as well */
|
||||
|
||||
/* Create the process to execute the command */
|
||||
result = CreateProcessW(comspec,
|
||||
cmdline,
|
||||
NULL,
|
||||
NULL,
|
||||
TRUE,
|
||||
CREATE_NEW_PROCESS_GROUP,
|
||||
NULL,
|
||||
NULL,
|
||||
&startup_info,
|
||||
&process_info);
|
||||
LocalFree(cmdline);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
_dosmaperr(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
CloseHandle(process_info.hThread);
|
||||
|
||||
/* Wait for the process to exit */
|
||||
WaitForSingleObject(process_info.hProcess, INFINITE);
|
||||
GetExitCodeProcess(process_info.hProcess, &exit_code);
|
||||
|
||||
CloseHandle(process_info.hProcess);
|
||||
|
||||
_set_errno(0);
|
||||
return (int)exit_code;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue