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.
135 extern COMMAND cmds
[]; /* The internal command table, used in '?' */
138 #ifdef INCLUDE_CMD_CHDIR
140 static LPTSTR lpLastPath
;
143 VOID
InitLastPath (VOID
)
149 VOID
FreeLastPath (VOID
)
160 INT
cmd_chdir (LPTSTR cmd
, LPTSTR param
)
162 LPTSTR dir
; /* pointer to the directory to change to */
163 LPTSTR place
; /* used to search for the \ when no space is used */
168 if (!_tcsncmp (param
, _T("/?"), 2))
170 ConOutPuts (_T("Changes the current directory or displays it's name\n\n"
171 "CHDIR [drive:][path]\n"
173 "CD [drive:][path]\n"
175 " .. parent directory\n"
176 " - previous directory\n\n"
177 "Type CD drive: to display the current directory on the specified drive.\n"
178 "Type CD without a parameter to display the current drive and directory."));
182 /* check if there is no space between the command and the path */
183 if (param
[0] == _T('\0'))
185 /* search for the '\', '.' or '-' so that both short & long names will work */
186 for (place
= cmd
; *place
; place
++)
187 if (*place
== _T('.') || *place
== _T('\\') || *place
== _T('-'))
193 /* signal that there are no parameters */
198 arg
= split (param
, &argc
);
202 /*JPP 20-Jul-1998 use standard error message */
203 error_too_many_parameters (param
);
211 /* if doing a CD and no parameters given, print out current directory */
214 TCHAR szPath
[MAX_PATH
];
216 GetCurrentDirectory (MAX_PATH
, szPath
);
225 if (dir
&& _tcslen (dir
) == 1 && *dir
== _T('-'))
235 else if (dir
&& _tcslen (dir
) > 1 && dir
[1] == _T(':'))
237 TCHAR szRoot
[3] = _T("A:");
238 TCHAR szPath
[MAX_PATH
];
240 szRoot
[0] = _totupper (dir
[0]);
241 GetFullPathName (szRoot
, MAX_PATH
, szPath
, NULL
);
243 /* PathRemoveBackslash */
244 if (_tcslen (szPath
) > 3)
246 LPTSTR p
= _tcsrchr (szPath
, _T('\\'));
257 /* remove trailing \ if any, but ONLY if dir is not the root dir */
258 if (_tcslen (dir
) > 3 && dir
[_tcslen (dir
) - 1] == _T('\\'))
259 dir
[_tcslen(dir
) - 1] = _T('\0');
262 /* store current directory */
263 lpOldPath
= (LPTSTR
)malloc (MAX_PATH
* sizeof(TCHAR
));
264 GetCurrentDirectory (MAX_PATH
, lpOldPath
);
266 if (!SetCurrentDirectory (dir
))
268 ErrorMessage (GetLastError(), _T("CD"));
270 /* throw away current directory */
281 lpLastPath
= lpOldPath
;
292 #ifdef INCLUDE_CMD_MKDIR
297 INT
cmd_mkdir (LPTSTR cmd
, LPTSTR param
)
299 LPTSTR dir
; /* pointer to the directory to change to */
300 LPTSTR place
; /* used to search for the \ when no space is used */
305 if (!_tcsncmp (param
, _T("/?"), 2))
307 ConOutPuts (_T("Creates a directory.\n\n"
308 "MKDIR [drive:]path\nMD [drive:]path"));
313 /* check if there is no space between the command and the path */
314 if (param
[0] == _T('\0'))
316 /* search for the \ or . so that both short & long names will work */
317 for (place
= cmd
; *place
; place
++)
318 if (*place
== _T('.') || *place
== _T('\\'))
324 /* signal that there are no parameters */
329 p
= split (param
, &argc
);
332 /*JPP 20-Jul-1998 use standard error message */
333 error_too_many_parameters (param
);
343 ConErrPrintf (_T("Required parameter missing\n"));
347 /* remove trailing \ if any, but ONLY if dir is not the root dir */
348 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
349 dir
[_tcslen(dir
) - 1] = _T('\0');
351 if (!CreateDirectory (dir
, NULL
))
353 ErrorMessage (GetLastError(), _T("MD"));
366 #ifdef INCLUDE_CMD_RMDIR
371 INT
cmd_rmdir (LPTSTR cmd
, LPTSTR param
)
373 LPTSTR dir
; /* pointer to the directory to change to */
374 LPTSTR place
; /* used to search for the \ when no space is used */
379 if (!_tcsncmp (param
, _T("/?"), 2))
381 ConOutPuts (_T("Removes a directory.\n\n"
382 "RMDIR [drive:]path\nRD [drive:]path"));
386 /* check if there is no space between the command and the path */
387 if (param
[0] == _T('\0'))
389 /* search for the \ or . so that both short & long names will work */
390 for (place
= cmd
; *place
; place
++)
391 if (*place
== _T('.') || *place
== _T('\\'))
397 /* signal that there are no parameters */
402 p
= split (param
, &argc
);
405 /*JPP 20-Jul-1998 use standard error message */
406 error_too_many_parameters (param
);
416 ConErrPrintf (_T("Required parameter missing\n"));
420 /* remove trailing \ if any, but ONLY if dir is not the root dir */
421 if (_tcslen (dir
) >= 2 && dir
[_tcslen (dir
) - 1] == _T('\\'))
422 dir
[_tcslen(dir
) - 1] = _T('\0');
424 if (!RemoveDirectory (dir
))
426 ErrorMessage (GetLastError(), _T("RD"));
440 * set the exitflag to true
443 INT
internal_exit (LPTSTR cmd
, LPTSTR param
)
445 if (!_tcsncmp (param
, _T("/?"), 2))
447 ConOutPuts (_T("Exits the command line interpreter.\n\nEXIT"));
458 #ifdef INCLUDE_CMD_REM
463 VOID
CommandRem (LPTSTR cmd
, LPTSTR param
)
465 if (!_tcsncmp (param
, _T("/?"), 2))
467 ConOutPuts (_T("Starts a comment line in a batch file.\n\n"
473 #endif /* INCLUDE_CMD_REM */
476 VOID
CommandShowCommands (LPTSTR cmd
, LPTSTR param
)
487 ConOutPuts (cmdptr
->name
);
491 ConOutPrintf (_T("%-10s"), cmdptr
->name
);
497 ConOutChar (_T('\n'));