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.
132 #include "resource.h"
134 #ifdef INCLUDE_CMD_CHDIR
136 static LPTSTR lpLastPath
;
139 VOID
InitLastPath (VOID
)
145 VOID
FreeLastPath (VOID
)
151 /* help functions for getting current path from drive
152 without changing drive. Return code 0 = ok, 1 = fail.
153 INT GetRootPath("C:",outbuffer,chater size of outbuffer);
154 the first param can have any size, if the the two frist
155 letter are not a drive with : it will get Currentpath on
156 current drive exacly as GetCurrentDirectory does.
159 INT
GetRootPath(TCHAR
*InPath
,TCHAR
*OutPath
,INT size
)
164 if (_tcslen(InPath
)>1)
166 if (InPath
[1]==_T(':'))
170 if (_tgetdcwd(t
,OutPath
,size
) != NULL
)
172 if (_tcsncicmp(InPath
,OutPath
,2))
182 /* fail to getting path devic did not exists */
183 if (_tcslen(InPath
)>1)
185 if (InPath
[1]==_T(':'))
189 retcode
= GetCurrentDirectory(size
,OutPath
);
201 INT
cmd_chdir (LPTSTR cmd
, LPTSTR param
)
203 LPTSTR dir
; /* pointer to the directory to change to */
205 size_t size
, str_len
;
206 WIN32_FIND_DATA FileData
;
209 BOOL fFinished
= FALSE
;
212 /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
213 but on the other hand paths are generally not very long*/
215 if (!_tcsncmp (param
, _T("/?"), 2))
217 ConOutResPaging(TRUE
,STRING_CD_HELP
);
223 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
224 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
225 post-processing of it after the function call (what would that accomplish?) */
227 size
= _tcscspn(param
, _T("\"") );
228 str_len
= _tcslen(param
)-1;
230 if ((param
[size
] == _T('"')) && (str_len
>1))
235 _tcsncpy(param
,¶m
[size
+1],str_len
);
236 param
[str_len
] = _T('\0');
239 size
= _tcscspn(param
, _T("\"") );
240 if (param
[size
] == _T('"'))
242 param
[size
] = _T('\0');
247 str_len
= _tcslen(param
);
250 if (param
[0] == _T('*'))
258 /* if doing a CD and no parameters given, print out current directory */
261 TCHAR szPath
[MAX_PATH
];
263 GetCurrentDirectory (MAX_PATH
, szPath
);
268 if (dir
&& _tcslen (dir
) == 1 && *dir
== _T('-'))
275 else if (dir
&& _tcslen (dir
)==2 && dir
[1] == _T(':'))
277 TCHAR szRoot
[3] = _T("A:");
278 TCHAR szPath
[MAX_PATH
];
280 szRoot
[0] = _totupper (dir
[0]);
281 GetFullPathName (szRoot
, MAX_PATH
, szPath
, NULL
);
283 /* PathRemoveBackslash */
284 if (_tcslen (szPath
) > 3)
286 LPTSTR p
= _tcsrchr (szPath
, _T('\\'));
296 /* remove trailing \ if any, but ONLY if dir is not the root dir */
297 if (_tcslen (dir
) > 3 && dir
[_tcslen (dir
) - 1] == _T('\\'))
298 dir
[_tcslen(dir
) - 1] = _T('\0');
301 /* store current directory */
302 lpOldPath
= (LPTSTR
)malloc (MAX_PATH
* sizeof(TCHAR
));
303 GetCurrentDirectory (MAX_PATH
, lpOldPath
);
305 if (!SetCurrentDirectory (dir
))
308 hSearch
= FindFirstFile(dir
, &FileData
);
309 if (hSearch
== INVALID_HANDLE_VALUE
)
311 ConOutFormatMessage(GetLastError());
321 dwAttrs
= GetFileAttributes(FileData
.cFileName
);
323 DebugPrintf(_T("Search found folder :%s\n"),FileData
.cFileName
);
325 if ((dwAttrs
& FILE_ATTRIBUTE_DIRECTORY
))
329 if (!SetCurrentDirectory (FileData
.cFileName
))
331 ConOutFormatMessage(GetLastError());
342 else if (!FindNextFile(hSearch
, &FileData
))
345 ConOutFormatMessage(GetLastError());
353 //ErrorMessage (GetLastError(), _T("CD"));
354 ConOutFormatMessage(GetLastError());
357 /* throw away current directory */
365 GetCurrentDirectory(MAX_PATH
, dir
);
366 if (dir
[0]!=lpOldPath
[0])
368 SetCurrentDirectory(lpOldPath
);
375 lpLastPath
= lpOldPath
;
386 #ifdef INCLUDE_CMD_MKDIR
391 INT
cmd_mkdir (LPTSTR cmd
, LPTSTR param
)
393 LPTSTR dir
; /* pointer to the directory to change to */
394 LPTSTR place
; /* used to search for the \ when no space is used */
398 if (!_tcsncmp (param
, _T("/?"), 2))
400 ConOutResPaging(TRUE
,STRING_MKDIR_HELP
);
405 /* check if there is no space between the command and the path */
406 if (param
[0] == _T('\0'))
408 /* search for the \ or . so that both short & long names will work */
409 for (place
= cmd
; *place
; place
++)
410 if (*place
== _T('.') || *place
== _T('\\'))
416 /* signal that there are no parameters */
421 p
= split (param
, &argc
, FALSE
);
424 /*JPP 20-Jul-1998 use standard error message */
425 error_too_many_parameters (param
);
435 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING
);
439 /* remove trailing \ if any, but ONLY if dir is not the root dir */
440 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
441 dir
[_tcslen(dir
) - 1] = _T('\0');
443 if (!CreateDirectory (dir
, NULL
))
445 ErrorMessage (GetLastError(), _T("MD"));
458 #ifdef INCLUDE_CMD_RMDIR
463 INT
cmd_rmdir (LPTSTR cmd
, LPTSTR param
)
465 LPTSTR dir
; /* pointer to the directory to change to */
466 LPTSTR place
; /* used to search for the \ when no space is used */
471 if (!_tcsncmp (param
, _T("/?"), 2))
473 ConOutResPaging(TRUE
,STRING_RMDIR_HELP
);
477 /* check if there is no space between the command and the path */
478 if (param
[0] == _T('\0'))
480 /* search for the \ or . so that both short & long names will work */
481 for (place
= cmd
; *place
; place
++)
482 if (*place
== _T('.') || *place
== _T('\\'))
488 /* signal that there are no parameters */
493 p
= split (param
, &argc
, FALSE
);
496 /*JPP 20-Jul-1998 use standard error message */
497 error_too_many_parameters (param
);
507 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
511 /* remove trailing \ if any, but ONLY if dir is not the root dir */
512 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
513 dir
[_tcslen(dir
) - 1] = _T('\0');
515 if (!RemoveDirectory (dir
))
517 ErrorMessage (GetLastError(), _T("RD"));
531 * set the exitflag to true
534 INT
CommandExit (LPTSTR cmd
, LPTSTR param
)
536 if (!_tcsncmp (param
, _T("/?"), 2))
537 ConOutResPaging(TRUE
,STRING_EXIT_HELP
);
539 if (bc
!= NULL
&& _tcsnicmp(param
,_T("/b"),2) == 0)
542 while (_istspace (*param
))
544 if (_istdigit(*param
))
545 nErrorLevel
= _ttoi(param
);
557 #ifdef INCLUDE_CMD_REM
562 INT
CommandRem (LPTSTR cmd
, LPTSTR param
)
564 if (!_tcsncmp (param
, _T("/?"), 2))
566 ConOutResPaging(TRUE
,STRING_REM_HELP
);
571 #endif /* INCLUDE_CMD_REM */
574 INT
CommandShowCommands (LPTSTR cmd
, LPTSTR param
)
580 INT
CommandShowCommandsDetail (LPTSTR cmd
, LPTSTR param
)
582 PrintCommandListDetail ();