a04d9a6f0a6798f9ff4aea10fbc594cf20193123
[reactos.git] / reactos / base / shell / 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)
101 * Replaced DOS calls by Win32 calls.
102 *
103 * 08-Dec-1998 (Eric Kohl)
104 * Added help texts ("/?").
105 *
106 * 18-Dec-1998 (Eric Kohl)
107 * Added support for quoted arguments (cd "program files").
108 *
109 * 07-Jan-1999 (Eric Kohl)
110 * Clean up.
111 *
112 * 26-Jan-1999 (Eric Kohl)
113 * Replaced remaining CRT io functions by Win32 io functions.
114 * Unicode safe!
115 *
116 * 30-Jan-1999 (Eric Kohl)
117 * Added "cd -" feature. Changes to the previous directory.
118 *
119 * 15-Mar-1999 (Eric Kohl)
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 * 19-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
131 * Rewrite the CD, it working as Windows 2000 CMD
132 *
133 * 19-Jul-2005 (Magnus Olsen <magnus@greatlord.com>)
134 * Add SetRootPath and GetRootPath
135 *
136 * 14-Jul-2007 (Pierre Schweitzer <heis_spiter@hotmail.com>)
137 * Added commands help display to help command (ex. : "help cmd")
138 */
139
140 #include <precomp.h>
141
142 #ifdef INCLUDE_CMD_CHDIR
143
144 static LPTSTR lpLastPath;
145
146
147 VOID InitLastPath (VOID)
148 {
149 lpLastPath = NULL;
150 }
151
152
153 VOID FreeLastPath (VOID)
154 {
155 if (lpLastPath)
156 cmd_free (lpLastPath);
157 }
158
159 /* help functions for getting current path from drive
160 without changing drive. Return code 0 = ok, 1 = fail.
161 INT GetRootPath("C:",outbuffer,chater size of outbuffer);
162 the first param can have any size, if the the two frist
163 letter are not a drive with : it will get Currentpath on
164 current drive exacly as GetCurrentDirectory does.
165 */
166
167 INT GetRootPath(TCHAR *InPath,TCHAR *OutPath,INT size)
168 {
169 INT retcode = 1;
170
171 if (_tcslen(InPath)>1)
172 {
173 if (InPath[1]==_T(':'))
174 {
175 INT t=0;
176
177 if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
178 {
179 t = (InPath[0] - _T('0')) +28;
180 }
181
182 if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
183 {
184 t = (InPath[0] - _T('a')) +1;
185 InPath[0] = t + _T('A') - 1;
186 }
187
188 if ((InPath[0] >= _T('A')) && (InPath[0] <= _T('Z')))
189 {
190 t = (InPath[0] - _T('A')) +1;
191 }
192
193 if (_tgetdcwd(t,OutPath,size) != NULL)
194 {
195 return 0;
196 }
197 }
198 }
199
200 /* fail */
201 if (_tcslen(InPath)>1)
202 {
203 if (InPath[1]==_T(':'))
204 return 1;
205 }
206
207 /* Get current directory */
208 retcode = GetCurrentDirectory(size,OutPath);
209 if (retcode==0)
210 return 1;
211
212 return 0;
213 }
214
215
216 BOOL SetRootPath(TCHAR *InPath)
217 {
218 TCHAR oldpath[MAX_PATH];
219 TCHAR OutPath[MAX_PATH];
220 TCHAR OutPathTemp[MAX_PATH];
221 TCHAR OutPathTemp2[MAX_PATH];
222 BOOL fail;
223
224
225 /* Get The current directory path and save it */
226 fail = GetCurrentDirectory(MAX_PATH,oldpath);
227 if (!fail)
228 return 1;
229
230 /* Get current drive directory path if C: was only pass down*/
231
232 if (_tcsncicmp(&InPath[1],_T(":\\"),2)!=0)
233 {
234 if (!GetRootPath(InPath,OutPathTemp,MAX_PATH))
235 _tcscpy(OutPathTemp,InPath);
236 }
237 else
238 {
239 _tcscpy(OutPathTemp,InPath);
240 }
241
242 _tcsupr(OutPathTemp);
243 /* The use of both of these together will correct the case of a path
244 where as one alone or GetFullPath will not. Exameple:
245 c:\windows\SYSTEM32 => C:\WINDOWS\system32 */
246 GetFullPathName(OutPathTemp, MAX_PATH, OutPathTemp2, NULL);
247 GetPathCase(OutPathTemp2, OutPath);
248
249 fail = SetCurrentDirectory(OutPath);
250 if (!fail)
251 return 1;
252
253
254
255 SetCurrentDirectory(OutPath);
256 GetCurrentDirectory(MAX_PATH,OutPath);
257 _tchdir(OutPath);
258
259 if (_tcsncicmp(OutPath,oldpath,2)!=0)
260 SetCurrentDirectory(oldpath);
261
262 return 0;
263 }
264
265
266 /*
267 * CD / CHDIR
268 *
269 */
270 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
271 {
272
273 WIN32_FIND_DATA f;
274 HANDLE hFile;
275 BOOL bChangeDrive = FALSE;
276 TCHAR szPath[MAX_PATH];
277 TCHAR szFinalPath[MAX_PATH];
278 TCHAR * tmpPath;
279 TCHAR szCurrent[MAX_PATH];
280 TCHAR szMsg[RC_STRING_MAX_SIZE];
281 INT i;
282
283
284 /* Filter out special cases first */
285
286 /* Print Help */
287 if (!_tcsncmp(param, _T("/?"), 2))
288 {
289 ConOutResPaging(TRUE,STRING_CD_HELP);
290 return 0;
291 }
292
293 /* Set Error Level to Success */
294 nErrorLevel = 0;
295
296 /* Input String Contains /D Switch */
297 if (!_tcsncicmp(param, _T("/D"), 2))
298 {
299 bChangeDrive = TRUE;
300 tmpPath = _tcsstr(param,_T(" "));
301 if(!tmpPath)
302 {
303 /* Didnt find an directories */
304 LoadString(CMD_ModuleHandle, STRING_ERROR_PATH_NOT_FOUND, szMsg, RC_STRING_MAX_SIZE);
305 ConErrPrintf(szMsg);
306 nErrorLevel = 1;
307 return 1;
308 }
309 tmpPath++;
310 _tcscpy(szPath,tmpPath);
311 }
312 else
313 {
314 _tcscpy(szPath,param);
315 }
316
317 /* Print Current Directory on a disk */
318 if (_tcslen(szPath) == 2 && szPath[1] == _T(':'))
319 {
320 if(GetRootPath(szPath,szCurrent,MAX_PATH))
321 {
322 nErrorLevel = 1;
323 return 1;
324 }
325 ConOutPuts(szCurrent);
326 return 0;
327 }
328
329 /* Get Current Directory */
330 GetRootPath(_T("."),szCurrent,MAX_PATH);
331
332 /* Remove " */
333 i = 0;
334 while(i < (INT)_tcslen(szPath))
335 {
336 if(szPath[i] == _T('\"'))
337 memmove(&szPath[i],&szPath[i + 1], _tcslen(&szPath[i]) * sizeof(TCHAR));
338 else
339 i++;
340 }
341
342 tmpPath = szPath;
343 while (_istspace (*tmpPath))
344 tmpPath++;
345 _tcscpy(szPath,tmpPath);
346
347 if (szPath[0] == _T('\0'))
348 {
349 ConOutPuts(szCurrent);
350 return 0;
351 }
352
353
354 /* change to full path if relative path was given */
355 GetFullPathName(szPath,MAX_PATH,szFinalPath,NULL);
356
357 if(szFinalPath[_tcslen(szFinalPath) - 1] == _T('\\') && _tcslen(szFinalPath) > 3)
358 szFinalPath[_tcslen(szFinalPath) - 1] = _T('\0');
359
360 /* Handle Root Directory Alone*/
361 if (_tcslen(szFinalPath) == 3 && szFinalPath[1] == _T(':'))
362 {
363 if(!SetRootPath(szFinalPath))
364 {
365 /* Change prompt if it is one the same drive or /D */
366 if(bChangeDrive || !_tcsncicmp(szFinalPath,szCurrent,1))
367 SetCurrentDirectory(szFinalPath);
368 return 0;
369 }
370 /* Didnt find an directories */
371 LoadString(CMD_ModuleHandle, STRING_ERROR_PATH_NOT_FOUND, szMsg, RC_STRING_MAX_SIZE);
372 ConErrPrintf(szMsg);
373 nErrorLevel = 1;
374 return 1;
375
376 }
377
378 /* Get a list of all the files */
379 hFile = FindFirstFile (szFinalPath, &f);
380
381 do
382 {
383 if(hFile == INVALID_HANDLE_VALUE)
384 {
385 ConErrFormatMessage (GetLastError(), szFinalPath);
386 nErrorLevel = 1;
387 return 1;
388 }
389
390 /* Strip the paths back to the folder they are in */
391 for(i = (_tcslen(szFinalPath) - 1); i > -1; i--)
392 if(szFinalPath[i] != _T('\\'))
393 szFinalPath[i] = _T('\0');
394 else
395 break;
396
397 _tcscat(szFinalPath,f.cFileName);
398
399 if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
400 {
401 if(!SetRootPath(szFinalPath))
402 {
403 /* Change for /D */
404 if(bChangeDrive)
405 {
406 _tcsupr(szFinalPath);
407 GetPathCase(szFinalPath, szPath);
408 SetCurrentDirectory(szPath);
409 }
410 return 0;
411 }
412
413 }
414 }while(FindNextFile (hFile, &f));
415
416 /* Didnt find an directories */
417 LoadString(CMD_ModuleHandle, STRING_ERROR_PATH_NOT_FOUND, szMsg, RC_STRING_MAX_SIZE);
418 ConErrPrintf(szMsg);
419 nErrorLevel = 1;
420 return 1;
421 }
422
423 #endif
424
425
426
427 #ifdef INCLUDE_CMD_MKDIR
428
429 /* Helper funtion for mkdir to make directories in a path.
430 Dont use the api to decrease depence on libs */
431 BOOL
432 MakeFullPath(TCHAR * DirPath)
433 {
434 TCHAR path[MAX_PATH];
435 TCHAR *p = DirPath;
436 INT n;
437
438 if (p[0] && p[1] == _T(':'))
439 p += 2;
440 while (*p == _T('\\'))
441 p++; /* skip drive root */
442 while ((p = _tcschr(p, _T('\\'))) != NULL)
443 {
444 n = p - DirPath + 1;
445 _tcsncpy(path, DirPath, n);
446 path[n] = _T('\0');
447 if( !CreateDirectory(path, NULL) &&
448 (GetLastError() != ERROR_ALREADY_EXISTS))
449 return FALSE;
450 p++;
451 }
452 if (GetLastError() == ERROR_ALREADY_EXISTS)
453 SetLastError(ERROR_SUCCESS);
454
455 return TRUE;
456 }
457
458 /*
459 * MD / MKDIR
460 *
461 */
462 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
463 {
464 LPTSTR dir; /* pointer to the directory to change to */
465 LPTSTR place; /* used to search for the \ when no space is used */
466 LPTSTR new_dir, *p = NULL;
467 INT argc;
468 nErrorLevel = 0;
469 if (!_tcsncmp (param, _T("/?"), 2))
470 {
471 ConOutResPaging(TRUE,STRING_MKDIR_HELP);
472 return 0;
473 }
474
475
476 /* check if there is no space between the command and the path */
477 if (param[0] == _T('\0'))
478 {
479 /* search for the \ or . so that both short & long names will work */
480 for (place = cmd; *place; place++)
481 if (*place == _T('.') || *place == _T('\\'))
482 break;
483
484 if (*place)
485 {
486 argc = 0;
487 if (add_entry(&argc, &p, place))
488 dir = place;
489 else
490 dir = NULL;
491 }
492 else
493 /* signal that there are no parameters */
494 dir = NULL;
495 }
496 else
497 {
498 p = split (param, &argc, FALSE);
499 if (argc > 1)
500 {
501 /*JPP 20-Jul-1998 use standard error message */
502 error_too_many_parameters (param);
503 freep (p);
504 return 1;
505 }
506 else
507 dir = p[0];
508 }
509
510 if (!dir)
511 {
512 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
513 nErrorLevel = 1;
514 if(p != NULL)
515 freep (p);
516 return 1;
517 }
518
519 /* Add a \ at the end of the path is there isnt on already */
520 if (dir[_tcslen (dir) - 1] != _T('\\'))
521 {
522 new_dir = cmd_realloc(dir, (_tcslen (dir) + 2) * sizeof(TCHAR));
523 if (new_dir != NULL)
524 {
525 p[0] = dir = new_dir;
526 _tcscat(dir,_T("\\"));
527 }
528 }
529
530 if (!MakeFullPath(dir))
531 {
532 if(GetLastError() == ERROR_PATH_NOT_FOUND)
533 {
534 ConErrResPuts(STRING_MD_ERROR2);
535 }
536 else
537 {
538 ErrorMessage (GetLastError(), _T("MD"));
539 }
540 nErrorLevel = 1;
541 freep (p);
542 return 1;
543 }
544
545 freep (p);
546
547 return 0;
548 }
549 #endif
550
551
552 #ifdef INCLUDE_CMD_RMDIR
553 /*
554 * RD / RMDIR
555 *
556 */
557 BOOL DeleteFolder(LPTSTR FileName)
558 {
559 TCHAR Base[MAX_PATH];
560 TCHAR TempFileName[MAX_PATH];
561 HANDLE hFile;
562 WIN32_FIND_DATA f;
563 _tcscpy(Base,FileName);
564 _tcscat(Base,_T("\\*"));
565 hFile = FindFirstFile(Base, &f);
566 Base[_tcslen(Base) - 1] = _T('\0');
567 if (hFile != INVALID_HANDLE_VALUE)
568 {
569 do
570 {
571 if (!_tcscmp(f.cFileName, _T(".")) ||
572 !_tcscmp(f.cFileName, _T("..")))
573 continue;
574 _tcscpy(TempFileName,Base);
575 _tcscat(TempFileName,f.cFileName);
576
577 if(f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
578 DeleteFolder(TempFileName);
579 else
580 {
581 SetFileAttributes(TempFileName,FILE_ATTRIBUTE_NORMAL);
582 if(!DeleteFile(TempFileName))
583 return 0;
584 }
585
586 }while (FindNextFile (hFile, &f));
587 FindClose (hFile);
588 }
589 return RemoveDirectory(FileName);
590 }
591 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
592 {
593 TCHAR dir[MAX_PATH]; /* pointer to the directory to change to */
594 char ch;
595 INT args;
596 LPTSTR *arg = NULL;
597 INT i;
598 BOOL RD_SUB = FALSE;
599 BOOL RD_QUIET = FALSE;
600 HANDLE hFile;
601 WIN32_FIND_DATA f;
602 INT res;
603 TCHAR szMsg[RC_STRING_MAX_SIZE];
604 TCHAR szFullPath[MAX_PATH];
605
606 if (!_tcsncmp (param, _T("/?"), 2))
607 {
608 ConOutResPaging(TRUE,STRING_RMDIR_HELP);
609 return 0;
610 }
611
612 nErrorLevel = 0;
613
614 arg = split (param, &args, FALSE);
615
616 if (args == 0)
617 {
618 /* only command given */
619 error_req_param_missing ();
620 freep (arg);
621 return 1;
622 }
623
624 dir[0] = 0;
625 /* check for options anywhere in command line */
626 for (i = 0; i < args; i++)
627 {
628 if (*arg[i] == _T('/'))
629 {
630 /*found a command, but check to make sure it has something after it*/
631 if (_tcslen (arg[i]) == 2)
632 {
633 ch = _totupper (arg[i][1]);
634 if (ch == _T('S'))
635 {
636 RD_SUB = TRUE;
637 }
638 else if (ch == _T('Q'))
639 {
640 RD_QUIET = TRUE;
641 }
642 }
643 }
644 else
645 {
646 /* get the folder name */
647 _tcscpy(dir,arg[i]);
648 }
649 }
650
651 if (dir[0] == _T('\0'))
652 {
653 /* No folder to remove */
654 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
655 freep(arg);
656 return 1;
657 }
658
659 GetFullPathName(dir,MAX_PATH,szFullPath,NULL);
660 /* remove trailing \ if any, but ONLY if dir is not the root dir */
661 if (_tcslen (szFullPath) >= 2 && szFullPath[_tcslen (szFullPath) - 1] == _T('\\'))
662 szFullPath[_tcslen(szFullPath) - 1] = _T('\0');
663
664 if(RD_SUB)
665 {
666 /* ask if they want to delete evrything in the folder */
667 if (!RD_QUIET)
668 {
669 LoadString( CMD_ModuleHandle, STRING_DEL_HELP2, szMsg, RC_STRING_MAX_SIZE);
670 res = FilePromptYNA (szMsg);
671 if ((res == PROMPT_NO) || (res == PROMPT_BREAK))
672 {
673 freep(arg);
674 nErrorLevel = 1;
675 return 1;
676 }
677 }
678
679 }
680 else
681 {
682 /* check for files in the folder */
683 _tcscat(szFullPath,_T("\\*"));
684
685 hFile = FindFirstFile(szFullPath, &f);
686 if (hFile != INVALID_HANDLE_VALUE)
687 {
688 do
689 {
690 if (!_tcscmp(f.cFileName,_T(".")) ||
691 !_tcscmp(f.cFileName,_T("..")))
692 continue;
693 ConOutResPuts(STRING_RMDIR_HELP2);
694 freep(arg);
695 FindClose (hFile);
696 nErrorLevel = 1;
697 return 1;
698 }while (FindNextFile (hFile, &f));
699 FindClose (hFile);
700 }
701 /* reovme the \\* */
702 szFullPath[_tcslen(szFullPath) - 2] = _T('\0');
703 }
704
705 if (!DeleteFolder(szFullPath))
706 {
707 /* Couldnt delete the folder, clean up and print out the error */
708 ErrorMessage (GetLastError(), _T("RD"));
709 freep (arg);
710 nErrorLevel = 1;
711 return 1;
712 }
713
714 freep (arg);
715 return 0;
716 }
717 #endif
718
719
720 /*
721 * set the exitflag to true
722 *
723 */
724 INT CommandExit (LPTSTR cmd, LPTSTR param)
725 {
726 if (!_tcsncmp (param, _T("/?"), 2))
727 {
728 ConOutResPaging(TRUE,STRING_EXIT_HELP);
729 /* Just make sure */
730 bExit = FALSE;
731 /* Dont exit */
732 return 0;
733 }
734
735 if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
736 {
737 param += 2;
738 while (_istspace (*param))
739 param++;
740 if (_istdigit(*param))
741 nErrorLevel = _ttoi(param);
742 ExitBatch (NULL);
743 }
744
745 else
746 bExit = TRUE;
747
748
749 return 0;
750
751 }
752
753 #ifdef INCLUDE_CMD_REM
754 /*
755 * does nothing
756 *
757 */
758 INT CommandRem (LPTSTR cmd, LPTSTR param)
759 {
760 if (!_tcsncmp (param, _T("/?"), 2))
761 {
762 ConOutResPaging(TRUE,STRING_REM_HELP);
763 }
764
765 return 0;
766 }
767 #endif /* INCLUDE_CMD_REM */
768
769
770 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
771 {
772 PrintCommandList ();
773 return 0;
774 }
775
776 INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
777 {
778 /* If a param was send, display help of correspondent command */
779 if (_tcslen(param))
780 {
781 LPTSTR NewCommand = cmd_alloc((_tcslen(param)+4)*sizeof(TCHAR));
782 _tcscpy(NewCommand, param);
783 _tcscat(NewCommand, _T(" /?"));
784 DoCommand(NewCommand);
785 cmd_free(NewCommand);
786 }
787 /* Else, display detailed commands list */
788 else
789 {
790 PrintCommandListDetail ();
791 }
792 return 0;
793 }
794
795 /* EOF */