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