make GetRootPath bit faster if it does not get a drive name with :
[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 /* 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.
157 */
158
159 INT GetRootPath(TCHAR *InPath,TCHAR *OutPath,INT size)
160 {
161 INT retcode = 1;
162 INT t;
163
164 if (_tcslen(InPath)>1)
165 {
166 if (InPath[1]==_T(':'))
167 {
168 for (t=0;t<32;t++)
169 {
170 if (_tgetdcwd(t,OutPath,size) != NULL)
171 {
172 if (_tcsncicmp(InPath,OutPath,2))
173 {
174 retcode = 0;
175 return retcode;
176 }
177 }
178 }
179 }
180 }
181
182 /* fail to getting path devic did not exists */
183 if (_tcslen(InPath)>1)
184 {
185 if (InPath[1]==_T(':'))
186 return 1;
187 }
188
189 retcode = GetCurrentDirectory(size,OutPath);
190 if (retcode==0)
191 return 1;
192
193 return 0;
194 }
195
196
197 /*
198 * CD / CHDIR
199 *
200 */
201 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
202 {
203 LPTSTR dir; /* pointer to the directory to change to */
204 LPTSTR lpOldPath;
205 size_t size, str_len;
206 WIN32_FIND_DATA FileData;
207 HANDLE hSearch;
208 DWORD dwAttrs;
209 BOOL fFinished = FALSE;
210
211
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*/
214
215 if (!_tcsncmp (param, _T("/?"), 2))
216 {
217 ConOutResPaging(TRUE,STRING_CD_HELP);
218 return 0;
219 }
220
221 nErrorLevel = 0;
222
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?) */
226
227 size = _tcscspn(param, _T("\"") );
228 str_len = _tcslen(param)-1;
229
230 if ((param[size] == _T('"')) && (str_len >1))
231 {
232
233 if (size==0)
234 {
235 _tcsncpy(param,&param[size+1],str_len);
236 param[str_len] = _T('\0');
237 }
238
239 size = _tcscspn(param, _T("\"") );
240 if (param[size] == _T('"'))
241 {
242 param[size] = _T('\0');
243 }
244
245 }
246
247 str_len = _tcslen(param);
248 if (str_len==1)
249 {
250 if (param[0] == _T('*'))
251 {
252 param[0] = _T('.');
253 }
254 }
255
256 dir=param;
257
258 /* if doing a CD and no parameters given, print out current directory */
259 if (!dir || !dir[0])
260 {
261 TCHAR szPath[MAX_PATH];
262
263 GetCurrentDirectory (MAX_PATH, szPath);
264 ConOutPuts (szPath);
265 return 0;
266 }
267
268 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
269 {
270 if (lpLastPath)
271 dir = lpLastPath;
272 else
273 return 0;
274 }
275 else if (dir && _tcslen (dir)==2 && dir[1] == _T(':'))
276 {
277 TCHAR szRoot[3] = _T("A:");
278 TCHAR szPath[MAX_PATH];
279
280 szRoot[0] = _totupper (dir[0]);
281 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
282
283 /* PathRemoveBackslash */
284 if (_tcslen (szPath) > 3)
285 {
286 LPTSTR p = _tcsrchr (szPath, _T('\\'));
287 *p = _T('\0');
288 }
289
290 ConOutPuts (szPath);
291
292
293 return 0;
294 }
295
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');
299
300
301 /* store current directory */
302 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
303 GetCurrentDirectory (MAX_PATH, lpOldPath);
304
305 if (!SetCurrentDirectory (dir))
306 {
307
308 hSearch = FindFirstFile(dir, &FileData);
309 if (hSearch == INVALID_HANDLE_VALUE)
310 {
311 ConOutFormatMessage(GetLastError());
312 free (lpOldPath);
313 lpOldPath = NULL;
314 nErrorLevel = 1;
315 return 1;
316 }
317
318
319 while (!fFinished)
320 {
321 dwAttrs = GetFileAttributes(FileData.cFileName);
322 #ifdef _DEBUG
323 DebugPrintf(_T("Search found folder :%s\n"),FileData.cFileName);
324 #endif
325 if ((dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
326 {
327 FindClose(hSearch);
328 // change folder
329 if (!SetCurrentDirectory (FileData.cFileName))
330 {
331 ConOutFormatMessage(GetLastError());
332 free (lpOldPath);
333 lpOldPath = NULL;
334 nErrorLevel = 1;
335 return 1;
336 }
337
338
339 return 0;
340 }
341
342 else if (!FindNextFile(hSearch, &FileData))
343 {
344 FindClose(hSearch);
345 ConOutFormatMessage(GetLastError());
346 nErrorLevel = 1;
347 free (lpOldPath);
348 lpOldPath = NULL;
349 return 1;
350 }
351 }
352
353 //ErrorMessage (GetLastError(), _T("CD"));
354 ConOutFormatMessage(GetLastError());
355 nErrorLevel = 1;
356
357 /* throw away current directory */
358 free (lpOldPath);
359 lpOldPath = NULL;
360
361 return 1;
362 }
363 else
364 {
365 GetCurrentDirectory(MAX_PATH, dir);
366 if (dir[0]!=lpOldPath[0])
367 {
368 SetCurrentDirectory(lpOldPath);
369 free(lpOldPath);
370 }
371 else
372 {
373 if (lpLastPath)
374 free (lpLastPath);
375 lpLastPath = lpOldPath;
376 }
377 }
378
379
380 return 0;
381 }
382 #endif
383
384
385
386 #ifdef INCLUDE_CMD_MKDIR
387 /*
388 * MD / MKDIR
389 *
390 */
391 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
392 {
393 LPTSTR dir; /* pointer to the directory to change to */
394 LPTSTR place; /* used to search for the \ when no space is used */
395 LPTSTR *p = NULL;
396 INT argc;
397
398 if (!_tcsncmp (param, _T("/?"), 2))
399 {
400 ConOutResPaging(TRUE,STRING_MKDIR_HELP);
401 return 0;
402 }
403
404
405 /* check if there is no space between the command and the path */
406 if (param[0] == _T('\0'))
407 {
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('\\'))
411 break;
412
413 if (*place)
414 dir = place;
415 else
416 /* signal that there are no parameters */
417 dir = NULL;
418 }
419 else
420 {
421 p = split (param, &argc, FALSE);
422 if (argc > 1)
423 {
424 /*JPP 20-Jul-1998 use standard error message */
425 error_too_many_parameters (param);
426 freep (p);
427 return 1;
428 }
429 else
430 dir = p[0];
431 }
432
433 if (!dir)
434 {
435 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
436 return 1;
437 }
438
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');
442
443 if (!CreateDirectory (dir, NULL))
444 {
445 ErrorMessage (GetLastError(), _T("MD"));
446
447 freep (p);
448 return 1;
449 }
450
451 freep (p);
452
453 return 0;
454 }
455 #endif
456
457
458 #ifdef INCLUDE_CMD_RMDIR
459 /*
460 * RD / RMDIR
461 *
462 */
463 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
464 {
465 LPTSTR dir; /* pointer to the directory to change to */
466 LPTSTR place; /* used to search for the \ when no space is used */
467
468 LPTSTR *p = NULL;
469 INT argc;
470
471 if (!_tcsncmp (param, _T("/?"), 2))
472 {
473 ConOutResPaging(TRUE,STRING_RMDIR_HELP);
474 return 0;
475 }
476
477 /* check if there is no space between the command and the path */
478 if (param[0] == _T('\0'))
479 {
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('\\'))
483 break;
484
485 if (*place)
486 dir = place;
487 else
488 /* signal that there are no parameters */
489 dir = NULL;
490 }
491 else
492 {
493 p = split (param, &argc, FALSE);
494 if (argc > 1)
495 {
496 /*JPP 20-Jul-1998 use standard error message */
497 error_too_many_parameters (param);
498 freep (p);
499 return 1;
500 }
501 else
502 dir = p[0];
503 }
504
505 if (!dir)
506 {
507 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
508 return 1;
509 }
510
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');
514
515 if (!RemoveDirectory (dir))
516 {
517 ErrorMessage (GetLastError(), _T("RD"));
518 freep (p);
519
520 return 1;
521 }
522
523 freep (p);
524
525 return 0;
526 }
527 #endif
528
529
530 /*
531 * set the exitflag to true
532 *
533 */
534 INT CommandExit (LPTSTR cmd, LPTSTR param)
535 {
536 if (!_tcsncmp (param, _T("/?"), 2))
537 ConOutResPaging(TRUE,STRING_EXIT_HELP);
538
539 if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
540 {
541 param += 2;
542 while (_istspace (*param))
543 param++;
544 if (_istdigit(*param))
545 nErrorLevel = _ttoi(param);
546 ExitBatch (NULL);
547 }
548
549 else
550 bExit = TRUE;
551
552
553 return 0;
554
555 }
556
557 #ifdef INCLUDE_CMD_REM
558 /*
559 * does nothing
560 *
561 */
562 INT CommandRem (LPTSTR cmd, LPTSTR param)
563 {
564 if (!_tcsncmp (param, _T("/?"), 2))
565 {
566 ConOutResPaging(TRUE,STRING_REM_HELP);
567 }
568
569 return 0;
570 }
571 #endif /* INCLUDE_CMD_REM */
572
573
574 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
575 {
576 PrintCommandList ();
577 return 0;
578 }
579
580 INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
581 {
582 PrintCommandListDetail ();
583 return 0;
584 }
585
586 /* EOF */