1 /* $Id: _system.c,v 1.6 2002/11/25 17:41:39 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>
16 #include <msvcrt/internal/file.h>
18 int system(const char *command
)
20 char *szCmdLine
= NULL
;
21 char *szComSpec
= NULL
;
23 PROCESS_INFORMATION ProcessInformation
;
24 STARTUPINFO StartupInfo
;
30 szComSpec
= getenv("COMSPEC");
32 // system should return 0 if command is null and the shell is found
34 if (command
== NULL
) {
35 if (szComSpec
== NULL
)
44 // should return 127 or 0 ( MS ) if the shell is not found
45 // __set_errno(ENOENT);
47 if (szComSpec
== NULL
)
49 szComSpec
= strdup("cmd.exe");
50 if (szComSpec
== NULL
)
57 s
= max(strchr(szComSpec
, '\\'), strchr(szComSpec
, '/'));
63 szCmdLine
= malloc(strlen(s
) + 4 + strlen(command
) + 1);
64 if (szCmdLine
== NULL
)
72 s
= strrchr(szCmdLine
, '.');
75 strcat(szCmdLine
, " /C ");
76 strcat(szCmdLine
, command
);
78 //command file has invalid format ENOEXEC
80 memset (&StartupInfo
, 0, sizeof(STARTUPINFO
));
81 StartupInfo
.cb
= sizeof(STARTUPINFO
);
82 StartupInfo
.lpReserved
= NULL
;
83 StartupInfo
.dwFlags
= 0;
84 StartupInfo
.wShowWindow
= SW_SHOWDEFAULT
;
85 StartupInfo
.lpReserved2
= NULL
;
86 StartupInfo
.cbReserved2
= 0;
88 // According to ansi standards the new process should ignore SIGINT and SIGQUIT
89 // In order to disable ctr-c the process is created with CREATE_NEW_PROCESS_GROUP,
90 // thus SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process.
93 //SIGCHILD should be blocked aswell
95 result
= CreateProcessA(szComSpec
,
104 &ProcessInformation
);
110 __set_errno(ENOEXEC
);
114 CloseHandle(ProcessInformation
.hThread
);
116 // system should wait untill the calling process is finished
117 _cwait(&nStatus
,(int)ProcessInformation
.hProcess
,0);
118 CloseHandle(ProcessInformation
.hProcess
);