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