remove whitespace from end of lines
[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
162 /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
163 but on the other hand paths are generally not very long*/
164
165 if (!_tcsncmp (param, _T("/?"), 2))
166 {
167 ConOutResPuts(STRING_CD_HELP);
168 return 0;
169 }
170
171 /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
172 /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
173 post-processing of it after the function call (what would that accomplish?) */
174
175 dir=param;
176 endofstring=dir+_tcslen(dir);
177
178 while ((lastquote = _tcsrchr(dir, _T('\"'))))
179 {
180 endofstring--;
181 memmove(lastquote,lastquote+1,endofstring-lastquote);
182 *endofstring=_T('\0');
183 }
184
185 /* if doing a CD and no parameters given, print out current directory */
186 if (!dir || !dir[0])
187 {
188 TCHAR szPath[MAX_PATH];
189
190 GetCurrentDirectory (MAX_PATH, szPath);
191 ConOutPuts (szPath);
192 return 0;
193 }
194
195 if (dir && _tcslen (dir) == 1 && *dir == _T('-'))
196 {
197 if (lpLastPath)
198 dir = lpLastPath;
199 else
200 return 0;
201 }
202 else if (dir && _tcslen (dir)==2 && dir[1] == _T(':'))
203 {
204 TCHAR szRoot[3] = _T("A:");
205 TCHAR szPath[MAX_PATH];
206
207 szRoot[0] = _totupper (dir[0]);
208 GetFullPathName (szRoot, MAX_PATH, szPath, NULL);
209
210 /* PathRemoveBackslash */
211 if (_tcslen (szPath) > 3)
212 {
213 LPTSTR p = _tcsrchr (szPath, _T('\\'));
214 *p = _T('\0');
215 }
216
217 ConOutPuts (szPath);
218
219
220 return 0;
221 }
222
223 /* remove trailing \ if any, but ONLY if dir is not the root dir */
224 if (_tcslen (dir) > 3 && dir[_tcslen (dir) - 1] == _T('\\'))
225 dir[_tcslen(dir) - 1] = _T('\0');
226
227
228 /* store current directory */
229 lpOldPath = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
230 GetCurrentDirectory (MAX_PATH, lpOldPath);
231
232 if (!SetCurrentDirectory (dir))
233 {
234 //ErrorMessage (GetLastError(), _T("CD"));
235 ConOutFormatMessage(GetLastError());
236
237 /* throw away current directory */
238 free (lpOldPath);
239 lpOldPath = NULL;
240
241 return 1;
242 }
243 else
244 {
245 GetCurrentDirectory(MAX_PATH, dir);
246 if (dir[0]!=lpOldPath[0])
247 {
248 SetCurrentDirectory(lpOldPath);
249 free(lpOldPath);
250 }
251 else
252 {
253 if (lpLastPath)
254 free (lpLastPath);
255 lpLastPath = lpOldPath;
256 }
257 }
258
259
260 return 0;
261 }
262 #endif
263
264
265
266 #ifdef INCLUDE_CMD_MKDIR
267 /*
268 * MD / MKDIR
269 *
270 */
271 INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
272 {
273 LPTSTR dir; /* pointer to the directory to change to */
274 LPTSTR place; /* used to search for the \ when no space is used */
275 LPTSTR *p = NULL;
276 INT argc;
277
278 if (!_tcsncmp (param, _T("/?"), 2))
279 {
280 ConOutResPuts(STRING_MKDIR_HELP);
281 return 0;
282 }
283
284
285 /* check if there is no space between the command and the path */
286 if (param[0] == _T('\0'))
287 {
288 /* search for the \ or . so that both short & long names will work */
289 for (place = cmd; *place; place++)
290 if (*place == _T('.') || *place == _T('\\'))
291 break;
292
293 if (*place)
294 dir = place;
295 else
296 /* signal that there are no parameters */
297 dir = NULL;
298 }
299 else
300 {
301 p = split (param, &argc, FALSE);
302 if (argc > 1)
303 {
304 /*JPP 20-Jul-1998 use standard error message */
305 error_too_many_parameters (param);
306 freep (p);
307 return 1;
308 }
309 else
310 dir = p[0];
311 }
312
313 if (!dir)
314 {
315 ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
316 return 1;
317 }
318
319 /* remove trailing \ if any, but ONLY if dir is not the root dir */
320 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
321 dir[_tcslen(dir) - 1] = _T('\0');
322
323 if (!CreateDirectory (dir, NULL))
324 {
325 ErrorMessage (GetLastError(), _T("MD"));
326
327 freep (p);
328 return 1;
329 }
330
331 freep (p);
332
333 return 0;
334 }
335 #endif
336
337
338 #ifdef INCLUDE_CMD_RMDIR
339 /*
340 * RD / RMDIR
341 *
342 */
343 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
344 {
345 LPTSTR dir; /* pointer to the directory to change to */
346 LPTSTR place; /* used to search for the \ when no space is used */
347
348 LPTSTR *p = NULL;
349 INT argc;
350
351 if (!_tcsncmp (param, _T("/?"), 2))
352 {
353 ConOutResPuts(STRING_RMDIR_HELP);
354 return 0;
355 }
356
357 /* check if there is no space between the command and the path */
358 if (param[0] == _T('\0'))
359 {
360 /* search for the \ or . so that both short & long names will work */
361 for (place = cmd; *place; place++)
362 if (*place == _T('.') || *place == _T('\\'))
363 break;
364
365 if (*place)
366 dir = place;
367 else
368 /* signal that there are no parameters */
369 dir = NULL;
370 }
371 else
372 {
373 p = split (param, &argc, FALSE);
374 if (argc > 1)
375 {
376 /*JPP 20-Jul-1998 use standard error message */
377 error_too_many_parameters (param);
378 freep (p);
379 return 1;
380 }
381 else
382 dir = p[0];
383 }
384
385 if (!dir)
386 {
387 ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
388 return 1;
389 }
390
391 /* remove trailing \ if any, but ONLY if dir is not the root dir */
392 if (_tcslen (dir) >= 2 && dir[_tcslen (dir) - 1] == _T('\\'))
393 dir[_tcslen(dir) - 1] = _T('\0');
394
395 if (!RemoveDirectory (dir))
396 {
397 ErrorMessage (GetLastError(), _T("RD"));
398 freep (p);
399
400 return 1;
401 }
402
403 freep (p);
404
405 return 0;
406 }
407 #endif
408
409
410 /*
411 * set the exitflag to true
412 *
413 */
414 INT CommandExit (LPTSTR cmd, LPTSTR param)
415 {
416 if (!_tcsncmp (param, _T("/?"), 2))
417 {
418 ConOutResPuts(STRING_EXIT_HELP);
419 return 0;
420 }
421
422 bExit = TRUE;
423 return 0;
424 }
425
426
427 #ifdef INCLUDE_CMD_REM
428 /*
429 * does nothing
430 *
431 */
432 INT CommandRem (LPTSTR cmd, LPTSTR param)
433 {
434 if (!_tcsncmp (param, _T("/?"), 2))
435 {
436 ConOutResPuts(STRING_REM_HELP);
437 }
438
439 return 0;
440 }
441 #endif /* INCLUDE_CMD_REM */
442
443
444 INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
445 {
446 PrintCommandList ();
447 return 0;
448 }
449
450 /* EOF */