migrate substitution keywords to SVN
[reactos.git] / reactos / lib / crtdll / process / _system.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/crtdll/process/system.c
6 * PURPOSE: Excutes a shell command
7 * PROGRAMER: Boudewijn Dekker
8 * UPDATE HISTORY:
9 * 04/03/99: Created
10 */
11
12 #include "precomp.h"
13 #include <msvcrt/stdlib.h>
14 #include <msvcrt/string.h>
15 #include <msvcrt/process.h>
16
17 /*
18 * @implemented
19 */
20 int system(const char *command)
21 {
22 char szCmdLine[MAX_PATH];
23 char *szComSpec=NULL;
24
25
26 PROCESS_INFORMATION ProcessInformation;
27 STARTUPINFOA StartupInfo;
28
29 int nStatus;
30
31 szComSpec = getenv("COMSPEC");
32
33 // system should return 0 if command is null and the shell is found
34
35 if ( command == NULL ) {
36 if ( szComSpec == NULL )
37 return 0;
38 else
39 return -1;
40 }
41
42
43
44 // should return 127 or 0 ( MS ) if the shell is not found
45 // __set_errno(ENOENT);
46
47 if ( szComSpec == NULL )
48 szComSpec = "cmd.exe";
49
50
51
52 strcpy(szCmdLine," /C ");
53
54 strncat(szCmdLine,command,MAX_PATH-5);
55
56 //check for a too long argument E2BIG
57
58 //command file has invalid format ENOEXEC
59
60
61 StartupInfo.cb = sizeof(StartupInfo);
62 StartupInfo.lpReserved= NULL;
63 StartupInfo.dwFlags = 0;
64 StartupInfo.wShowWindow = SW_SHOWDEFAULT;
65 StartupInfo.lpReserved2 = NULL;
66 StartupInfo.cbReserved2 = 0;
67
68 // According to ansi standards the new process should ignore SIGINT and SIGQUIT
69 // In order to disable ctr-c the process is created with CREATE_NEW_PROCESS_GROUP,
70 // thus SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process.
71
72
73 //SIGCHILD should be blocked aswell
74
75 if ( CreateProcessA(szComSpec,szCmdLine,NULL,NULL,TRUE,CREATE_NEW_PROCESS_GROUP,NULL,NULL,&StartupInfo,&ProcessInformation) == FALSE) {
76 return -1;
77 }
78
79
80 // system should wait untill the calling process is finished
81
82 _cwait(&nStatus,(int)ProcessInformation.hProcess,0);
83
84 // free the comspec [ if the COMSPEC == NULL provision is removed
85 // free(szComSpec);
86
87 return nStatus;
88 }