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