f7ea056fd46ae5263ce33130a10be8056d71c41d
[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
124 #include "config.h"
125
126 #include <windows.h>
127 #include <tchar.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <ctype.h>
131
132 #include "cmd.h"
133
134
135 #ifdef INCLUDE_CMD_CHDIR
136
137 static LPTSTR lpLastPath;
138
139
140 VOID InitLastPath (VOID)
141 {
142 lpLastPath = NULL;
143 }
144
145
146 VOID FreeLastPath (VOID)
147 {
148 if (lpLastPath)
149 free (lpLastPath);
150 }
151
152
153 /*
154 * CD / CHDIR
155 *
156 */
157 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
158 {
159 LPTSTR dir; /* pointer to the directory to change to */
160 LPTSTR place; /* used to search for the \ when no space is used */
161 LPTSTR lpOldPath;
162 LPTSTR *arg = NULL;
163 INT argc;
164
165 if (!_tcsncmp (param, _T("/?"), 2))
166 {
167 ConOutPuts (_T("Changes the current directory or displays it's name\n\n"
168 "CHDIR [drive:][path]\n"
169 "CHDIR[..|-]\n"
170 "CD [drive:][path]\n"
171 "CD[..|-]\n\n"
172 " .. parent directory\n"
173 " - previous directory\n\n"
174 "Type CD drive: to display the current directory on the specified drive.\n"
175 "Type CD without a parameter to display the current drive and directory."));
176 return 0;
177 }
178
179 /* check if there is no space between the command and the path */
180 if (param[0] == _T('\0'))
181 {
182 /* search for the '\', '.' or '-' so that both short & long names will work */
183 for (place = cmd; *place; place++)
184 if (*place == _T('.') || *place == _T('\\') || *place == _T('-'))
185 break;
186
187 if (*place)
188 dir = place;
189 else
190 /* signal that there are no parameters */
191 dir = NULL;
192 }
193 else
194 {
195 arg = split (param, &argc);
196
197 if (argc > 1)
198 {
199 /*JPP 20-Jul-1998 use standard error message */
200 error_too_many_parameters (param);
201 freep (arg);
202 return 1;
203 }
204 else
205 dir = arg[0];
206 }
207
208 /* if doing a CD and no parameters given, print out current directory */
209 if (!dir || !dir[0])
210 {
211 TCHAR szPath[MAX_PATH];
212
213 GetCurrentDirectory (MAX_PATH, szPath);
214
215 ConOutPuts (szPath);
216
217 freep (arg);
218
219 return 0;
220 }
221
222 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
223 {
224 if (lpLastPath)
225 dir = lpLastPath;
226 else
227 {
228 freep (arg);
229 return 0;
230 }
231 }
232 else if (dir && _tcslen (dir) > 1 && dir[1] == _T(':'))
233 {
234 TCHAR szRoot[3] = _T("A:");
235 TCHAR szPath[MAX_PATH];
236
237 szRoot[0] = _totupper (dir[0]);
238 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
239
240 /* PathRemoveBackslash */
241 if (_tcslen (szPath) > 3)
242 {
243 LPTSTR p = _tcsrchr (szPath, _T('\\'));
244 *p = _T('\0');
245 }
246
247 ConOutPuts (szPath);
248
249 freep (arg);
250
251 return 0;
252 }
253
254 /* remove trailing \ if any, but ONLY if dir is not the root dir */
255 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
256 dir[_tcslen(dir) - 1] = _T('\0');
257
258
259 /* store current directory */
260 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
261 GetCurrentDirectory (MAX_PATH, lpOldPath);
262
263 if (!SetCurrentDirectory (dir))
264 {
265 ErrorMessage (GetLastError(), _T("CD"));
266
267 /* throw away current directory */
268 free (lpOldPath);
269 lpOldPath = NULL;
270
271 freep (arg);
272 return 1;
273 }
274 else
275 {
276 if (lpLastPath)
277 free (lpLastPath);
278 lpLastPath = lpOldPath;
279 }
280
281 freep (arg);
282
283 return 0;
284 }
285 #endif
286
287
288
289 #ifdef INCLUDE_CMD_MKDIR
290 /*
291 * MD / MKDIR
292 *
293 */
294 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
295 {
296 LPTSTR dir; /* pointer to the directory to change to */
297 LPTSTR place; /* used to search for the \ when no space is used */
298 LPTSTR *p = NULL;
299 INT argc;
300
301
302 if (!_tcsncmp (param, _T("/?"), 2))
303 {
304 ConOutPuts (_T("Creates a directory.\n\n"
305 "MKDIR [drive:]path\nMD [drive:]path"));
306 return 0;
307 }
308
309
310 /* check if there is no space between the command and the path */
311 if (param[0] == _T('\0'))
312 {
313 /* search for the \ or . so that both short & long names will work */
314 for (place = cmd; *place; place++)
315 if (*place == _T('.') || *place == _T('\\'))
316 break;
317
318 if (*place)
319 dir = place;
320 else
321 /* signal that there are no parameters */
322 dir = NULL;
323 }
324 else
325 {
326 p = split (param, &argc);
327 if (argc > 1)
328 {
329 /*JPP 20-Jul-1998 use standard error message */
330 error_too_many_parameters (param);
331 freep (p);
332 return 1;
333 }
334 else
335 dir = p[0];
336 }
337
338 if (!dir)
339 {
340 ConErrPrintf (_T("Required parameter missing\n"));
341 return 1;
342 }
343
344 /* remove trailing \ if any, but ONLY if dir is not the root dir */
345 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
346 dir[_tcslen(dir) - 1] = _T('\0');
347
348 if (!CreateDirectory (dir, NULL))
349 {
350 ErrorMessage (GetLastError(), _T("MD"));
351
352 freep (p);
353 return 1;
354 }
355
356 freep (p);
357
358 return 0;
359 }
360 #endif
361
362
363 #ifdef INCLUDE_CMD_RMDIR
364 /*
365 * RD / RMDIR
366 *
367 */
368 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
369 {
370 LPTSTR dir; /* pointer to the directory to change to */
371 LPTSTR place; /* used to search for the \ when no space is used */
372
373 LPTSTR *p = NULL;
374 INT argc;
375
376 if (!_tcsncmp (param, _T("/?"), 2))
377 {
378 ConOutPuts (_T("Removes a directory.\n\n"
379 "RMDIR [drive:]path\nRD [drive:]path"));
380 return 0;
381 }
382
383 /* check if there is no space between the command and the path */
384 if (param[0] == _T('\0'))
385 {
386 /* search for the \ or . so that both short & long names will work */
387 for (place = cmd; *place; place++)
388 if (*place == _T('.') || *place == _T('\\'))
389 break;
390
391 if (*place)
392 dir = place;
393 else
394 /* signal that there are no parameters */
395 dir = NULL;
396 }
397 else
398 {
399 p = split (param, &argc);
400 if (argc > 1)
401 {
402 /*JPP 20-Jul-1998 use standard error message */
403 error_too_many_parameters (param);
404 freep (p);
405 return 1;
406 }
407 else
408 dir = p[0];
409 }
410
411 if (!dir)
412 {
413 ConErrPrintf (_T("Required parameter missing\n"));
414 return 1;
415 }
416
417 /* remove trailing \ if any, but ONLY if dir is not the root dir */
418 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
419 dir[_tcslen(dir) - 1] = _T('\0');
420
421 if (!RemoveDirectory (dir))
422 {
423 ErrorMessage (GetLastError(), _T("RD"));
424 freep (p);
425
426 return 1;
427 }
428
429 freep (p);
430
431 return 0;
432 }
433 #endif
434
435
436 /*
437 * set the exitflag to true
438 *
439 */
440 INT CommandExit (LPTSTR cmd, LPTSTR param)
441 {
442 if (!_tcsncmp (param, _T("/?"), 2))
443 {
444 ConOutPuts (_T("Exits the command line interpreter.\n\nEXIT"));
445 return 0;
446 }
447
448 bExit = TRUE;
449 return 0;
450 }
451
452
453 #ifdef INCLUDE_CMD_REM
454 /*
455 * does nothing
456 *
457 */
458 INT CommandRem (LPTSTR cmd, LPTSTR param)
459 {
460 if (!_tcsncmp (param, _T("/?"), 2))
461 {
462 ConOutPuts (_T("Starts a comment line in a batch file.\n\n"
463 "REM [Comment]"));
464 }
465
466 return 0;
467 }
468 #endif /* INCLUDE_CMD_REM */
469
470
471 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
472 {
473 PrintCommandList ();
474 return 0;
475 }
476
477 /* EOF */