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