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