3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/msvcrt/process/system.c
6 * PURPOSE: Excutes a shell command
7 * PROGRAMER: Boudewijn Dekker
13 #include <msvcrt/stdlib.h>
14 #include <msvcrt/string.h>
15 #include <msvcrt/process.h>
16 #include <msvcrt/errno.h>
17 #include <msvcrt/internal/file.h>
22 int system(const char *command
)
24 char *szCmdLine
= NULL
;
25 char *szComSpec
= NULL
;
27 PROCESS_INFORMATION ProcessInformation
;
28 STARTUPINFOA StartupInfo
;
34 szComSpec
= getenv("COMSPEC");
36 // system should return 0 if command is null and the shell is found
38 if (command
== NULL
) {
39 if (szComSpec
== NULL
)
45 // should return 127 or 0 ( MS ) if the shell is not found
46 // __set_errno(ENOENT);
48 if (szComSpec
== NULL
)
50 szComSpec
= strdup("cmd.exe");
51 if (szComSpec
== NULL
)
58 s
= max(strchr(szComSpec
, '\\'), strchr(szComSpec
, '/'));
64 szCmdLine
= malloc(strlen(s
) + 4 + strlen(command
) + 1);
65 if (szCmdLine
== NULL
)
73 s
= strrchr(szCmdLine
, '.');
76 strcat(szCmdLine
, " /C ");
77 strcat(szCmdLine
, command
);
79 //command file has invalid format ENOEXEC
81 memset (&StartupInfo
, 0, sizeof(StartupInfo
));
82 StartupInfo
.cb
= sizeof(StartupInfo
);
83 StartupInfo
.lpReserved
= NULL
;
84 StartupInfo
.dwFlags
= 0;
85 StartupInfo
.wShowWindow
= SW_SHOWDEFAULT
;
86 StartupInfo
.lpReserved2
= NULL
;
87 StartupInfo
.cbReserved2
= 0;
89 // According to ansi standards the new process should ignore SIGINT and SIGQUIT
90 // In order to disable ctr-c the process is created with CREATE_NEW_PROCESS_GROUP,
91 // thus SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process.
94 //SIGCHILD should be blocked aswell
96 result
= CreateProcessA(szComSpec
,
105 &ProcessInformation
);
111 _dosmaperr(GetLastError());
115 CloseHandle(ProcessInformation
.hThread
);
117 // system should wait untill the calling process is finished
118 _cwait(&nStatus
,(int)ProcessInformation
.hProcess
,0);
119 CloseHandle(ProcessInformation
.hProcess
);