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 /* helper functions for getting current path from drive
145 without changing drive. Return code 0 = ok, 1 = fail.
146 INT GetRootPath("C:",outbuffer,chater size of outbuffer);
147 the first param can have any size, if the the two frist
148 letter are not a drive with : it will get Currentpath on
149 current drive exactly as GetCurrentDirectory does.
152 INT
GetRootPath(TCHAR
*InPath
,TCHAR
*OutPath
,INT size
)
154 if (InPath
[0] && InPath
[1] == _T(':'))
158 if ((InPath
[0] >= _T('0')) && (InPath
[0] <= _T('9')))
160 t
= (InPath
[0] - _T('0')) +28;
163 if ((InPath
[0] >= _T('a')) && (InPath
[0] <= _T('z')))
165 t
= (InPath
[0] - _T('a')) +1;
166 InPath
[0] = t
+ _T('A') - 1;
169 if ((InPath
[0] >= _T('A')) && (InPath
[0] <= _T('Z')))
171 t
= (InPath
[0] - _T('A')) +1;
174 return _tgetdcwd(t
,OutPath
,size
) == NULL
;
177 /* Get current directory */
178 return !GetCurrentDirectory(size
,OutPath
);
182 BOOL
SetRootPath(TCHAR
*oldpath
, TCHAR
*InPath
)
184 TCHAR OutPath
[MAX_PATH
];
185 TCHAR OutPathTemp
[MAX_PATH
];
187 /* Retrieve the full path name from the (possibly relative) InPath */
188 if (GetFullPathName(InPath
, MAX_PATH
, OutPathTemp
, NULL
) == 0)
191 /* Convert the full path to its correct case.
192 * Example: c:\windows\SYSTEM32 => C:\WINDOWS\System32 */
193 GetPathCase(OutPathTemp
, OutPath
);
195 /* Use _tchdir, since unlike SetCurrentDirectory it updates
196 * the current-directory-on-drive environment variables. */
197 if (_tchdir(OutPath
) != 0)
200 /* Keep original drive in ordinary CD/CHDIR (without /D switch). */
201 if (oldpath
!= NULL
&& _tcsncicmp(OutPath
, oldpath
, 2) != 0)
202 SetCurrentDirectory(oldpath
);
207 ConErrFormatMessage(GetLastError());
217 INT
cmd_chdir (LPTSTR param
)
219 BOOL bChangeDrive
= FALSE
;
221 TCHAR szCurrent
[MAX_PATH
];
223 /* Filter out special cases first */
226 if (!_tcsncmp(param
, _T("/?"), 2))
228 ConOutResPaging(TRUE
,STRING_CD_HELP
);
232 /* Remove extra quotes and strip trailing whitespace */
234 tmp
= param
+ _tcslen(param
) - 1;
235 while (tmp
> param
&& _istspace(*tmp
))
237 *(tmp
+ 1) = _T('\0');
239 /* Set Error Level to Success */
242 /* Print Current Directory on a disk */
243 if (_tcslen(param
) == 2 && param
[1] == _T(':'))
245 if (GetRootPath(param
, szCurrent
, MAX_PATH
))
247 error_invalid_drive();
250 ConOutPrintf(_T("%s\n"), szCurrent
);
254 /* Get Current Directory */
255 GetCurrentDirectory(MAX_PATH
, szCurrent
);
256 if (param
[0] == _T('\0'))
258 ConOutPrintf(_T("%s\n"), szCurrent
);
262 /* Input String Contains /D Switch */
263 if (!_tcsncicmp(param
, _T("/D"), 2))
267 while (_istspace(*param
))
271 if (!SetRootPath(bChangeDrive
? NULL
: szCurrent
, param
))
282 #ifdef INCLUDE_CMD_MKDIR
284 /* Helper function for mkdir to make directories in a path.
285 Don't use the api to decrease dependence on libs */
287 MakeFullPath(TCHAR
* DirPath
)
289 TCHAR path
[MAX_PATH
];
293 if (CreateDirectory(DirPath
, NULL
))
295 else if (GetLastError() != ERROR_PATH_NOT_FOUND
)
298 /* got ERROR_PATH_NOT_FOUND, so try building it up one component at a time */
299 if (p
[0] && p
[1] == _T(':'))
301 while (*p
== _T('\\'))
302 p
++; /* skip drive root */
305 p
= _tcschr(p
, _T('\\'));
306 n
= p
? p
++ - DirPath
: _tcslen(DirPath
);
307 _tcsncpy(path
, DirPath
, n
);
309 if ( !CreateDirectory(path
, NULL
) &&
310 (GetLastError() != ERROR_ALREADY_EXISTS
))
322 INT
cmd_mkdir (LPTSTR param
)
326 if (!_tcsncmp (param
, _T("/?"), 2))
328 ConOutResPaging(TRUE
,STRING_MKDIR_HELP
);
332 p
= split (param
, &argc
, FALSE
, FALSE
);
335 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
342 for (i
= 0; i
< argc
; i
++)
344 if (!MakeFullPath(p
[i
]))
346 if (GetLastError() == ERROR_PATH_NOT_FOUND
)
348 ConErrResPuts(STRING_MD_ERROR2
);
352 ErrorMessage (GetLastError(), _T("MD"));
364 #ifdef INCLUDE_CMD_RMDIR
368 BOOL
DeleteFolder(LPTSTR FileName
)
370 TCHAR Base
[MAX_PATH
];
371 TCHAR TempFileName
[MAX_PATH
];
374 _tcscpy(Base
,FileName
);
375 _tcscat(Base
,_T("\\*"));
376 hFile
= FindFirstFile(Base
, &f
);
377 Base
[_tcslen(Base
) - 1] = _T('\0');
378 if (hFile
!= INVALID_HANDLE_VALUE
)
382 if (!_tcscmp(f
.cFileName
, _T(".")) ||
383 !_tcscmp(f
.cFileName
, _T("..")))
385 _tcscpy(TempFileName
,Base
);
386 _tcscat(TempFileName
,f
.cFileName
);
388 if (f
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
389 DeleteFolder(TempFileName
);
392 SetFileAttributes(TempFileName
,FILE_ATTRIBUTE_NORMAL
);
393 if (!DeleteFile(TempFileName
))
400 }while (FindNextFile (hFile
, &f
));
403 return RemoveDirectory(FileName
);
406 INT
cmd_rmdir (LPTSTR param
)
414 BOOL RD_QUIET
= FALSE
;
417 TCHAR szFullPath
[MAX_PATH
];
419 if (!_tcsncmp (param
, _T("/?"), 2))
421 ConOutResPaging(TRUE
,STRING_RMDIR_HELP
);
425 arg
= split (param
, &args
, FALSE
, FALSE
);
428 /* check for options anywhere in command line */
429 for (i
= 0; i
< args
; i
++)
431 if (*arg
[i
] == _T('/'))
433 /*found a command, but check to make sure it has something after it*/
434 if (_tcslen (arg
[i
]) == 2)
436 ch
= _totupper (arg
[i
][1]);
442 else if (ch
== _T('Q'))
456 /* No folder to remove */
457 error_req_param_missing();
462 for (i
= 0; i
< args
; i
++)
464 if (*arg
[i
] == _T('/'))
469 /* ask if they want to delete everything in the folder */
472 res
= FilePromptYNA (STRING_DEL_HELP2
);
473 if (res
== PROMPT_NO
|| res
== PROMPT_BREAK
)
478 if (res
== PROMPT_ALL
)
481 /* get the folder name */
482 GetFullPathName(arg
[i
],MAX_PATH
,szFullPath
,NULL
);
484 /* remove trailing \ if any, but ONLY if dir is not the root dir */
485 if (_tcslen (szFullPath
) >= 2 && szFullPath
[_tcslen (szFullPath
) - 1] == _T('\\'))
486 szFullPath
[_tcslen(szFullPath
) - 1] = _T('\0');
488 res
= DeleteFolder(szFullPath
);
492 res
= RemoveDirectory(arg
[i
]);
497 /* Couldn't delete the folder, print out the error */
498 nError
= GetLastError();
499 ErrorMessage(nError
, _T("RD"));
510 * set the exitflag to true
512 INT
CommandExit(LPTSTR param
)
514 if (!_tcsncmp(param
, _T("/?"), 2))
516 ConOutResPaging(TRUE
, STRING_EXIT_HELP
);
518 /* Just make sure we don't exit */
523 if (_tcsnicmp(param
, _T("/b"), 2) == 0)
528 * If a current batch file is running, exit it,
529 * otherwise exit this command interpreter instance.
538 /* Exit this command interpreter instance */
542 /* Search for an optional exit code */
543 while (_istspace(*param
))
546 /* Set the errorlevel to the exit code */
547 if (_istdigit(*param
))
548 nErrorLevel
= _ttoi(param
);
553 #ifdef INCLUDE_CMD_REM
557 INT
CommandRem (LPTSTR param
)
559 if (!_tcsncmp (param
, _T("/?"), 2))
561 ConOutResPaging(TRUE
,STRING_REM_HELP
);
566 #endif /* INCLUDE_CMD_REM */
569 INT
CommandShowCommands (LPTSTR param
)