Added Eric Kohl's port of freedos command
[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
120 #define WIN32_LEAN_AND_MEAN
121
122 #include "config.h"
123
124 #include <windows.h>
125 #include <tchar.h>
126 #include <string.h>
127 #include <stdlib.h>
128
129 #include "cmd.h"
130
131
132 extern COMMAND cmds[]; /* The internal command table, used in '?' */
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) >= 2 && 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 /* remove trailing \ if any, but ONLY if dir is not the root dir */
339 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
340 dir[_tcslen(dir) - 1] = _T('\0');
341
342 if (!CreateDirectory (dir, NULL))
343 {
344 ErrorMessage (GetLastError(), _T("MD"));
345
346 freep (p);
347 return 1;
348 }
349
350 freep (p);
351
352 return 0;
353 }
354 #endif
355
356
357 #ifdef INCLUDE_CMD_RMDIR
358 /*
359 * RD / RMDIR
360 *
361 */
362 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
363 {
364 LPTSTR dir; /* pointer to the directory to change to */
365 LPTSTR place; /* used to search for the \ when no space is used */
366
367 LPTSTR *p = NULL;
368 INT argc;
369
370 if (!_tcsncmp (param, _T("/?"), 2))
371 {
372 ConOutPuts (_T("Removes a directory.\n\n"
373 "RMDIR [drive:]path\nRD [drive:]path"));
374 return 0;
375 }
376
377 /* check if there is no space between the command and the path */
378 if (param[0] == _T('\0'))
379 {
380 /* search for the \ or . so that both short & long names will work */
381 for (place = cmd; *place; place++)
382 if (*place == _T('.') || *place == _T('\\'))
383 break;
384
385 if (*place)
386 dir = place;
387 else
388 /* signal that there are no parameters */
389 dir = NULL;
390 }
391 else
392 {
393 p = split (param, &argc);
394 if (argc > 1)
395 {
396 /*JPP 20-Jul-1998 use standard error message */
397 error_too_many_parameters (param);
398 freep (p);
399 return 1;
400 }
401 else
402 dir = p[0];
403 }
404
405 /* remove trailing \ if any, but ONLY if dir is not the root dir */
406 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
407 dir[_tcslen(dir) - 1] = _T('\0');
408
409 if (!RemoveDirectory (dir))
410 {
411 ErrorMessage (GetLastError(), _T("RD"));
412 freep (p);
413
414 return 1;
415 }
416
417 freep (p);
418
419 return 0;
420 }
421 #endif
422
423
424 /*
425 * set the exitflag to true
426 *
427 */
428 INT internal_exit (LPTSTR cmd, LPTSTR param)
429 {
430 if (!_tcsncmp (param, _T("/?"), 2))
431 {
432 ConOutPuts (_T("Exits the command line interpreter.\n\nEXIT"));
433 }
434 else
435 {
436 bExit = TRUE;
437 }
438
439 return 0;
440 }
441
442
443 #ifdef INCLUDE_CMD_REM
444 /*
445 * does nothing
446 *
447 */
448 INT cmd_rem (LPTSTR cmd, LPTSTR param)
449 {
450 if (!_tcsncmp (param, _T("/?"), 2))
451 {
452 ConOutPuts (_T("Starts a comment line in a batch file.\n\n"
453 "REM [Comment]"));
454 }
455
456 return 0;
457 }
458 #endif
459
460
461 INT cmd_showcommands (LPTSTR cmd, LPTSTR param)
462 {
463 LPCOMMAND cmdptr;
464 INT y;
465
466 y = 0;
467 cmdptr = cmds;
468 while (cmdptr->name)
469 {
470 if (++y == 8)
471 {
472 ConOutPuts (cmdptr->name);
473 y = 0;
474 }
475 else
476 ConOutPrintf (_T("%-10s"), cmdptr->name);
477
478 cmdptr++;
479 }
480
481 if (y != 0)
482 ConOutChar (_T('\n'));
483
484 return 0;
485 }