5c21a4733bc69d973500f1d906bd1cb6b65d376a
[reactos.git] / reactos / 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 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
11 * Remove all hardcode string to En.rc
12 */
13
14 #include "precomp.h"
15 #include "resource.h"
16
17 #ifdef INCLUDE_CMD_START
18
19
20 INT cmd_start (LPTSTR first, LPTSTR rest)
21 {
22 TCHAR szFullName[MAX_PATH];
23 BOOL bWait = FALSE;
24 TCHAR *param;
25
26 if (_tcsncmp (rest, _T("/?"), 2) == 0)
27 {
28 ConOutResPuts(STRING_START_HELP1);
29 return 0;
30 }
31
32 /* check for a drive change */
33 if (!_tcscmp (first + 1, _T(":")) && _istalpha (*first))
34 {
35 TCHAR szPath[MAX_PATH];
36
37 _tcscpy (szPath, _T("A:"));
38 szPath[0] = _totupper (*first);
39 SetCurrentDirectory (szPath);
40 GetCurrentDirectory (MAX_PATH, szPath);
41 if (szPath[0] != (TCHAR)_totupper (*first))
42 ConErrPuts (INVALIDDRIVE);
43
44 return 0;
45 }
46
47 if( !*rest )
48 {
49 // FIXME: use comspec instead
50 rest = _T("cmd");
51 }
52
53 /* get the PATH environment variable and parse it */
54 /* search the PATH environment variable for the binary */
55 param = _tcschr( rest, _T(' ') ); // skip program name to reach parameters
56 if( param )
57 {
58 *param = 0;
59 param++;
60 }
61
62 if (!SearchForExecutable (rest, szFullName))
63 {
64 error_bad_command ();
65 return 1;
66 }
67
68 /* check if this is a .BAT or .CMD file */
69 if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
70 !_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".cmd")))
71 {
72 #ifdef _DEBUG
73 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName, rest);
74 #endif
75
76 ConErrResPuts(STRING_START_ERROR1);
77 }
78 else
79 {
80 /* exec the program */
81 TCHAR szFullCmdLine [CMDLINE_LENGTH];
82 PROCESS_INFORMATION prci;
83 STARTUPINFO stui;
84
85 #ifdef _DEBUG
86 DebugPrintf (_T("[EXEC: %s %s]\n"), szFullName, rest);
87 #endif
88 /* build command line for CreateProcess() */
89 _tcscpy (szFullCmdLine, first);
90 if( param )
91 {
92 _tcscat(szFullCmdLine, _T(" ") );
93 _tcscat (szFullCmdLine, param);
94 }
95
96 /* fill startup info */
97 memset (&stui, 0, sizeof (STARTUPINFO));
98 stui.cb = sizeof (STARTUPINFO);
99 stui.dwFlags = STARTF_USESHOWWINDOW;
100 stui.wShowWindow = SW_SHOWDEFAULT;
101
102 if (CreateProcess (szFullName, szFullCmdLine, NULL, NULL, FALSE,
103 CREATE_NEW_CONSOLE, NULL, NULL, &stui, &prci))
104 {
105 if (bWait)
106 {
107 DWORD dwExitCode;
108 WaitForSingleObject (prci.hProcess, INFINITE);
109 GetExitCodeProcess (prci.hProcess, &dwExitCode);
110 nErrorLevel = (INT)dwExitCode;
111 }
112 CloseHandle (prci.hThread);
113 CloseHandle (prci.hProcess);
114 /* Get New code page if it has change */
115 InputCodePage= GetConsoleCP();
116 OutputCodePage = GetConsoleOutputCP();
117 }
118 else
119 {
120 ErrorMessage(GetLastError (),
121 _T("Error executing CreateProcess()!!\n"));
122 }
123 }
124
125 return 0;
126 }
127
128 #endif
129
130 /* EOF */