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