5a0fe529d0f57d34e5e00d6e46b1d972313a4e71
[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 * 19-jul-2005 (Brandon Turner <turnerb7@msu.edu)
131 * Rewrite the CD, it working as Windows 2000 CMD
132 *
133 * 19-jul-2005 (Magnus Olsen <magnus@greatlord.com)
134 * Add SetRootPath and GetRootPath
135 */
136
137 #include <precomp.h>
138 #include "resource.h"
139
140 #ifdef INCLUDE_CMD_CHDIR
141
142 static LPTSTR lpLastPath;
143
144
145 VOID InitLastPath (VOID)
146 {
147 lpLastPath = NULL;
148 }
149
150
151 VOID FreeLastPath (VOID)
152 {
153 if (lpLastPath)
154 free (lpLastPath);
155 }
156
157 /* help functions for getting current path from drive
158 without changing drive. Return code 0 = ok, 1 = fail.
159 INT GetRootPath("C:",outbuffer,chater size of outbuffer);
160 the first param can have any size, if the the two frist
161 letter are not a drive with : it will get Currentpath on
162 current drive exacly as GetCurrentDirectory does.
163 */
164
165 INT GetRootPath(TCHAR *InPath,TCHAR *OutPath,INT size)
166 {
167 INT retcode = 1;
168
169 if (_tcslen(InPath)>1)
170 {
171 if (InPath[1]==_T(':'))
172 {
173 INT t=0;
174
175 if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
176 {
177 t = (InPath[0] - _T('0')) +28;
178 }
179
180 if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
181 {
182 t = (InPath[0] - _T('a')) +1;
183 InPath[0] = t + _T('A') - 1;
184 }
185
186 if ((InPath[0] >= _T('A')) && (InPath[0] <= _T('Z')))
187 {
188 t = (InPath[0] - _T('A')) +1;
189 }
190
191 if (_tgetdcwd(t,OutPath,size) != NULL)
192 {
193 return 0;
194 }
195 }
196 }
197
198 /* fail */
199 if (_tcslen(InPath)>1)
200 {
201 if (InPath[1]==_T(':'))
202 return 1;
203 }
204
205 /* Get current directory */
206 retcode = GetCurrentDirectory(size,OutPath);
207 if (retcode==0)
208 return 1;
209
210 return 0;
211 }
212
213
214 BOOL SetRootPath(TCHAR *InPath)
215 {
216 TCHAR oldpath[MAX_PATH];
217 TCHAR OutPath[MAX_PATH];
218 TCHAR OutPathUpper[MAX_PATH];
219 BOOL fail;
220
221
222 /* Get The current directory path and save it */
223 fail = GetCurrentDirectory(MAX_PATH,oldpath);
224 if (!fail)
225 return 1;
226
227 /* Get current drive directory path if C: was only pass down*/
228
229 if (_tcsncicmp(&InPath[1],_T(":\\"),2)!=0)
230 {
231 if (!GetRootPath(InPath,OutPathUpper,MAX_PATH))
232 _tcscpy(OutPathUpper,InPath);
233 }
234 else
235 {
236 _tcscpy(OutPathUpper,InPath);
237 }
238
239 _tcsupr(OutPathUpper);
240 GetLongPathName(OutPathUpper, OutPath, MAX_PATH);
241
242 fail = SetCurrentDirectory(OutPath);
243 if (!fail)
244 return 1;
245
246
247
248 SetCurrentDirectory(OutPath);
249 GetCurrentDirectory(MAX_PATH,OutPath);
250 _tchdir(OutPath);
251
252 if (_tcsncicmp(OutPath,oldpath,2)!=0)
253 SetCurrentDirectory(oldpath);
254
255 return 0;
256 }
257
258
259 /*
260 * CD / CHDIR
261 *
262 */
263 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
264 {
265
266 WIN32_FIND_DATA f;
267 HANDLE hFile;
268 BOOL bChangeDrive = FALSE;
269 TCHAR szPath[MAX_PATH];
270 TCHAR szFinalPath[MAX_PATH];
271 TCHAR * tmpPath;
272 TCHAR szCurrent[MAX_PATH];
273 TCHAR szMsg[RC_STRING_MAX_SIZE];
274 INT i;
275
276
277 /* Filter out special cases first */
278
279 /* Print Help */
280 if (!_tcsncmp(param, _T("/?"), 2))
281 {
282 ConOutResPaging(TRUE,STRING_CD_HELP);
283 return 0;
284 }
285
286 /* Set Error Level to Success */
287 nErrorLevel = 0;
288
289 /* Input String Contains /D Switch */
290 if (!_tcsncicmp(param, _T("/D"), 2))
291 {
292 bChangeDrive = TRUE;
293 tmpPath = _tcsstr(param,_T(" "));
294 tmpPath++;
295 _tcscpy(szPath,tmpPath);
296
297 }
298 else
299 {
300 _tcscpy(szPath,param);
301 }
302
303 /* Print Current Directory on a disk */
304 if (_tcslen(szPath) == 2 && szPath[1] == _T(':'))
305 {
306 if(GetRootPath(szPath,szCurrent,MAX_PATH))
307 {
308 nErrorLevel = 1;
309 return 1;
310 }
311 ConOutPuts(szCurrent);
312 return 0;
313 }
314
315 /* Get Current Directory */
316 GetRootPath(_T("."),szCurrent,MAX_PATH);
317
318 /* Remove " */
319 if(szPath[0] == _T('\"'))
320 {
321 tmpPath = _tcsstr(szPath,_T("\""));
322 tmpPath++;
323 _tcscpy(szPath,tmpPath);
324 }
325
326 if(szPath[_tcslen(szPath) - 1] == _T('\"'))
327 {
328 szPath[_tcslen(szPath) - 1] = _T('\0');
329 }
330
331 tmpPath = szPath;
332 while (_istspace (*tmpPath))
333 tmpPath++;
334 _tcscpy(szPath,tmpPath);
335
336 if (szPath[0] == _T('\0'))
337 {
338 ConOutPuts(szCurrent);
339 return 0;
340 }
341
342
343 /* change to full path if relative path was given */
344 GetFullPathName(szPath,MAX_PATH,szFinalPath,NULL);
345
346 if(szFinalPath[_tcslen(szFinalPath) - 1] == _T('\\') && _tcslen(szFinalPath) > 3)
347 szFinalPath[_tcslen(szFinalPath) - 1] = _T('\0');
348
349 /* Handle Root Directory Alone*/
350 if (_tcslen(szFinalPath) == 3 && szFinalPath[1] == _T(':'))
351 {
352 if(!SetRootPath(szFinalPath))
353 {
354 /* Change prompt if it is one the same drive or /D */
355 if(bChangeDrive || !_tcsncicmp(szFinalPath,szCurrent,1))
356 SetCurrentDirectory(szFinalPath);
357 return 0;
358 }
359 /* Didnt find an directories */
360 LoadString(CMD_ModuleHandle, STRING_ERROR_PATH_NOT_FOUND, szMsg, RC_STRING_MAX_SIZE);
361 ConErrPrintf(szMsg);
362 nErrorLevel = 1;
363 return 1;
364
365 }
366
367 /* Get a list of all the files */
368 hFile = FindFirstFile (szFinalPath, &f);
369
370 do
371 {
372 if(hFile == INVALID_HANDLE_VALUE)
373 {
374 ConErrFormatMessage (GetLastError(), szFinalPath);
375 nErrorLevel = 1;
376 return 1;
377 }
378
379 /* Strip the paths back to the folder they are in */
380 for(i = (_tcslen(szFinalPath) - 1); i > -1; i--)
381 if(szFinalPath[i] != _T('\\'))
382 szFinalPath[i] = _T('\0');
383 else
384 break;
385
386 _tcscat(szFinalPath,f.cFileName);
387
388 if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
389 {
390 if(!SetRootPath(szFinalPath))
391 {
392 /* Change for /D */
393 if(bChangeDrive)
394 {
395 _tcsupr(szFinalPath);
396 GetLongPathName(szFinalPath, szPath, MAX_PATH);
397 SetCurrentDirectory(szPath);
398 }
399 return 0;
400 }
401
402 }
403 }while(FindNextFile (hFile, &f));
404
405 /* Didnt find an directories */
406 LoadString(CMD_ModuleHandle, STRING_ERROR_PATH_NOT_FOUND, szMsg, RC_STRING_MAX_SIZE);
407 ConErrPrintf(szMsg);
408 nErrorLevel = 1;
409 return 1;
410 }
411
412 #endif
413
414
415
416 #ifdef INCLUDE_CMD_MKDIR
417 /*
418 * MD / MKDIR
419 *
420 */
421 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
422 {
423 LPTSTR dir; /* pointer to the directory to change to */
424 LPTSTR place; /* used to search for the \ when no space is used */
425 LPTSTR *p = NULL;
426 INT argc;
427
428 if (!_tcsncmp (param, _T("/?"), 2))
429 {
430 ConOutResPaging(TRUE,STRING_MKDIR_HELP);
431 return 0;
432 }
433
434
435 /* check if there is no space between the command and the path */
436 if (param[0] == _T('\0'))
437 {
438 /* search for the \ or . so that both short & long names will work */
439 for (place = cmd; *place; place++)
440 if (*place == _T('.') || *place == _T('\\'))
441 break;
442
443 if (*place)
444 dir = place;
445 else
446 /* signal that there are no parameters */
447 dir = NULL;
448 }
449 else
450 {
451 p = split (param, &argc, FALSE);
452 if (argc > 1)
453 {
454 /*JPP 20-Jul-1998 use standard error message */
455 error_too_many_parameters (param);
456 freep (p);
457 return 1;
458 }
459 else
460 dir = p[0];
461 }
462
463 if (!dir)
464 {
465 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
466 return 1;
467 }
468
469 /* remove trailing \ if any, but ONLY if dir is not the root dir */
470 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
471 dir[_tcslen(dir) - 1] = _T('\0');
472
473 if (!CreateDirectory (dir, NULL))
474 {
475 ErrorMessage (GetLastError(), _T("MD"));
476
477 freep (p);
478 return 1;
479 }
480
481 freep (p);
482
483 return 0;
484 }
485 #endif
486
487
488 #ifdef INCLUDE_CMD_RMDIR
489 /*
490 * RD / RMDIR
491 *
492 */
493 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
494 {
495 LPTSTR dir; /* pointer to the directory to change to */
496 LPTSTR place; /* used to search for the \ when no space is used */
497
498 LPTSTR *p = NULL;
499 INT argc;
500
501 if (!_tcsncmp (param, _T("/?"), 2))
502 {
503 ConOutResPaging(TRUE,STRING_RMDIR_HELP);
504 return 0;
505 }
506
507 /* check if there is no space between the command and the path */
508 if (param[0] == _T('\0'))
509 {
510 /* search for the \ or . so that both short & long names will work */
511 for (place = cmd; *place; place++)
512 if (*place == _T('.') || *place == _T('\\'))
513 break;
514
515 if (*place)
516 dir = place;
517 else
518 /* signal that there are no parameters */
519 dir = NULL;
520 }
521 else
522 {
523 p = split (param, &argc, FALSE);
524 if (argc > 1)
525 {
526 /*JPP 20-Jul-1998 use standard error message */
527 error_too_many_parameters (param);
528 freep (p);
529 return 1;
530 }
531 else
532 dir = p[0];
533 }
534
535 if (!dir)
536 {
537 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
538 return 1;
539 }
540
541 /* remove trailing \ if any, but ONLY if dir is not the root dir */
542 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
543 dir[_tcslen(dir) - 1] = _T('\0');
544
545 if (!RemoveDirectory (dir))
546 {
547 ErrorMessage (GetLastError(), _T("RD"));
548 freep (p);
549
550 return 1;
551 }
552
553 freep (p);
554
555 return 0;
556 }
557 #endif
558
559
560 /*
561 * set the exitflag to true
562 *
563 */
564 INT CommandExit (LPTSTR cmd, LPTSTR param)
565 {
566 if (!_tcsncmp (param, _T("/?"), 2))
567 ConOutResPaging(TRUE,STRING_EXIT_HELP);
568
569 if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
570 {
571 param += 2;
572 while (_istspace (*param))
573 param++;
574 if (_istdigit(*param))
575 nErrorLevel = _ttoi(param);
576 ExitBatch (NULL);
577 }
578
579 else
580 bExit = TRUE;
581
582
583 return 0;
584
585 }
586
587 #ifdef INCLUDE_CMD_REM
588 /*
589 * does nothing
590 *
591 */
592 INT CommandRem (LPTSTR cmd, LPTSTR param)
593 {
594 if (!_tcsncmp (param, _T("/?"), 2))
595 {
596 ConOutResPaging(TRUE,STRING_REM_HELP);
597 }
598
599 return 0;
600 }
601 #endif /* INCLUDE_CMD_REM */
602
603
604 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
605 {
606 PrintCommandList ();
607 return 0;
608 }
609
610 INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
611 {
612 PrintCommandListDetail ();
613 return 0;
614 }
615
616 /* EOF */