1 /* $Id: _system.c,v 1.5 2002/11/24 18:42:23 robd Exp $
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
12 #include <msvcrt/stdlib.h>
13 #include <msvcrt/string.h>
14 #include <msvcrt/process.h>
15 #include <msvcrt/errno.h>
17 int system(const char *command
)
19 char *szCmdLine
= NULL
;
20 char *szComSpec
= NULL
;
22 PROCESS_INFORMATION ProcessInformation
;
23 STARTUPINFO StartupInfo
;
29 szComSpec
= getenv("COMSPEC");
31 // system should return 0 if command is null and the shell is found
33 if (command
== NULL
) {
34 if (szComSpec
== NULL
)
43 // should return 127 or 0 ( MS ) if the shell is not found
44 // __set_errno(ENOENT);
46 if (szComSpec
== NULL
)
48 szComSpec
= strdup("cmd.exe");
49 if (szComSpec
== NULL
)
56 s
= max(strchr(szComSpec
, '\\'), strchr(szComSpec
, '/'));
62 szCmdLine
= malloc(strlen(s
) + 4 + strlen(command
) + 1);
63 if (szCmdLine
== NULL
)
71 s
= strrchr(szCmdLine
, '.');
74 strcat(szCmdLine
, " /C ");
75 strcat(szCmdLine
, command
);
77 //command file has invalid format ENOEXEC
79 memset (&StartupInfo
, 0, sizeof(STARTUPINFO
));
80 StartupInfo
.cb
= sizeof(STARTUPINFO
);
81 StartupInfo
.lpReserved
= NULL
;
82 StartupInfo
.dwFlags
= 0;
83 StartupInfo
.wShowWindow
= SW_SHOWDEFAULT
;
84 StartupInfo
.lpReserved2
= NULL
;
85 StartupInfo
.cbReserved2
= 0;
87 // According to ansi standards the new process should ignore SIGINT and SIGQUIT
88 // In order to disable ctr-c the process is created with CREATE_NEW_PROCESS_GROUP,
89 // thus SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process.
92 //SIGCHILD should be blocked aswell
94 result
= CreateProcessA(szComSpec
,
103 &ProcessInformation
);
109 __set_errno(ENOEXEC
);
113 CloseHandle(ProcessInformation
.hThread
);
115 // system should wait untill the calling process is finished
116 _cwait(&nStatus
,(int)ProcessInformation
.hProcess
,0);
117 CloseHandle(ProcessInformation
.hProcess
);