set right errorlevel for cd / chdir, left to do we do not have /d param that are...
[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 * 02-Apr-2004 (Magnus Olsen <magnus@greatlord.com>)
127 * Remove all hard code string so they can be
128 * translate to other langues.
129 */
130
131 #include "precomp.h"
132 #include "resource.h"
133
134 #ifdef INCLUDE_CMD_CHDIR
135
136 static LPTSTR lpLastPath;
137
138
139 VOID InitLastPath (VOID)
140 {
141 lpLastPath = NULL;
142 }
143
144
145 VOID FreeLastPath (VOID)
146 {
147 if (lpLastPath)
148 free (lpLastPath);
149 }
150
151 /*
152 * CD / CHDIR
153 *
154 */
155 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
156 {
157 LPTSTR dir; /* pointer to the directory to change to */
158 LPTSTR lpOldPath;
159 size_t size, str_len;
160 WIN32_FIND_DATA FileData;
161 HANDLE hSearch;
162 DWORD dwAttrs;
163 BOOL fFinished = FALSE;
164
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 ConOutResPuts(STRING_CD_HELP);
172 return 0;
173 }
174
175 nErrorLevel = 0;
176
177 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
178 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
179 post-processing of it after the function call (what would that accomplish?) */
180
181 size = _tcscspn(param, _T("\"") );
182 str_len = _tcslen(param)-1;
183
184 if ((param[size] == _T('"')) && (str_len >1))
185 {
186
187 if (size==0)
188 {
189 _tcsncpy(param,&param[size+1],str_len);
190 param[str_len] = _T('\0');
191 }
192
193 size = _tcscspn(param, _T("\"") );
194 if (param[size] == _T('"'))
195 {
196 param[size] = _T('\0');
197 }
198
199 }
200
201 str_len = _tcslen(param);
202 if (str_len==1)
203 {
204 if (param[0] == _T('*'))
205 {
206 param[0] = _T('.');
207 }
208 }
209
210 dir=param;
211
212 /* if doing a CD and no parameters given, print out current directory */
213 if (!dir || !dir[0])
214 {
215 TCHAR szPath[MAX_PATH];
216
217 GetCurrentDirectory (MAX_PATH, szPath);
218 ConOutPuts (szPath);
219 return 0;
220 }
221
222 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
223 {
224 if (lpLastPath)
225 dir = lpLastPath;
226 else
227 return 0;
228 }
229 else if (dir && _tcslen (dir)==2 && dir[1] == _T(':'))
230 {
231 TCHAR szRoot[3] = _T("A:");
232 TCHAR szPath[MAX_PATH];
233
234 szRoot[0] = _totupper (dir[0]);
235 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
236
237 /* PathRemoveBackslash */
238 if (_tcslen (szPath) > 3)
239 {
240 LPTSTR p = _tcsrchr (szPath, _T('\\'));
241 *p = _T('\0');
242 }
243
244 ConOutPuts (szPath);
245
246
247 return 0;
248 }
249
250 /* remove trailing \ if any, but ONLY if dir is not the root dir */
251 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
252 dir[_tcslen(dir) - 1] = _T('\0');
253
254
255 /* store current directory */
256 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
257 GetCurrentDirectory (MAX_PATH, lpOldPath);
258
259 if (!SetCurrentDirectory (dir))
260 {
261
262 hSearch = FindFirstFile(dir, &FileData);
263 if (hSearch == INVALID_HANDLE_VALUE)
264 {
265 ConOutFormatMessage(GetLastError());
266 free (lpOldPath);
267 lpOldPath = NULL;
268 nErrorLevel = 1;
269 return 1;
270 }
271
272
273 while (!fFinished)
274 {
275 dwAttrs = GetFileAttributes(FileData.cFileName);
276 #ifdef _DEBUG
277 DebugPrintf(_T("Search found folder :%s\n"),FileData.cFileName);
278 #endif
279 if ((dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
280 {
281 FindClose(hSearch);
282 // change folder
283 if (!SetCurrentDirectory (FileData.cFileName))
284 {
285 ConOutFormatMessage(GetLastError());
286 free (lpOldPath);
287 lpOldPath = NULL;
288 nErrorLevel = 1;
289 return 1;
290 }
291
292
293 return 0;
294 }
295
296 else if (!FindNextFile(hSearch, &FileData))
297 {
298 FindClose(hSearch);
299 ConOutFormatMessage(GetLastError());
300 nErrorLevel = 1;
301 free (lpOldPath);
302 lpOldPath = NULL;
303 return 1;
304 }
305 }
306
307 //ErrorMessage (GetLastError(), _T("CD"));
308 ConOutFormatMessage(GetLastError());
309 nErrorLevel = 1;
310
311 /* throw away current directory */
312 free (lpOldPath);
313 lpOldPath = NULL;
314
315 return 1;
316 }
317 else
318 {
319 GetCurrentDirectory(MAX_PATH, dir);
320 if (dir[0]!=lpOldPath[0])
321 {
322 SetCurrentDirectory(lpOldPath);
323 free(lpOldPath);
324 }
325 else
326 {
327 if (lpLastPath)
328 free (lpLastPath);
329 lpLastPath = lpOldPath;
330 }
331 }
332
333
334 return 0;
335 }
336 #endif
337
338
339
340 #ifdef INCLUDE_CMD_MKDIR
341 /*
342 * MD / MKDIR
343 *
344 */
345 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
346 {
347 LPTSTR dir; /* pointer to the directory to change to */
348 LPTSTR place; /* used to search for the \ when no space is used */
349 LPTSTR *p = NULL;
350 INT argc;
351
352 if (!_tcsncmp (param, _T("/?"), 2))
353 {
354 ConOutResPuts(STRING_MKDIR_HELP);
355 return 0;
356 }
357
358
359 /* check if there is no space between the command and the path */
360 if (param[0] == _T('\0'))
361 {
362 /* search for the \ or . so that both short & long names will work */
363 for (place = cmd; *place; place++)
364 if (*place == _T('.') || *place == _T('\\'))
365 break;
366
367 if (*place)
368 dir = place;
369 else
370 /* signal that there are no parameters */
371 dir = NULL;
372 }
373 else
374 {
375 p = split (param, &argc, FALSE);
376 if (argc > 1)
377 {
378 /*JPP 20-Jul-1998 use standard error message */
379 error_too_many_parameters (param);
380 freep (p);
381 return 1;
382 }
383 else
384 dir = p[0];
385 }
386
387 if (!dir)
388 {
389 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
390 return 1;
391 }
392
393 /* remove trailing \ if any, but ONLY if dir is not the root dir */
394 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
395 dir[_tcslen(dir) - 1] = _T('\0');
396
397 if (!CreateDirectory (dir, NULL))
398 {
399 ErrorMessage (GetLastError(), _T("MD"));
400
401 freep (p);
402 return 1;
403 }
404
405 freep (p);
406
407 return 0;
408 }
409 #endif
410
411
412 #ifdef INCLUDE_CMD_RMDIR
413 /*
414 * RD / RMDIR
415 *
416 */
417 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
418 {
419 LPTSTR dir; /* pointer to the directory to change to */
420 LPTSTR place; /* used to search for the \ when no space is used */
421
422 LPTSTR *p = NULL;
423 INT argc;
424
425 if (!_tcsncmp (param, _T("/?"), 2))
426 {
427 ConOutResPuts(STRING_RMDIR_HELP);
428 return 0;
429 }
430
431 /* check if there is no space between the command and the path */
432 if (param[0] == _T('\0'))
433 {
434 /* search for the \ or . so that both short & long names will work */
435 for (place = cmd; *place; place++)
436 if (*place == _T('.') || *place == _T('\\'))
437 break;
438
439 if (*place)
440 dir = place;
441 else
442 /* signal that there are no parameters */
443 dir = NULL;
444 }
445 else
446 {
447 p = split (param, &argc, FALSE);
448 if (argc > 1)
449 {
450 /*JPP 20-Jul-1998 use standard error message */
451 error_too_many_parameters (param);
452 freep (p);
453 return 1;
454 }
455 else
456 dir = p[0];
457 }
458
459 if (!dir)
460 {
461 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
462 return 1;
463 }
464
465 /* remove trailing \ if any, but ONLY if dir is not the root dir */
466 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
467 dir[_tcslen(dir) - 1] = _T('\0');
468
469 if (!RemoveDirectory (dir))
470 {
471 ErrorMessage (GetLastError(), _T("RD"));
472 freep (p);
473
474 return 1;
475 }
476
477 freep (p);
478
479 return 0;
480 }
481 #endif
482
483
484 /*
485 * set the exitflag to true
486 *
487 */
488 INT CommandExit (LPTSTR cmd, LPTSTR param)
489 {
490 if (!_tcsncmp (param, _T("/?"), 2))
491 ConOutResPuts(STRING_EXIT_HELP);
492
493 if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
494 {
495 param += 2;
496 while (_istspace (*param))
497 param++;
498 if (_istdigit(*param))
499 nErrorLevel = _ttoi(param);
500 ExitBatch (NULL);
501 }
502
503 else
504 bExit = TRUE;
505
506
507 return 0;
508
509 }
510
511 #ifdef INCLUDE_CMD_REM
512 /*
513 * does nothing
514 *
515 */
516 INT CommandRem (LPTSTR cmd, LPTSTR param)
517 {
518 if (!_tcsncmp (param, _T("/?"), 2))
519 {
520 ConOutResPuts(STRING_REM_HELP);
521 }
522
523 return 0;
524 }
525 #endif /* INCLUDE_CMD_REM */
526
527
528 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
529 {
530 PrintCommandList ();
531 return 0;
532 }
533
534 INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
535 {
536 PrintCommandListDetail ();
537 return 0;
538 }
539
540 /* EOF */