2 * INTERNAL.C - command.com internal commands.
7 * 17/08/94 (Tim Norman)
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.
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.
23 * i have added support for directorys. now if the directory attribute is
24 * detected the file size if replaced with the string "<dir>".
38 * does not support wildcard selection.
40 * todo: add delete directory support.
41 * add recursive directory delete support.
46 * does not support wildcard selection.
48 * todo: add rename directory support.
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.
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 \
60 * added an isatty call to DIR so it won't prompt for keypresses unless
61 * stdin and stdout are the console.
63 * changed parameters to be mutually consistent to make calling the
72 * 01/22/96 (Oliver Mueller)
73 * error messages are now handled by perror.
75 * 02/05/96 (Tim Norman)
76 * converted all functions to accept first/rest parameters
78 * 07/26/96 (Tim Norman)
79 * changed return values to int instead of void
83 * 12/23/96 (Aaron Kaufman)
84 * rewrote dir() to mimic MS-DOS's dir
86 * 01/28/97 (Tim Norman)
87 * cleaned up Aaron's DIR code
89 * 06/13/97 (Tim Norman)
90 * moved DIR code to dir.c
91 * re-implemented Aaron's DIR code
93 * 06/14/97 (Steffan Kaiser)
97 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
98 * added config.h include
100 * 03-Dec-1998 (Eric Kohl)
101 * Replaced DOS calls by Win32 calls.
103 * 08-Dec-1998 (Eric Kohl)
104 * Added help texts ("/?").
106 * 18-Dec-1998 (Eric Kohl)
107 * Added support for quoted arguments (cd "program files").
109 * 07-Jan-1999 (Eric Kohl)
112 * 26-Jan-1999 (Eric Kohl)
113 * Replaced remaining CRT io functions by Win32 io functions.
116 * 30-Jan-1999 (Eric Kohl)
117 * Added "cd -" feature. Changes to the previous directory.
119 * 15-Mar-1999 (Eric Kohl)
120 * Fixed bug in "cd -" feature. If the previous directory was a root
121 * directory, it was ignored.
123 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
124 * Improved chdir/cd command.
126 * 02-Apr-2004 (Magnus Olsen <magnus@greatlord.com>)
127 * Remove all hard code string so they can be
128 * translate to other langues.
130 * 19-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
131 * Rewrite the CD, it working as Windows 2000 CMD
133 * 19-Jul-2005 (Magnus Olsen <magnus@greatlord.com>)
134 * Add SetRootPath and GetRootPath
136 * 14-Jul-2007 (Pierre Schweitzer <heis_spiter@hotmail.com>)
137 * Added commands help display to help command (ex. : "help cmd")
142 #ifdef INCLUDE_CMD_CHDIR
144 static LPTSTR lpLastPath
;
147 VOID
InitLastPath (VOID
)
153 VOID
FreeLastPath (VOID
)
156 cmd_free (lpLastPath
);
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.
167 INT
GetRootPath(TCHAR
*InPath
,TCHAR
*OutPath
,INT size
)
171 if (_tcslen(InPath
)>1)
173 if (InPath
[1]==_T(':'))
177 if ((InPath
[0] >= _T('0')) && (InPath
[0] <= _T('9')))
179 t
= (InPath
[0] - _T('0')) +28;
182 if ((InPath
[0] >= _T('a')) && (InPath
[0] <= _T('z')))
184 t
= (InPath
[0] - _T('a')) +1;
185 InPath
[0] = t
+ _T('A') - 1;
188 if ((InPath
[0] >= _T('A')) && (InPath
[0] <= _T('Z')))
190 t
= (InPath
[0] - _T('A')) +1;
193 if (_tgetdcwd(t
,OutPath
,size
) != NULL
)
201 if (_tcslen(InPath
)>1)
203 if (InPath
[1]==_T(':'))
207 /* Get current directory */
208 retcode
= GetCurrentDirectory(size
,OutPath
);
216 BOOL
SetRootPath(TCHAR
*InPath
)
218 TCHAR oldpath
[MAX_PATH
];
219 TCHAR OutPath
[MAX_PATH
];
220 TCHAR OutPathTemp
[MAX_PATH
];
221 TCHAR OutPathTemp2
[MAX_PATH
];
225 /* Get The current directory path and save it */
226 fail
= GetCurrentDirectory(MAX_PATH
,oldpath
);
230 /* Get current drive directory path if C: was only pass down*/
232 if (_tcsncicmp(&InPath
[1],_T(":\\"),2)!=0)
234 if (!GetRootPath(InPath
,OutPathTemp
,MAX_PATH
))
235 _tcscpy(OutPathTemp
,InPath
);
239 _tcscpy(OutPathTemp
,InPath
);
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
);
249 fail
= SetCurrentDirectory(OutPath
);
255 SetCurrentDirectory(OutPath
);
256 GetCurrentDirectory(MAX_PATH
,OutPath
);
259 if (_tcsncicmp(OutPath
,oldpath
,2)!=0)
260 SetCurrentDirectory(oldpath
);
270 INT
cmd_chdir (LPTSTR cmd
, LPTSTR param
)
275 BOOL bChangeDrive
= FALSE
;
276 TCHAR szPath
[MAX_PATH
];
277 TCHAR szFinalPath
[MAX_PATH
];
279 TCHAR szCurrent
[MAX_PATH
];
283 /* Filter out special cases first */
286 if (!_tcsncmp(param
, _T("/?"), 2))
288 ConOutResPaging(TRUE
,STRING_CD_HELP
);
292 /* Set Error Level to Success */
295 /* Input String Contains /D Switch */
296 if (!_tcsncicmp(param
, _T("/D"), 2))
299 tmpPath
= _tcsstr(param
,_T(" "));
302 /* Didnt find an directories */
303 ConErrResPrintf(STRING_ERROR_PATH_NOT_FOUND
);
308 _tcscpy(szPath
,tmpPath
);
312 _tcscpy(szPath
,param
);
315 /* Print Current Directory on a disk */
316 if (_tcslen(szPath
) == 2 && szPath
[1] == _T(':'))
318 if(GetRootPath(szPath
,szCurrent
,MAX_PATH
))
323 ConOutPuts(szCurrent
);
327 /* Get Current Directory */
328 GetRootPath(_T("."),szCurrent
,MAX_PATH
);
332 while(i
< (INT
)_tcslen(szPath
))
334 if(szPath
[i
] == _T('\"'))
335 memmove(&szPath
[i
],&szPath
[i
+ 1], _tcslen(&szPath
[i
]) * sizeof(TCHAR
));
341 while (_istspace (*tmpPath
))
343 _tcscpy(szPath
,tmpPath
);
345 if (szPath
[0] == _T('\0'))
347 ConOutPuts(szCurrent
);
352 /* change to full path if relative path was given */
353 GetFullPathName(szPath
,MAX_PATH
,szFinalPath
,NULL
);
355 if(szFinalPath
[_tcslen(szFinalPath
) - 1] == _T('\\') && _tcslen(szFinalPath
) > 3)
356 szFinalPath
[_tcslen(szFinalPath
) - 1] = _T('\0');
358 /* Handle Root Directory Alone*/
359 if (_tcslen(szFinalPath
) == 3 && szFinalPath
[1] == _T(':'))
361 if(!SetRootPath(szFinalPath
))
363 /* Change prompt if it is one the same drive or /D */
364 if(bChangeDrive
|| !_tcsncicmp(szFinalPath
,szCurrent
,1))
365 SetCurrentDirectory(szFinalPath
);
368 /* Didnt find an directories */
369 ConErrResPrintf(STRING_ERROR_PATH_NOT_FOUND
);
375 /* Get a list of all the files */
376 hFile
= FindFirstFile (szFinalPath
, &f
);
380 if(hFile
== INVALID_HANDLE_VALUE
)
382 ConErrFormatMessage (GetLastError(), szFinalPath
);
387 /* Strip the paths back to the folder they are in */
388 for(i
= (_tcslen(szFinalPath
) - 1); i
> -1; i
--)
389 if(szFinalPath
[i
] != _T('\\'))
390 szFinalPath
[i
] = _T('\0');
394 _tcscat(szFinalPath
,f
.cFileName
);
396 if ((f
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
)
398 if(!SetRootPath(szFinalPath
))
403 _tcsupr(szFinalPath
);
404 GetPathCase(szFinalPath
, szPath
);
405 SetCurrentDirectory(szPath
);
411 }while(FindNextFile (hFile
, &f
));
413 /* Didnt find an directories */
414 ConErrResPrintf(STRING_ERROR_PATH_NOT_FOUND
);
423 #ifdef INCLUDE_CMD_MKDIR
425 /* Helper funtion for mkdir to make directories in a path.
426 Dont use the api to decrease depence on libs */
428 MakeFullPath(TCHAR
* DirPath
)
430 TCHAR path
[MAX_PATH
];
434 if (p
[0] && p
[1] == _T(':'))
436 while (*p
== _T('\\'))
437 p
++; /* skip drive root */
440 p
= _tcschr(p
, _T('\\'));
441 n
= p
? p
++ - DirPath
: _tcslen(DirPath
);
442 _tcsncpy(path
, DirPath
, n
);
444 if( !CreateDirectory(path
, NULL
) &&
445 (GetLastError() != ERROR_ALREADY_EXISTS
))
448 if (GetLastError() == ERROR_ALREADY_EXISTS
)
449 SetLastError(ERROR_SUCCESS
);
458 INT
cmd_mkdir (LPTSTR cmd
, LPTSTR param
)
460 LPTSTR dir
; /* pointer to the directory to change to */
461 LPTSTR place
; /* used to search for the \ when no space is used */
465 if (!_tcsncmp (param
, _T("/?"), 2))
467 ConOutResPaging(TRUE
,STRING_MKDIR_HELP
);
472 /* check if there is no space between the command and the path */
473 if (param
[0] == _T('\0'))
475 /* search for the \ or . so that both short & long names will work */
476 for (place
= cmd
; *place
; place
++)
477 if (*place
== _T('.') || *place
== _T('\\'))
483 if (add_entry(&argc
, &p
, place
))
489 /* signal that there are no parameters */
494 p
= split (param
, &argc
, FALSE
);
497 /*JPP 20-Jul-1998 use standard error message */
498 error_too_many_parameters (param
);
508 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING
);
515 if (!MakeFullPath(dir
))
517 if(GetLastError() == ERROR_PATH_NOT_FOUND
)
519 ConErrResPuts(STRING_MD_ERROR2
);
523 ErrorMessage (GetLastError(), _T("MD"));
537 #ifdef INCLUDE_CMD_RMDIR
542 BOOL
DeleteFolder(LPTSTR FileName
)
544 TCHAR Base
[MAX_PATH
];
545 TCHAR TempFileName
[MAX_PATH
];
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
)
556 if (!_tcscmp(f
.cFileName
, _T(".")) ||
557 !_tcscmp(f
.cFileName
, _T("..")))
559 _tcscpy(TempFileName
,Base
);
560 _tcscat(TempFileName
,f
.cFileName
);
562 if(f
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
563 DeleteFolder(TempFileName
);
566 SetFileAttributes(TempFileName
,FILE_ATTRIBUTE_NORMAL
);
567 if(!DeleteFile(TempFileName
))
571 }while (FindNextFile (hFile
, &f
));
574 return RemoveDirectory(FileName
);
576 INT
cmd_rmdir (LPTSTR cmd
, LPTSTR param
)
578 TCHAR dir
[MAX_PATH
]; /* pointer to the directory to change to */
584 BOOL RD_QUIET
= FALSE
;
588 TCHAR szFullPath
[MAX_PATH
];
590 if (!_tcsncmp (param
, _T("/?"), 2))
592 ConOutResPaging(TRUE
,STRING_RMDIR_HELP
);
598 arg
= split (param
, &args
, FALSE
);
602 /* only command given */
603 error_req_param_missing ();
610 /* check for options anywhere in command line */
611 for (i
= 0; i
< args
; i
++)
613 if (*arg
[i
] == _T('/'))
615 /*found a command, but check to make sure it has something after it*/
616 if (_tcslen (arg
[i
]) == 2)
618 ch
= _totupper (arg
[i
][1]);
624 else if (ch
== _T('Q'))
632 /* get the folder name */
637 if (dir
[0] == _T('\0'))
639 /* No folder to remove */
640 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
645 GetFullPathName(dir
,MAX_PATH
,szFullPath
,NULL
);
647 /* remove trailing \ if any, but ONLY if dir is not the root dir */
648 if (_tcslen (szFullPath
) >= 2 && szFullPath
[_tcslen (szFullPath
) - 1] == _T('\\'))
649 szFullPath
[_tcslen(szFullPath
) - 1] = _T('\0');
653 /* ask if they want to delete evrything in the folder */
656 res
= FilePromptYNA (STRING_DEL_HELP2
);
657 if ((res
== PROMPT_NO
) || (res
== PROMPT_BREAK
))
668 /* check for files in the folder */
669 _tcscat(szFullPath
,_T("\\*"));
671 hFile
= FindFirstFile(szFullPath
, &f
);
672 if (hFile
!= INVALID_HANDLE_VALUE
)
676 if (!_tcscmp(f
.cFileName
,_T(".")) ||
677 !_tcscmp(f
.cFileName
,_T("..")))
679 ConOutResPuts(STRING_RMDIR_HELP2
);
684 } while (FindNextFile (hFile
, &f
));
688 szFullPath
[_tcslen(szFullPath
) - 2] = _T('\0');
691 if (!DeleteFolder(szFullPath
))
693 /* Couldnt delete the folder, clean up and print out the error */
694 ErrorMessage (GetLastError(), _T("RD"));
707 * set the exitflag to true
710 INT
CommandExit (LPTSTR cmd
, LPTSTR param
)
712 if (!_tcsncmp (param
, _T("/?"), 2))
714 ConOutResPaging(TRUE
,STRING_EXIT_HELP
);
721 if (bc
!= NULL
&& _tcsnicmp(param
,_T("/b"),2) == 0)
724 while (_istspace (*param
))
726 if (_istdigit(*param
))
727 nErrorLevel
= _ttoi(param
);
739 #ifdef INCLUDE_CMD_REM
744 INT
CommandRem (LPTSTR cmd
, LPTSTR param
)
746 if (!_tcsncmp (param
, _T("/?"), 2))
748 ConOutResPaging(TRUE
,STRING_REM_HELP
);
753 #endif /* INCLUDE_CMD_REM */
756 INT
CommandShowCommands (LPTSTR cmd
, LPTSTR param
)
762 INT
CommandShowCommandsDetail (LPTSTR cmd
, LPTSTR param
)
764 /* If a param was send, display help of correspondent command */
767 LPTSTR NewCommand
= cmd_alloc((_tcslen(param
)+4)*sizeof(TCHAR
));
768 _tcscpy(NewCommand
, param
);
769 _tcscat(NewCommand
, _T(" /?"));
770 DoCommand(NewCommand
);
771 cmd_free(NewCommand
);
773 /* Else, display detailed commands list */
776 PrintCommandListDetail ();