update for HEAD-2003091401
[reactos.git] / subsys / system / cmd / start.c
1 /*
2  *  START.C - start internal command.
3  *
4  *
5  *  History:
6  *
7  *    24-Jul-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
8  *        Started.
9  */
10
11 #include "config.h"
12
13 #ifdef INCLUDE_CMD_START
14 #include <windows.h>
15 #include <tchar.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19
20 #include "cmd.h"
21
22
23 INT cmd_start (LPTSTR first, LPTSTR rest)
24 {
25         TCHAR szFullName[MAX_PATH];
26         BOOL bWait = FALSE;
27         TCHAR *param;
28
29         if (_tcsncmp (rest, _T("/?"), 2) == 0)
30         {
31                 ConOutPuts (_T("Starts a command.\n\n"
32                                    "START command \n\n"
33                                    "  command     Specifies the command to run.\n\n"
34                                    "At the moment all commands are started asynchronously.\n"));
35
36                 return 0;
37         }
38
39         /* check for a drive change */
40         if (!_tcscmp (first + 1, _T(":")) && _istalpha (*first))
41         {
42                 TCHAR szPath[MAX_PATH];
43
44                 _tcscpy (szPath, _T("A:"));
45                 szPath[0] = _totupper (*first);
46                 SetCurrentDirectory (szPath);
47                 GetCurrentDirectory (MAX_PATH, szPath);
48                 if (szPath[0] != (TCHAR)_totupper (*first))
49                         ConErrPuts (INVALIDDRIVE);
50
51                 return 0;
52         }
53         if( !*rest )
54           {
55             // FIXME: use comspec instead
56             rest = _T("cmd");
57           }
58         /* get the PATH environment variable and parse it */
59         /* search the PATH environment variable for the binary */
60         param = _tcschr( rest, _T(' ') );  // skip program name to reach parameters
61         if( param )
62           {
63             *param = 0;
64             param++;
65           }
66         if (!SearchForExecutable (rest, szFullName))
67         {
68                 error_bad_command ();
69                 return 1;
70         }
71         /* check if this is a .BAT or .CMD file */
72         if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
73                 !_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".cmd")))
74         {
75 #ifdef _DEBUG
76                 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName, rest);
77 #endif
78                 ConErrPuts (_T("No batch support at the moment!"));
79         }
80         else
81         {
82                 /* exec the program */
83                 TCHAR szFullCmdLine [CMDLINE_LENGTH];
84                 PROCESS_INFORMATION prci;
85                 STARTUPINFO stui;
86
87 #ifdef _DEBUG
88                 DebugPrintf (_T("[EXEC: %s %s]\n"), szFullName, rest);
89 #endif
90                 /* build command line for CreateProcess() */
91                 _tcscpy (szFullCmdLine, first);
92                 if( param )
93                   {
94                     _tcscat(szFullCmdLine, _T(" ") );
95                     _tcscat (szFullCmdLine, param);
96                   }
97
98                 /* fill startup info */
99                 memset (&stui, 0, sizeof (STARTUPINFO));
100                 stui.cb = sizeof (STARTUPINFO);
101                 stui.dwFlags = STARTF_USESHOWWINDOW;
102                 stui.wShowWindow = SW_SHOWDEFAULT;
103                         
104                 if (CreateProcess (szFullName, szFullCmdLine, NULL, NULL, FALSE,
105                                                    CREATE_NEW_CONSOLE, NULL, NULL, &stui, &prci))
106                 {
107                         if (bWait)
108                         {
109                                 DWORD dwExitCode;
110                                 WaitForSingleObject (prci.hProcess, INFINITE);
111                                 GetExitCodeProcess (prci.hProcess, &dwExitCode);
112                                 nErrorLevel = (INT)dwExitCode;
113                         }
114                         CloseHandle (prci.hThread);
115                         CloseHandle (prci.hProcess);
116                 }
117                 else
118                 {
119                         ErrorMessage (GetLastError (),
120                                                   _T("Error executing CreateProcess()!!\n"));
121                 }
122         }
123
124         return 0;
125 }
126
127 #endif
128
129 /* EOF */