Cd does rember the cd c:\gcc and you can do c:
[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 /* help functions for getting current path from drive
152 without changing drive. Return code 0 = ok, 1 = fail.
153 INT GetRootPath("C:",outbuffer,chater size of outbuffer);
154 the first param can have any size, if the the two frist
155 letter are not a drive with : it will get Currentpath on
156 current drive exacly as GetCurrentDirectory does.
157 */
158
159 INT GetRootPath(TCHAR *InPath,TCHAR *OutPath,INT size)
160 {
161 INT retcode = 1;
162
163
164 if (_tcslen(InPath)>1)
165 {
166 if (InPath[1]==_T(':'))
167 {
168 TCHAR num[2];
169 INT t=0;
170
171 num[1] = _T('\0');
172 num[0] = InPath[0];
173 _tcslwr(num);
174
175 if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
176 {
177 t = (InPath[0] - _T('0')) +28;
178 }
179
180 if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
181 {
182 t = (InPath[0] - _T('a')) +1;
183 }
184
185 if (_tgetdcwd(t,OutPath,size) != NULL)
186 {
187 return 0;
188 }
189 }
190 }
191
192 /* fail */
193 if (_tcslen(InPath)>1)
194 {
195 if (InPath[1]==_T(':'))
196 return 1;
197 }
198
199 /* Get current directory */
200 retcode = GetCurrentDirectory(size,OutPath);
201 if (retcode==0)
202 return 1;
203
204 return 0;
205 }
206
207
208 /*
209 * CD / CHDIR
210 *
211 */
212 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
213 {
214 LPTSTR dir; /* pointer to the directory to change to */
215 LPTSTR lpOldPath;
216 size_t size, str_len;
217 WIN32_FIND_DATA FileData;
218 HANDLE hSearch;
219 DWORD dwAttrs;
220 BOOL fFinished = FALSE;
221
222
223 /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
224 but on the other hand paths are generally not very long*/
225
226 if (!_tcsncmp (param, _T("/?"), 2))
227 {
228 ConOutResPaging(TRUE,STRING_CD_HELP);
229 return 0;
230 }
231
232 nErrorLevel = 0;
233
234
235 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
236 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
237 post-processing of it after the function call (what would that accomplish?) */
238
239 size = _tcscspn(param, _T("\"") );
240 str_len = _tcslen(param)-1;
241
242 if ((param[size] == _T('"')) && (str_len >1))
243 {
244
245 if (size==0)
246 {
247 _tcsncpy(param,&param[size+1],str_len);
248 param[str_len] = _T('\0');
249 }
250
251 size = _tcscspn(param, _T("\"") );
252 if (param[size] == _T('"'))
253 {
254 param[size] = _T('\0');
255 }
256
257 }
258
259 str_len = _tcslen(param);
260 if (str_len==1)
261 {
262 if (param[0] == _T('*'))
263 {
264 param[0] = _T('.');
265 }
266 }
267
268 dir=param;
269
270 /* if doing a CD and no parameters given, print out current directory */
271 if (!dir || !dir[0])
272 {
273 TCHAR szPath[MAX_PATH];
274
275 GetCurrentDirectory (MAX_PATH, szPath);
276 ConOutPuts (szPath);
277 return 0;
278 }
279
280 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
281 {
282 if (lpLastPath)
283 dir = lpLastPath;
284 else
285 return 0;
286 }
287 else if (dir && _tcslen (dir)==2 && dir[1] == _T(':'))
288 {
289 TCHAR szRoot[3] = _T("A:");
290 TCHAR szPath[MAX_PATH];
291
292 szRoot[0] = _totupper (dir[0]);
293 GetRootPath (szRoot, szPath,MAX_PATH);
294
295 /* PathRemoveBackslash */
296 if (_tcslen (szPath) > 3)
297 {
298 LPTSTR p = _tcsrchr (szPath, _T('\\'));
299 *p = _T('\0');
300 }
301
302 ConOutPuts (szPath);
303
304
305 return 0;
306 }
307
308 /* remove trailing \ if any, but ONLY if dir is not the root dir */
309 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
310 dir[_tcslen(dir) - 1] = _T('\0');
311
312
313 /* store current directory */
314 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
315 GetCurrentDirectory (MAX_PATH, lpOldPath);
316
317 chdir(dir);
318 if (!SetCurrentDirectory (dir))
319 {
320
321 hSearch = FindFirstFile(dir, &FileData);
322 if (hSearch == INVALID_HANDLE_VALUE)
323 {
324 ConOutFormatMessage(GetLastError());
325 free (lpOldPath);
326 lpOldPath = NULL;
327 nErrorLevel = 1;
328 return 1;
329 }
330
331
332 while (!fFinished)
333 {
334 dwAttrs = GetFileAttributes(FileData.cFileName);
335 #ifdef _DEBUG
336 DebugPrintf(_T("Search found folder :%s\n"),FileData.cFileName);
337 #endif
338 if ((dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
339 {
340 FindClose(hSearch);
341 // change folder
342 chdir(dir);
343 if (!SetCurrentDirectory (FileData.cFileName))
344 {
345 ConOutFormatMessage(GetLastError());
346 free (lpOldPath);
347 lpOldPath = NULL;
348 nErrorLevel = 1;
349 return 1;
350 }
351
352
353 return 0;
354 }
355
356 else if (!FindNextFile(hSearch, &FileData))
357 {
358 FindClose(hSearch);
359 ConOutFormatMessage(GetLastError());
360 nErrorLevel = 1;
361 free (lpOldPath);
362 lpOldPath = NULL;
363 return 1;
364 }
365 }
366
367 //ErrorMessage (GetLastError(), _T("CD"));
368 ConOutFormatMessage(GetLastError());
369 nErrorLevel = 1;
370
371 /* throw away current directory */
372 free (lpOldPath);
373 lpOldPath = NULL;
374
375 return 1;
376 }
377 else
378 {
379 GetCurrentDirectory(MAX_PATH, dir);
380 if (dir[0]!=lpOldPath[0])
381 {
382 chdir(lpOldPath);
383 SetCurrentDirectory(lpOldPath);
384 free(lpOldPath);
385 }
386 else
387 {
388 if (lpLastPath)
389 free (lpLastPath);
390 lpLastPath = lpOldPath;
391 }
392 }
393
394
395 return 0;
396 }
397 #endif
398
399
400
401 #ifdef INCLUDE_CMD_MKDIR
402 /*
403 * MD / MKDIR
404 *
405 */
406 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
407 {
408 LPTSTR dir; /* pointer to the directory to change to */
409 LPTSTR place; /* used to search for the \ when no space is used */
410 LPTSTR *p = NULL;
411 INT argc;
412
413 if (!_tcsncmp (param, _T("/?"), 2))
414 {
415 ConOutResPaging(TRUE,STRING_MKDIR_HELP);
416 return 0;
417 }
418
419
420 /* check if there is no space between the command and the path */
421 if (param[0] == _T('\0'))
422 {
423 /* search for the \ or . so that both short & long names will work */
424 for (place = cmd; *place; place++)
425 if (*place == _T('.') || *place == _T('\\'))
426 break;
427
428 if (*place)
429 dir = place;
430 else
431 /* signal that there are no parameters */
432 dir = NULL;
433 }
434 else
435 {
436 p = split (param, &argc, FALSE);
437 if (argc > 1)
438 {
439 /*JPP 20-Jul-1998 use standard error message */
440 error_too_many_parameters (param);
441 freep (p);
442 return 1;
443 }
444 else
445 dir = p[0];
446 }
447
448 if (!dir)
449 {
450 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
451 return 1;
452 }
453
454 /* remove trailing \ if any, but ONLY if dir is not the root dir */
455 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
456 dir[_tcslen(dir) - 1] = _T('\0');
457
458 if (!CreateDirectory (dir, NULL))
459 {
460 ErrorMessage (GetLastError(), _T("MD"));
461
462 freep (p);
463 return 1;
464 }
465
466 freep (p);
467
468 return 0;
469 }
470 #endif
471
472
473 #ifdef INCLUDE_CMD_RMDIR
474 /*
475 * RD / RMDIR
476 *
477 */
478 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
479 {
480 LPTSTR dir; /* pointer to the directory to change to */
481 LPTSTR place; /* used to search for the \ when no space is used */
482
483 LPTSTR *p = NULL;
484 INT argc;
485
486 if (!_tcsncmp (param, _T("/?"), 2))
487 {
488 ConOutResPaging(TRUE,STRING_RMDIR_HELP);
489 return 0;
490 }
491
492 /* check if there is no space between the command and the path */
493 if (param[0] == _T('\0'))
494 {
495 /* search for the \ or . so that both short & long names will work */
496 for (place = cmd; *place; place++)
497 if (*place == _T('.') || *place == _T('\\'))
498 break;
499
500 if (*place)
501 dir = place;
502 else
503 /* signal that there are no parameters */
504 dir = NULL;
505 }
506 else
507 {
508 p = split (param, &argc, FALSE);
509 if (argc > 1)
510 {
511 /*JPP 20-Jul-1998 use standard error message */
512 error_too_many_parameters (param);
513 freep (p);
514 return 1;
515 }
516 else
517 dir = p[0];
518 }
519
520 if (!dir)
521 {
522 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
523 return 1;
524 }
525
526 /* remove trailing \ if any, but ONLY if dir is not the root dir */
527 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
528 dir[_tcslen(dir) - 1] = _T('\0');
529
530 if (!RemoveDirectory (dir))
531 {
532 ErrorMessage (GetLastError(), _T("RD"));
533 freep (p);
534
535 return 1;
536 }
537
538 freep (p);
539
540 return 0;
541 }
542 #endif
543
544
545 /*
546 * set the exitflag to true
547 *
548 */
549 INT CommandExit (LPTSTR cmd, LPTSTR param)
550 {
551 if (!_tcsncmp (param, _T("/?"), 2))
552 ConOutResPaging(TRUE,STRING_EXIT_HELP);
553
554 if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
555 {
556 param += 2;
557 while (_istspace (*param))
558 param++;
559 if (_istdigit(*param))
560 nErrorLevel = _ttoi(param);
561 ExitBatch (NULL);
562 }
563
564 else
565 bExit = TRUE;
566
567
568 return 0;
569
570 }
571
572 #ifdef INCLUDE_CMD_REM
573 /*
574 * does nothing
575 *
576 */
577 INT CommandRem (LPTSTR cmd, LPTSTR param)
578 {
579 if (!_tcsncmp (param, _T("/?"), 2))
580 {
581 ConOutResPaging(TRUE,STRING_REM_HELP);
582 }
583
584 return 0;
585 }
586 #endif /* INCLUDE_CMD_REM */
587
588
589 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
590 {
591 PrintCommandList ();
592 return 0;
593 }
594
595 INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
596 {
597 PrintCommandListDetail ();
598 return 0;
599 }
600
601 /* EOF */