Did forget the info I did change
[reactos.git] / reactos / subsys / system / cmd / internal.c
1 /*
2 * INTERNAL.C - command.com internal commands.
3 *
4 *
5 * History:
6 *
7 * 17/08/94 (Tim Norman)
8 * started.
9 *
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.
13 *
14 * cd()
15 * started.
16 *
17 * dir()
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.
22 *
23 * i have added support for directorys. now if the directory attribute is
24 * detected the file size if replaced with the string "<dir>".
25 *
26 * ver()
27 * started.
28 *
29 * md()
30 * started.
31 *
32 * rd()
33 * started.
34 *
35 * del()
36 * started.
37 *
38 * does not support wildcard selection.
39 *
40 * todo: add delete directory support.
41 * add recursive directory delete support.
42 *
43 * ren()
44 * started.
45 *
46 * does not support wildcard selection.
47 *
48 * todo: add rename directory support.
49 *
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.
53 *
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 \
58 *
59 * 1/6/96 (Tim Norman)
60 * added an isatty call to DIR so it won't prompt for keypresses unless
61 * stdin and stdout are the console.
62 *
63 * changed parameters to be mutually consistent to make calling the
64 * functions easier
65 *
66 * rem()
67 * started.
68 *
69 * doskey()
70 * started.
71 *
72 * 01/22/96 (Oliver Mueller)
73 * error messages are now handled by perror.
74 *
75 * 02/05/96 (Tim Norman)
76 * converted all functions to accept first/rest parameters
77 *
78 * 07/26/96 (Tim Norman)
79 * changed return values to int instead of void
80 *
81 * path() started.
82 *
83 * 12/23/96 (Aaron Kaufman)
84 * rewrote dir() to mimic MS-DOS's dir
85 *
86 * 01/28/97 (Tim Norman)
87 * cleaned up Aaron's DIR code
88 *
89 * 06/13/97 (Tim Norman)
90 * moved DIR code to dir.c
91 * re-implemented Aaron's DIR code
92 *
93 * 06/14/97 (Steffan Kaiser)
94 * ctrl-break handling
95 * bug fixes
96 *
97 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
98 * added config.h include
99 *
100 * 03-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
101 * Replaced DOS calls by Win32 calls.
102 *
103 * 08-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
104 * Added help texts ("/?").
105 *
106 * 18-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
107 * Added support for quoted arguments (cd "program files").
108 *
109 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
110 * Clean up.
111 *
112 * 26-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
113 * Replaced remaining CRT io functions by Win32 io functions.
114 * Unicode safe!
115 *
116 * 30-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
117 * Added "cd -" feature. Changes to the previous directory.
118 *
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.
122 *
123 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
124 * Improved chdir/cd command.
125 *
126 * 02-Apr-2004 (Magnus Olsen <magnus@greatlord.com>)
127 * Remove all hard code string so they can be
128 * translate to other langues.
129 */
130
131 #include "precomp.h"
132 #include "resource.h"
133
134 #ifdef INCLUDE_CMD_CHDIR
135
136 static LPTSTR lpLastPath;
137
138
139 VOID InitLastPath (VOID)
140 {
141 lpLastPath = NULL;
142 }
143
144
145 VOID FreeLastPath (VOID)
146 {
147 if (lpLastPath)
148 free (lpLastPath);
149 }
150
151 /*
152 * CD / CHDIR
153 *
154 */
155 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
156 {
157 LPTSTR dir; /* pointer to the directory to change to */
158 LPTSTR lpOldPath;
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 */
161 WCHAR szMsg[RC_STRING_MAX_SIZE];
162
163 /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
164 but on the other hand paths are generally not very long*/
165
166 if (!_tcsncmp (param, _T("/?"), 2))
167 {
168 LoadString( GetModuleHandle(NULL), STRING_CD_HELP, (LPTSTR) szMsg,sizeof(szMsg));
169 ConOutPuts (_T((LPTSTR)szMsg));
170 return 0;
171 }
172
173 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
174 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
175 post-processing of it after the function call (what would that accomplish?) */
176
177 dir=param;
178 endofstring=dir+_tcslen(dir);
179
180 while ((lastquote = _tcsrchr(dir, _T('\"'))))
181 {
182 endofstring--;
183 memmove(lastquote,lastquote+1,endofstring-lastquote);
184 *endofstring=_T('\0');
185 }
186
187 /* if doing a CD and no parameters given, print out current directory */
188 if (!dir || !dir[0])
189 {
190 TCHAR szPath[MAX_PATH];
191
192 GetCurrentDirectory (MAX_PATH, szPath);
193
194 ConOutPuts (szPath);
195
196
197 return 0;
198 }
199
200 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
201 {
202 if (lpLastPath)
203 dir = lpLastPath;
204 else
205 return 0;
206 }
207 else if (dir && _tcslen (dir)==2 && dir[1] == _T(':'))
208 {
209 TCHAR szRoot[3] = _T("A:");
210 TCHAR szPath[MAX_PATH];
211
212 szRoot[0] = _totupper (dir[0]);
213 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
214
215 /* PathRemoveBackslash */
216 if (_tcslen (szPath) > 3)
217 {
218 LPTSTR p = _tcsrchr (szPath, _T('\\'));
219 *p = _T('\0');
220 }
221
222 ConOutPuts (szPath);
223
224
225 return 0;
226 }
227
228 /* remove trailing \ if any, but ONLY if dir is not the root dir */
229 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
230 dir[_tcslen(dir) - 1] = _T('\0');
231
232
233 /* store current directory */
234 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
235 GetCurrentDirectory (MAX_PATH, lpOldPath);
236
237 if (!SetCurrentDirectory (dir))
238 {
239 //ErrorMessage (GetLastError(), _T("CD"));
240 ConOutFormatMessage(GetLastError());
241
242 /* throw away current directory */
243 free (lpOldPath);
244 lpOldPath = NULL;
245
246 return 1;
247 }
248 else
249 {
250 GetCurrentDirectory(MAX_PATH, dir);
251 if (dir[0]!=lpOldPath[0])
252 {
253 SetCurrentDirectory(lpOldPath);
254 free(lpOldPath);
255 }
256 else
257 {
258 if (lpLastPath)
259 free (lpLastPath);
260 lpLastPath = lpOldPath;
261 }
262 }
263
264
265 return 0;
266 }
267 #endif
268
269
270
271 #ifdef INCLUDE_CMD_MKDIR
272 /*
273 * MD / MKDIR
274 *
275 */
276 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
277 {
278 LPTSTR dir; /* pointer to the directory to change to */
279 LPTSTR place; /* used to search for the \ when no space is used */
280 LPTSTR *p = NULL;
281 INT argc;
282 WCHAR szMsg[RC_STRING_MAX_SIZE];
283
284 if (!_tcsncmp (param, _T("/?"), 2))
285 {
286 LoadString( GetModuleHandle(NULL), STRING_MKDIR_HELP, (LPTSTR) szMsg,sizeof(szMsg));
287 ConOutPuts (_T((LPTSTR)szMsg));
288
289 return 0;
290 }
291
292
293 /* check if there is no space between the command and the path */
294 if (param[0] == _T('\0'))
295 {
296 /* search for the \ or . so that both short & long names will work */
297 for (place = cmd; *place; place++)
298 if (*place == _T('.') || *place == _T('\\'))
299 break;
300
301 if (*place)
302 dir = place;
303 else
304 /* signal that there are no parameters */
305 dir = NULL;
306 }
307 else
308 {
309 p = split (param, &argc, FALSE);
310 if (argc > 1)
311 {
312 /*JPP 20-Jul-1998 use standard error message */
313 error_too_many_parameters (param);
314 freep (p);
315 return 1;
316 }
317 else
318 dir = p[0];
319 }
320
321 if (!dir)
322 {
323 LoadString( GetModuleHandle(NULL), STRING_PARAM_ERROR, (LPTSTR) szMsg,sizeof(szMsg));
324 ConErrPrintf (_T((LPTSTR)szMsg));
325 return 1;
326 }
327
328 /* remove trailing \ if any, but ONLY if dir is not the root dir */
329 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
330 dir[_tcslen(dir) - 1] = _T('\0');
331
332 if (!CreateDirectory (dir, NULL))
333 {
334 ErrorMessage (GetLastError(), _T("MD"));
335
336 freep (p);
337 return 1;
338 }
339
340 freep (p);
341
342 return 0;
343 }
344 #endif
345
346
347 #ifdef INCLUDE_CMD_RMDIR
348 /*
349 * RD / RMDIR
350 *
351 */
352 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
353 {
354 LPTSTR dir; /* pointer to the directory to change to */
355 LPTSTR place; /* used to search for the \ when no space is used */
356
357 LPTSTR *p = NULL;
358 INT argc;
359
360
361 WCHAR szMsg[RC_STRING_MAX_SIZE];
362
363 if (!_tcsncmp (param, _T("/?"), 2))
364 {
365 LoadString( GetModuleHandle(NULL), STRING_RMDIR_HELP, (LPTSTR) szMsg,sizeof(szMsg));
366
367 ConOutPuts (_T((LPTSTR)szMsg));
368
369 return 0;
370 }
371
372 /* check if there is no space between the command and the path */
373 if (param[0] == _T('\0'))
374 {
375 /* search for the \ or . so that both short & long names will work */
376 for (place = cmd; *place; place++)
377 if (*place == _T('.') || *place == _T('\\'))
378 break;
379
380 if (*place)
381 dir = place;
382 else
383 /* signal that there are no parameters */
384 dir = NULL;
385 }
386 else
387 {
388 p = split (param, &argc, FALSE);
389 if (argc > 1)
390 {
391 /*JPP 20-Jul-1998 use standard error message */
392 error_too_many_parameters (param);
393 freep (p);
394 return 1;
395 }
396 else
397 dir = p[0];
398 }
399
400 if (!dir)
401 {
402 LoadString( GetModuleHandle(NULL), STRING_PARAM_ERROR, (LPTSTR) szMsg,sizeof(szMsg));
403 ConErrPrintf (_T((LPTSTR)szMsg));
404
405 return 1;
406 }
407
408 /* remove trailing \ if any, but ONLY if dir is not the root dir */
409 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
410 dir[_tcslen(dir) - 1] = _T('\0');
411
412 if (!RemoveDirectory (dir))
413 {
414 ErrorMessage (GetLastError(), _T("RD"));
415 freep (p);
416
417 return 1;
418 }
419
420 freep (p);
421
422 return 0;
423 }
424 #endif
425
426
427 /*
428 * set the exitflag to true
429 *
430 */
431 INT CommandExit (LPTSTR cmd, LPTSTR param)
432 {
433 WCHAR szMsg[RC_STRING_MAX_SIZE];
434
435 if (!_tcsncmp (param, _T("/?"), 2))
436 {
437 LoadString( GetModuleHandle(NULL), STRING_EXIT_HELP, (LPTSTR) szMsg,sizeof(szMsg));
438 ConOutPuts (_T((LPTSTR)szMsg));
439 return 0;
440 }
441
442 bExit = TRUE;
443 return 0;
444 }
445
446
447 #ifdef INCLUDE_CMD_REM
448 /*
449 * does nothing
450 *
451 */
452 INT CommandRem (LPTSTR cmd, LPTSTR param)
453 {
454 WCHAR szMsg[RC_STRING_MAX_SIZE];
455
456 if (!_tcsncmp (param, _T("/?"), 2))
457 {
458 LoadString( GetModuleHandle(NULL), STRING_REM_HELP, (LPTSTR) szMsg,sizeof(szMsg));
459 ConOutPuts (_T((LPTSTR)szMsg));
460 }
461
462 return 0;
463 }
464 #endif /* INCLUDE_CMD_REM */
465
466
467 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
468 {
469 PrintCommandList ();
470 return 0;
471 }
472
473 /* EOF */