Added MORE command.
[reactos.git] / rosapps / 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
28 if (_tcsncmp (rest, _T("/?"), 2) == 0)
29 {
30 ConOutPuts (_T("Starts a command.\n\n"
31 "START command \n\n"
32 " command Specifies the command to run.\n\n"
33 "At the moment all commands are started asynchronously.\n"));
34
35 return 0;
36 }
37
38 /* check for a drive change */
39 if (!_tcscmp (first + 1, _T(":")) && _istalpha (*first))
40 {
41 TCHAR szPath[MAX_PATH];
42
43 _tcscpy (szPath, _T("A:"));
44 szPath[0] = _totupper (*first);
45 SetCurrentDirectory (szPath);
46 GetCurrentDirectory (MAX_PATH, szPath);
47 if (szPath[0] != (TCHAR)_totupper (*first))
48 ConErrPuts (INVALIDDRIVE);
49
50 return 0;
51 }
52
53 /* get the PATH environment variable and parse it */
54 /* search the PATH environment variable for the binary */
55 if (!SearchForExecutable (first, szFullName))
56 {
57 error_bad_command ();
58 return 1;
59 }
60
61 /* check if this is a .BAT or .CMD file */
62 if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
63 !_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".cmd")))
64 {
65 #ifdef _DEBUG
66 DebugPrintf ("[BATCH: %s %s]\n", szFullName, rest);
67 #endif
68 ConErrPuts (_T("No batch support at the moment!"));
69 }
70 else
71 {
72 /* exec the program */
73 TCHAR szFullCmdLine [1024];
74 PROCESS_INFORMATION prci;
75 STARTUPINFO stui;
76
77 #ifdef _DEBUG
78 DebugPrintf ("[EXEC: %s %s]\n", szFullName, rest);
79 #endif
80 /* build command line for CreateProcess() */
81 _tcscpy (szFullCmdLine, szFullName);
82 _tcscat (szFullCmdLine, _T(" "));
83 _tcscat (szFullCmdLine, rest);
84
85 /* fill startup info */
86 memset (&stui, 0, sizeof (STARTUPINFO));
87 stui.cb = sizeof (STARTUPINFO);
88 stui.dwFlags = STARTF_USESHOWWINDOW;
89 stui.wShowWindow = SW_SHOWDEFAULT;
90
91 if (CreateProcess (NULL, szFullCmdLine, NULL, NULL, FALSE,
92 0, NULL, NULL, &stui, &prci))
93 {
94 if (bWait)
95 {
96 DWORD dwExitCode;
97 WaitForSingleObject (prci.hProcess, INFINITE);
98 GetExitCodeProcess (prci.hProcess, &dwExitCode);
99 nErrorLevel = (INT)dwExitCode;
100 CloseHandle (prci.hThread);
101 CloseHandle (prci.hProcess);
102 }
103 }
104 else
105 {
106 ErrorMessage (GetLastError (),
107 "Error executing CreateProcess()!!\n");
108 }
109 }
110
111 return 0;
112 }
113
114 #endif
115
116 /* EOF */