cleaned up some warnings.
[reactos.git] / reactos / lib / msvcrt / process / _system.c
1 /* $Id: _system.c,v 1.6 2002/11/25 17:41:39 robd Exp $
2 *
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
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 #include <msvcrt/errno.h>
16 #include <msvcrt/internal/file.h>
17
18 int system(const char *command)
19 {
20 char *szCmdLine = NULL;
21 char *szComSpec = NULL;
22
23 PROCESS_INFORMATION ProcessInformation;
24 STARTUPINFO StartupInfo;
25 char *s;
26 BOOL result;
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 {
39 free(szComSpec);
40 return -1;
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 {
49 szComSpec = strdup("cmd.exe");
50 if (szComSpec == NULL)
51 {
52 __set_errno(ENOMEM);
53 return -1;
54 }
55 }
56
57 s = max(strchr(szComSpec, '\\'), strchr(szComSpec, '/'));
58 if (s == NULL)
59 s = szComSpec;
60 else
61 s++;
62
63 szCmdLine = malloc(strlen(s) + 4 + strlen(command) + 1);
64 if (szCmdLine == NULL)
65 {
66 free (szComSpec);
67 __set_errno(ENOMEM);
68 return -1;
69 }
70
71 strcpy(szCmdLine, s);
72 s = strrchr(szCmdLine, '.');
73 if (s)
74 *s = 0;
75 strcat(szCmdLine, " /C ");
76 strcat(szCmdLine, command);
77
78 //command file has invalid format ENOEXEC
79
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;
87
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.
91
92
93 //SIGCHILD should be blocked aswell
94
95 result = CreateProcessA(szComSpec,
96 szCmdLine,
97 NULL,
98 NULL,
99 TRUE,
100 0,
101 NULL,
102 NULL,
103 &StartupInfo,
104 &ProcessInformation);
105 free(szCmdLine);
106 free(szComSpec);
107
108 if (result == FALSE)
109 {
110 __set_errno(ENOEXEC);
111 return -1;
112 }
113
114 CloseHandle(ProcessInformation.hThread);
115
116 // system should wait untill the calling process is finished
117 _cwait(&nStatus,(int)ProcessInformation.hProcess,0);
118 CloseHandle(ProcessInformation.hProcess);
119
120 return nStatus;
121 }