1007f3a70d71fe63f030069a62ea933531e32f1b
[reactos.git] / rosapps / cmd / internal.c
1 /*
2 * INTERNAL.C - command.com internal commands.
3 *
4 *
5 * History:
6 *
7 * 17/08/94 (Tim Norman)
8 * started.
9 *
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source into
12 * guidelines for recommended programming practice.
13 *
14 * cd()
15 * started.
16 *
17 * dir()
18 * i have added support for file attributes to the DIR() function. the
19 * routine adds "d" (directory) and "r" (read only) output. files with the
20 * system attribute have the filename converted to lowercase. files with
21 * the hidden attribute are not displayed.
22 *
23 * i have added support for directorys. now if the directory attribute is
24 * detected the file size if replaced with the string "<dir>".
25 *
26 * ver()
27 * started.
28 *
29 * md()
30 * started.
31 *
32 * rd()
33 * started.
34 *
35 * del()
36 * started.
37 *
38 * does not support wildcard selection.
39 *
40 * todo: add delete directory support.
41 * add recursive directory delete support.
42 *
43 * ren()
44 * started.
45 *
46 * does not support wildcard selection.
47 *
48 * todo: add rename directory support.
49 *
50 * a general structure has been used for the cd, rd and md commands. this
51 * will be better in the long run. it is too hard to maintain such diverse
52 * functions when you are involved in a group project like this.
53 *
54 * 12/14/95 (Tim Norman)
55 * fixed DIR so that it will stick \*.* if a directory is specified and
56 * that it will stick on .* if a file with no extension is specified or
57 * *.* if it ends in a \
58 *
59 * 1/6/96 (Tim Norman)
60 * added an isatty call to DIR so it won't prompt for keypresses unless
61 * stdin and stdout are the console.
62 *
63 * changed parameters to be mutually consistent to make calling the
64 * functions easier
65 *
66 * rem()
67 * started.
68 *
69 * doskey()
70 * started.
71 *
72 * 01/22/96 (Oliver Mueller)
73 * error messages are now handled by perror.
74 *
75 * 02/05/96 (Tim Norman)
76 * converted all functions to accept first/rest parameters
77 *
78 * 07/26/96 (Tim Norman)
79 * changed return values to int instead of void
80 *
81 * path() started.
82 *
83 * 12/23/96 (Aaron Kaufman)
84 * rewrote dir() to mimic MS-DOS's dir
85 *
86 * 01/28/97 (Tim Norman)
87 * cleaned up Aaron's DIR code
88 *
89 * 06/13/97 (Tim Norman)
90 * moved DIR code to dir.c
91 * re-implemented Aaron's DIR code
92 *
93 * 06/14/97 (Steffan Kaiser)
94 * ctrl-break handling
95 * bug fixes
96 *
97 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
98 * added config.h include
99 *
100 * 03-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
101 * Replaced DOS calls by Win32 calls.
102 *
103 * 08-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
104 * Added help texts ("/?").
105 *
106 * 18-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
107 * Added support for quoted arguments (cd "program files").
108 *
109 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
110 * Clean up.
111 *
112 * 26-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
113 * Replaced remaining CRT io functions by Win32 io functions.
114 * Unicode safe!
115 *
116 * 30-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
117 * Added "cd -" feature. Changes to the previous directory.
118 *
119 * 15-Mar-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
120 * Fixed bug in "cd -" feature. If the previous directory was a root
121 * directory, it was ignored.
122 *
123 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
124 * Improved chdir/cd command.
125 */
126
127 #include "config.h"
128
129 #include <windows.h>
130 #include <tchar.h>
131 #include <string.h>
132 #include <stdlib.h>
133 #include <ctype.h>
134
135 #include "cmd.h"
136
137
138 #ifdef INCLUDE_CMD_CHDIR
139
140 static LPTSTR lpLastPath;
141
142
143 VOID InitLastPath (VOID)
144 {
145 lpLastPath = NULL;
146 }
147
148
149 VOID FreeLastPath (VOID)
150 {
151 if (lpLastPath)
152 free (lpLastPath);
153 }
154
155
156 /*
157 * CD / CHDIR
158 *
159 */
160 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
161 {
162 LPTSTR dir; /* pointer to the directory to change to */
163 LPTSTR place; /* used to search for the \ when no space is used */
164 LPTSTR lpOldPath;
165 LPTSTR *arg = NULL;
166 INT argc;
167
168 if (!_tcsncmp (param, _T("/?"), 2))
169 {
170 ConOutPuts (_T("Changes the current directory or displays it's name\n\n"
171 "CHDIR [drive:][path]\n"
172 "CHDIR[..|-]\n"
173 "CD [drive:][path]\n"
174 "CD[..|-]\n\n"
175 " .. parent directory\n"
176 " - previous directory\n\n"
177 "Type CD drive: to display the current directory on the specified drive.\n"
178 "Type CD without a parameter to display the current drive and directory."));
179 return 0;
180 }
181
182 /* check if there is no space between the command and the path */
183 if (param[0] == _T('\0'))
184 {
185 /* search for the '\', '.' or '-' so that both short & long names will work */
186 for (place = cmd; *place; place++)
187 if (*place == _T('.') || *place == _T('\\') || *place == _T('-'))
188 break;
189
190 if (*place)
191 dir = place;
192 else
193 /* signal that there are no parameters */
194 dir = NULL;
195 }
196 else
197 {
198 arg = split (param, &argc);
199
200 if (argc > 1)
201 {
202 /*JPP 20-Jul-1998 use standard error message */
203 error_too_many_parameters (param);
204 freep (arg);
205 return 1;
206 }
207 else
208 dir = arg[0];
209 }
210
211 /* if doing a CD and no parameters given, print out current directory */
212 if (!dir || !dir[0])
213 {
214 TCHAR szPath[MAX_PATH];
215
216 GetCurrentDirectory (MAX_PATH, szPath);
217
218 ConOutPuts (szPath);
219
220 freep (arg);
221
222 return 0;
223 }
224
225 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
226 {
227 if (lpLastPath)
228 dir = lpLastPath;
229 else
230 {
231 freep (arg);
232 return 0;
233 }
234 }
235 else if (dir && _tcslen (dir) > 1 && dir[1] == _T(':'))
236 {
237 TCHAR szRoot[3] = _T("A:");
238 TCHAR szPath[MAX_PATH];
239
240 szRoot[0] = _totupper (dir[0]);
241 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
242
243 /* PathRemoveBackslash */
244 if (_tcslen (szPath) > 3)
245 {
246 LPTSTR p = _tcsrchr (szPath, _T('\\'));
247 *p = _T('\0');
248 }
249
250 ConOutPuts (szPath);
251
252 freep (arg);
253
254 return 0;
255 }
256
257 /* remove trailing \ if any, but ONLY if dir is not the root dir */
258 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
259 dir[_tcslen(dir) - 1] = _T('\0');
260
261
262 /* store current directory */
263 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
264 GetCurrentDirectory (MAX_PATH, lpOldPath);
265
266 if (!SetCurrentDirectory (dir))
267 {
268 ErrorMessage (GetLastError(), _T("CD"));
269
270 /* throw away current directory */
271 free (lpOldPath);
272 lpOldPath = NULL;
273
274 freep (arg);
275 return 1;
276 }
277 else
278 {
279 if (lpLastPath)
280 free (lpLastPath);
281 lpLastPath = lpOldPath;
282 }
283
284 freep (arg);
285
286 return 0;
287 }
288 #endif
289
290
291
292 #ifdef INCLUDE_CMD_MKDIR
293 /*
294 * MD / MKDIR
295 *
296 */
297 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
298 {
299 LPTSTR dir; /* pointer to the directory to change to */
300 LPTSTR place; /* used to search for the \ when no space is used */
301 LPTSTR *p = NULL;
302 INT argc;
303
304
305 if (!_tcsncmp (param, _T("/?"), 2))
306 {
307 ConOutPuts (_T("Creates a directory.\n\n"
308 "MKDIR [drive:]path\nMD [drive:]path"));
309 return 0;
310 }
311
312
313 /* check if there is no space between the command and the path */
314 if (param[0] == _T('\0'))
315 {
316 /* search for the \ or . so that both short & long names will work */
317 for (place = cmd; *place; place++)
318 if (*place == _T('.') || *place == _T('\\'))
319 break;
320
321 if (*place)
322 dir = place;
323 else
324 /* signal that there are no parameters */
325 dir = NULL;
326 }
327 else
328 {
329 p = split (param, &argc);
330 if (argc > 1)
331 {
332 /*JPP 20-Jul-1998 use standard error message */
333 error_too_many_parameters (param);
334 freep (p);
335 return 1;
336 }
337 else
338 dir = p[0];
339 }
340
341 if (!dir)
342 {
343 ConErrPrintf (_T("Required parameter missing\n"));
344 return 1;
345 }
346
347 /* remove trailing \ if any, but ONLY if dir is not the root dir */
348 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
349 dir[_tcslen(dir) - 1] = _T('\0');
350
351 if (!CreateDirectory (dir, NULL))
352 {
353 ErrorMessage (GetLastError(), _T("MD"));
354
355 freep (p);
356 return 1;
357 }
358
359 freep (p);
360
361 return 0;
362 }
363 #endif
364
365
366 #ifdef INCLUDE_CMD_RMDIR
367 /*
368 * RD / RMDIR
369 *
370 */
371 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
372 {
373 LPTSTR dir; /* pointer to the directory to change to */
374 LPTSTR place; /* used to search for the \ when no space is used */
375
376 LPTSTR *p = NULL;
377 INT argc;
378
379 if (!_tcsncmp (param, _T("/?"), 2))
380 {
381 ConOutPuts (_T("Removes a directory.\n\n"
382 "RMDIR [drive:]path\nRD [drive:]path"));
383 return 0;
384 }
385
386 /* check if there is no space between the command and the path */
387 if (param[0] == _T('\0'))
388 {
389 /* search for the \ or . so that both short & long names will work */
390 for (place = cmd; *place; place++)
391 if (*place == _T('.') || *place == _T('\\'))
392 break;
393
394 if (*place)
395 dir = place;
396 else
397 /* signal that there are no parameters */
398 dir = NULL;
399 }
400 else
401 {
402 p = split (param, &argc);
403 if (argc > 1)
404 {
405 /*JPP 20-Jul-1998 use standard error message */
406 error_too_many_parameters (param);
407 freep (p);
408 return 1;
409 }
410 else
411 dir = p[0];
412 }
413
414 if (!dir)
415 {
416 ConErrPrintf (_T("Required parameter missing\n"));
417 return 1;
418 }
419
420 /* remove trailing \ if any, but ONLY if dir is not the root dir */
421 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
422 dir[_tcslen(dir) - 1] = _T('\0');
423
424 if (!RemoveDirectory (dir))
425 {
426 ErrorMessage (GetLastError(), _T("RD"));
427 freep (p);
428
429 return 1;
430 }
431
432 freep (p);
433
434 return 0;
435 }
436 #endif
437
438
439 /*
440 * set the exitflag to true
441 *
442 */
443 INT CommandExit (LPTSTR cmd, LPTSTR param)
444 {
445 if (!_tcsncmp (param, _T("/?"), 2))
446 {
447 ConOutPuts (_T("Exits the command line interpreter.\n\nEXIT"));
448 return 0;
449 }
450
451 bExit = TRUE;
452 return 0;
453 }
454
455
456 #ifdef INCLUDE_CMD_REM
457 /*
458 * does nothing
459 *
460 */
461 INT CommandRem (LPTSTR cmd, LPTSTR param)
462 {
463 if (!_tcsncmp (param, _T("/?"), 2))
464 {
465 ConOutPuts (_T("Starts a comment line in a batch file.\n\n"
466 "REM [Comment]"));
467 }
468
469 return 0;
470 }
471 #endif /* INCLUDE_CMD_REM */
472
473
474 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
475 {
476 PrintCommandList ();
477 return 0;
478 }
479
480 /* EOF */