31839a19a4d0f8c94b715dcf92842b97bfaf1a21
[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 ConOutResPaging(TRUE,STRING_START_HELP1);
29 return 0;
30 }
31
32 nErrorLevel = 0;
33
34 /* check for a drive change */
35 if (!_tcscmp (first + 1, _T(":")) && _istalpha (*first))
36 {
37 TCHAR szPath[MAX_PATH];
38
39 _tcscpy (szPath, _T("A:"));
40 szPath[0] = _totupper (*first);
41 SetCurrentDirectory (szPath);
42 GetCurrentDirectory (MAX_PATH, szPath);
43 if (szPath[0] != (TCHAR)_totupper (*first))
44 ConErrResPuts (STRING_FREE_ERROR1);
45
46 return 0;
47 }
48
49 if( !*rest )
50 {
51 // FIXME: use comspec instead
52 rest = _T("cmd");
53 }
54
55 /* get the PATH environment variable and parse it */
56 /* search the PATH environment variable for the binary */
57 param = _tcschr( rest, _T(' ') ); // skip program name to reach parameters
58 if( param )
59 {
60 *param = 0;
61 param++;
62 }
63
64 if (!SearchForExecutable (rest, szFullName))
65 {
66 error_bad_command ();
67 return 1;
68 }
69
70 /* check if this is a .BAT or .CMD file */
71 if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
72 !_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".cmd")))
73 {
74 #ifdef _DEBUG
75 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName, rest);
76 #endif
77
78 ConErrResPuts(STRING_START_ERROR1);
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 /* Get New code page if it has change */
117 InputCodePage= GetConsoleCP();
118 OutputCodePage = GetConsoleOutputCP();
119 }
120 else
121 {
122 ErrorMessage(GetLastError (),
123 _T("Error executing CreateProcess()!!\n"));
124 }
125 }
126
127 return 0;
128 }
129
130 #endif
131
132 /* EOF */