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