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