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 <ekohl@abo.rhein-zeitung.de>)
101 * Replaced DOS calls by Win32 calls.
103 * 08-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
104 * Added help texts ("/?").
106 * 18-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
107 * Added support for quoted arguments (cd "program files").
109 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
112 * 26-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
113 * Replaced remaining CRT io functions by Win32 io functions.
116 * 30-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
117 * Added "cd -" feature. Changes to the previous directory.
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.
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
138 #include "resource.h"
140 #ifdef INCLUDE_CMD_CHDIR
142 static LPTSTR lpLastPath
;
145 VOID
InitLastPath (VOID
)
151 VOID
FreeLastPath (VOID
)
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.
165 INT
GetRootPath(TCHAR
*InPath
,TCHAR
*OutPath
,INT size
)
169 if (_tcslen(InPath
)>1)
171 if (InPath
[1]==_T(':'))
175 if ((InPath
[0] >= _T('0')) && (InPath
[0] <= _T('9')))
177 t
= (InPath
[0] - _T('0')) +28;
180 if ((InPath
[0] >= _T('a')) && (InPath
[0] <= _T('z')))
182 t
= (InPath
[0] - _T('a')) +1;
183 InPath
[0] = t
+ _T('A') - 1;
186 if ((InPath
[0] >= _T('A')) && (InPath
[0] <= _T('Z')))
188 t
= (InPath
[0] - _T('A')) +1;
191 if (_tgetdcwd(t
,OutPath
,size
) != NULL
)
199 if (_tcslen(InPath
)>1)
201 if (InPath
[1]==_T(':'))
205 /* Get current directory */
206 retcode
= GetCurrentDirectory(size
,OutPath
);
214 BOOL
SetRootPath(TCHAR
*InPath
)
216 TCHAR oldpath
[MAX_PATH
];
217 TCHAR OutPath
[MAX_PATH
];
218 TCHAR OutPathTemp
[MAX_PATH
];
219 TCHAR OutPathTemp2
[MAX_PATH
];
223 /* Get The current directory path and save it */
224 fail
= GetCurrentDirectory(MAX_PATH
,oldpath
);
228 /* Get current drive directory path if C: was only pass down*/
230 if (_tcsncicmp(&InPath
[1],_T(":\\"),2)!=0)
232 if (!GetRootPath(InPath
,OutPathTemp
,MAX_PATH
))
233 _tcscpy(OutPathTemp
,InPath
);
237 _tcscpy(OutPathTemp
,InPath
);
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 GetShortPathName(OutPathTemp
, OutPathTemp2
, MAX_PATH
);
245 GetLongPathName(OutPathTemp2
, OutPath
, MAX_PATH
);
247 fail
= SetCurrentDirectory(OutPath
);
253 SetCurrentDirectory(OutPath
);
254 GetCurrentDirectory(MAX_PATH
,OutPath
);
257 if (_tcsncicmp(OutPath
,oldpath
,2)!=0)
258 SetCurrentDirectory(oldpath
);
268 INT
cmd_chdir (LPTSTR cmd
, LPTSTR param
)
273 BOOL bChangeDrive
= FALSE
;
274 TCHAR szPath
[MAX_PATH
];
275 TCHAR szFinalPath
[MAX_PATH
];
277 TCHAR szCurrent
[MAX_PATH
];
278 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
282 /* Filter out special cases first */
285 if (!_tcsncmp(param
, _T("/?"), 2))
287 ConOutResPaging(TRUE
,STRING_CD_HELP
);
291 /* Set Error Level to Success */
294 /* Input String Contains /D Switch */
295 if (!_tcsncicmp(param
, _T("/D"), 2))
298 tmpPath
= _tcsstr(param
,_T(" "));
300 _tcscpy(szPath
,tmpPath
);
305 _tcscpy(szPath
,param
);
308 /* Print Current Directory on a disk */
309 if (_tcslen(szPath
) == 2 && szPath
[1] == _T(':'))
311 if(GetRootPath(szPath
,szCurrent
,MAX_PATH
))
316 ConOutPuts(szCurrent
);
320 /* Get Current Directory */
321 GetRootPath(_T("."),szCurrent
,MAX_PATH
);
325 while(i
< _tcslen(szPath
))
327 if(szPath
[i
] == _T('\"'))
328 memmove(&szPath
[i
],&szPath
[i
+ 1], _tcslen(&szPath
[i
]) * sizeof(TCHAR
));
334 while (_istspace (*tmpPath
))
336 _tcscpy(szPath
,tmpPath
);
338 if (szPath
[0] == _T('\0'))
340 ConOutPuts(szCurrent
);
345 /* change to full path if relative path was given */
346 GetFullPathName(szPath
,MAX_PATH
,szFinalPath
,NULL
);
348 if(szFinalPath
[_tcslen(szFinalPath
) - 1] == _T('\\') && _tcslen(szFinalPath
) > 3)
349 szFinalPath
[_tcslen(szFinalPath
) - 1] = _T('\0');
351 /* Handle Root Directory Alone*/
352 if (_tcslen(szFinalPath
) == 3 && szFinalPath
[1] == _T(':'))
354 if(!SetRootPath(szFinalPath
))
356 /* Change prompt if it is one the same drive or /D */
357 if(bChangeDrive
|| !_tcsncicmp(szFinalPath
,szCurrent
,1))
358 SetCurrentDirectory(szFinalPath
);
361 /* Didnt find an directories */
362 LoadString(CMD_ModuleHandle
, STRING_ERROR_PATH_NOT_FOUND
, szMsg
, RC_STRING_MAX_SIZE
);
369 /* Get a list of all the files */
370 hFile
= FindFirstFile (szFinalPath
, &f
);
374 if(hFile
== INVALID_HANDLE_VALUE
)
376 ConErrFormatMessage (GetLastError(), szFinalPath
);
381 /* Strip the paths back to the folder they are in */
382 for(i
= (_tcslen(szFinalPath
) - 1); i
> -1; i
--)
383 if(szFinalPath
[i
] != _T('\\'))
384 szFinalPath
[i
] = _T('\0');
388 _tcscat(szFinalPath
,f
.cFileName
);
390 if ((f
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
)
392 if(!SetRootPath(szFinalPath
))
397 _tcsupr(szFinalPath
);
398 GetLongPathName(szFinalPath
, szPath
, MAX_PATH
);
399 SetCurrentDirectory(szPath
);
405 }while(FindNextFile (hFile
, &f
));
407 /* Didnt find an directories */
408 LoadString(CMD_ModuleHandle
, STRING_ERROR_PATH_NOT_FOUND
, szMsg
, RC_STRING_MAX_SIZE
);
418 #ifdef INCLUDE_CMD_MKDIR
423 INT
cmd_mkdir (LPTSTR cmd
, LPTSTR param
)
425 LPTSTR dir
; /* pointer to the directory to change to */
426 LPTSTR place
; /* used to search for the \ when no space is used */
430 if (!_tcsncmp (param
, _T("/?"), 2))
432 ConOutResPaging(TRUE
,STRING_MKDIR_HELP
);
437 /* check if there is no space between the command and the path */
438 if (param
[0] == _T('\0'))
440 /* search for the \ or . so that both short & long names will work */
441 for (place
= cmd
; *place
; place
++)
442 if (*place
== _T('.') || *place
== _T('\\'))
448 /* signal that there are no parameters */
453 p
= split (param
, &argc
, FALSE
);
456 /*JPP 20-Jul-1998 use standard error message */
457 error_too_many_parameters (param
);
467 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING
);
471 /* remove trailing \ if any, but ONLY if dir is not the root dir */
472 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
473 dir
[_tcslen(dir
) - 1] = _T('\0');
475 if (!CreateDirectory (dir
, NULL
))
477 ErrorMessage (GetLastError(), _T("MD"));
490 #ifdef INCLUDE_CMD_RMDIR
495 INT
cmd_rmdir (LPTSTR cmd
, LPTSTR param
)
497 LPTSTR dir
; /* pointer to the directory to change to */
498 LPTSTR place
; /* used to search for the \ when no space is used */
503 if (!_tcsncmp (param
, _T("/?"), 2))
505 ConOutResPaging(TRUE
,STRING_RMDIR_HELP
);
509 /* check if there is no space between the command and the path */
510 if (param
[0] == _T('\0'))
512 /* search for the \ or . so that both short & long names will work */
513 for (place
= cmd
; *place
; place
++)
514 if (*place
== _T('.') || *place
== _T('\\'))
520 /* signal that there are no parameters */
525 p
= split (param
, &argc
, FALSE
);
528 /*JPP 20-Jul-1998 use standard error message */
529 error_too_many_parameters (param
);
539 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
543 /* remove trailing \ if any, but ONLY if dir is not the root dir */
544 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
545 dir
[_tcslen(dir
) - 1] = _T('\0');
547 if (!RemoveDirectory (dir
))
549 ErrorMessage (GetLastError(), _T("RD"));
563 * set the exitflag to true
566 INT
CommandExit (LPTSTR cmd
, LPTSTR param
)
568 if (!_tcsncmp (param
, _T("/?"), 2))
569 ConOutResPaging(TRUE
,STRING_EXIT_HELP
);
571 if (bc
!= NULL
&& _tcsnicmp(param
,_T("/b"),2) == 0)
574 while (_istspace (*param
))
576 if (_istdigit(*param
))
577 nErrorLevel
= _ttoi(param
);
589 #ifdef INCLUDE_CMD_REM
594 INT
CommandRem (LPTSTR cmd
, LPTSTR param
)
596 if (!_tcsncmp (param
, _T("/?"), 2))
598 ConOutResPaging(TRUE
,STRING_REM_HELP
);
603 #endif /* INCLUDE_CMD_REM */
606 INT
CommandShowCommands (LPTSTR cmd
, LPTSTR param
)
612 INT
CommandShowCommandsDetail (LPTSTR cmd
, LPTSTR param
)
614 PrintCommandListDetail ();