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
)
155 INT
cmd_chdir (LPTSTR cmd
, LPTSTR param
)
157 LPTSTR dir
; /* pointer to the directory to change to */
159 LPTSTR endofstring
; /* pointer to the null character in the directory to change to */
160 LPTSTR lastquote
; /* pointer to the last quotation mark in the directory to change to */
162 /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
163 but on the other hand paths are generally not very long*/
165 if (!_tcsncmp (param
, _T("/?"), 2))
167 ConOutResPuts(STRING_CD_HELP
);
171 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
172 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
173 post-processing of it after the function call (what would that accomplish?) */
176 endofstring
=dir
+_tcslen(dir
);
178 while ((lastquote
= _tcsrchr(dir
, _T('\"'))))
181 memmove(lastquote
,lastquote
+1,endofstring
-lastquote
);
182 *endofstring
=_T('\0');
185 /* if doing a CD and no parameters given, print out current directory */
188 TCHAR szPath
[MAX_PATH
];
190 GetCurrentDirectory (MAX_PATH
, szPath
);
195 if (dir
&& _tcslen (dir
) == 1 && *dir
== _T('-'))
202 else if (dir
&& _tcslen (dir
)==2 && dir
[1] == _T(':'))
204 TCHAR szRoot
[3] = _T("A:");
205 TCHAR szPath
[MAX_PATH
];
207 szRoot
[0] = _totupper (dir
[0]);
208 GetFullPathName (szRoot
, MAX_PATH
, szPath
, NULL
);
210 /* PathRemoveBackslash */
211 if (_tcslen (szPath
) > 3)
213 LPTSTR p
= _tcsrchr (szPath
, _T('\\'));
223 /* remove trailing \ if any, but ONLY if dir is not the root dir */
224 if (_tcslen (dir
) > 3 && dir
[_tcslen (dir
) - 1] == _T('\\'))
225 dir
[_tcslen(dir
) - 1] = _T('\0');
228 /* store current directory */
229 lpOldPath
= (LPTSTR
)malloc (MAX_PATH
* sizeof(TCHAR
));
230 GetCurrentDirectory (MAX_PATH
, lpOldPath
);
232 if (!SetCurrentDirectory (dir
))
234 //ErrorMessage (GetLastError(), _T("CD"));
235 ConOutFormatMessage(GetLastError());
237 /* throw away current directory */
245 GetCurrentDirectory(MAX_PATH
, dir
);
246 if (dir
[0]!=lpOldPath
[0])
248 SetCurrentDirectory(lpOldPath
);
255 lpLastPath
= lpOldPath
;
266 #ifdef INCLUDE_CMD_MKDIR
271 INT
cmd_mkdir (LPTSTR cmd
, LPTSTR param
)
273 LPTSTR dir
; /* pointer to the directory to change to */
274 LPTSTR place
; /* used to search for the \ when no space is used */
278 if (!_tcsncmp (param
, _T("/?"), 2))
280 ConOutResPuts(STRING_MKDIR_HELP
);
285 /* check if there is no space between the command and the path */
286 if (param
[0] == _T('\0'))
288 /* search for the \ or . so that both short & long names will work */
289 for (place
= cmd
; *place
; place
++)
290 if (*place
== _T('.') || *place
== _T('\\'))
296 /* signal that there are no parameters */
301 p
= split (param
, &argc
, FALSE
);
304 /*JPP 20-Jul-1998 use standard error message */
305 error_too_many_parameters (param
);
315 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING
);
319 /* remove trailing \ if any, but ONLY if dir is not the root dir */
320 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
321 dir
[_tcslen(dir
) - 1] = _T('\0');
323 if (!CreateDirectory (dir
, NULL
))
325 ErrorMessage (GetLastError(), _T("MD"));
338 #ifdef INCLUDE_CMD_RMDIR
343 INT
cmd_rmdir (LPTSTR cmd
, LPTSTR param
)
345 LPTSTR dir
; /* pointer to the directory to change to */
346 LPTSTR place
; /* used to search for the \ when no space is used */
351 if (!_tcsncmp (param
, _T("/?"), 2))
353 ConOutResPuts(STRING_RMDIR_HELP
);
357 /* check if there is no space between the command and the path */
358 if (param
[0] == _T('\0'))
360 /* search for the \ or . so that both short & long names will work */
361 for (place
= cmd
; *place
; place
++)
362 if (*place
== _T('.') || *place
== _T('\\'))
368 /* signal that there are no parameters */
373 p
= split (param
, &argc
, FALSE
);
376 /*JPP 20-Jul-1998 use standard error message */
377 error_too_many_parameters (param
);
387 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
391 /* remove trailing \ if any, but ONLY if dir is not the root dir */
392 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
393 dir
[_tcslen(dir
) - 1] = _T('\0');
395 if (!RemoveDirectory (dir
))
397 ErrorMessage (GetLastError(), _T("RD"));
411 * set the exitflag to true
414 INT
CommandExit (LPTSTR cmd
, LPTSTR param
)
416 if (!_tcsncmp (param
, _T("/?"), 2))
418 ConOutResPuts(STRING_EXIT_HELP
);
427 #ifdef INCLUDE_CMD_REM
432 INT
CommandRem (LPTSTR cmd
, LPTSTR param
)
434 if (!_tcsncmp (param
, _T("/?"), 2))
436 ConOutResPuts(STRING_REM_HELP
);
441 #endif /* INCLUDE_CMD_REM */
444 INT
CommandShowCommands (LPTSTR cmd
, LPTSTR param
)