Author: hbelusca Date: Fri Apr 3 13:27:21 2015 New Revision: 67020
URL: http://svn.reactos.org/svn/reactos?rev=67020&view=rev Log: [CMD] - GOTO command: Add a lower-bound test when skipping trailing spaces and such. - CD/CHDIR command: Fix the helper SetRootPath function so that it fails if getting the full path string fails, and remove trailing whitespace from directory string. Fixes stuff like: cd .. && echo success cd ".. " && echo success etc... CORE-6810
Modified: trunk/reactos/base/shell/cmd/goto.c trunk/reactos/base/shell/cmd/internal.c
Modified: trunk/reactos/base/shell/cmd/goto.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/goto.c?rev=6... ============================================================================== --- trunk/reactos/base/shell/cmd/goto.c [iso-8859-1] (original) +++ trunk/reactos/base/shell/cmd/goto.c [iso-8859-1] Fri Apr 3 13:27:21 2015 @@ -85,7 +85,7 @@ /* Strip out any trailing spaces or control chars */ tmp = textline + _tcslen (textline) - 1;
- while (_istcntrl (*tmp) || _istspace (*tmp) || (*tmp == _T(':'))) + while (tmp > textline && (_istcntrl (*tmp) || _istspace (*tmp) || (*tmp == _T(':')))) tmp--; *(tmp + 1) = _T('\0');
Modified: trunk/reactos/base/shell/cmd/internal.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/internal.c?r... ============================================================================== --- trunk/reactos/base/shell/cmd/internal.c [iso-8859-1] (original) +++ trunk/reactos/base/shell/cmd/internal.c [iso-8859-1] Fri Apr 3 13:27:21 2015 @@ -141,7 +141,7 @@
#ifdef INCLUDE_CMD_CHDIR
-/* help functions for getting current path from drive +/* helper functions for getting current path from drive without changing drive. Return code 0 = ok, 1 = fail. INT GetRootPath("C:",outbuffer,chater size of outbuffer); the first param can have any size, if the the two frist @@ -184,28 +184,29 @@ TCHAR OutPath[MAX_PATH]; TCHAR OutPathTemp[MAX_PATH];
- /* The use of both of these together will correct the case of a path - where as one alone or GetFullPath will not. Exameple: - c:\windows\SYSTEM32 => C:\WINDOWS\system32 */ - if (GetFullPathName(InPath, MAX_PATH, OutPathTemp, NULL)) - { - GetPathCase(OutPathTemp, OutPath); - - /* Use _tchdir, since unlike SetCurrentDirectory it updates - * the current-directory-on-drive environment variables. */ - if (_tchdir(OutPath) != 0) - { - ConErrFormatMessage(GetLastError()); - nErrorLevel = 1; - return FALSE; - } - - /* Keep original drive in ordinary CD/CHDIR (without /D switch). */ - if (oldpath != NULL && _tcsncicmp(OutPath, oldpath, 2) != 0) - SetCurrentDirectory(oldpath); - } + /* Retrieve the full path name from the (possibly relative) InPath */ + if (GetFullPathName(InPath, MAX_PATH, OutPathTemp, NULL) == 0) + goto Fail; + + /* Convert the full path to its correct case. + * Example: c:\windows\SYSTEM32 => C:\WINDOWS\System32 */ + GetPathCase(OutPathTemp, OutPath); + + /* Use _tchdir, since unlike SetCurrentDirectory it updates + * the current-directory-on-drive environment variables. */ + if (_tchdir(OutPath) != 0) + goto Fail; + + /* Keep original drive in ordinary CD/CHDIR (without /D switch). */ + if (oldpath != NULL && _tcsncicmp(OutPath, oldpath, 2) != 0) + SetCurrentDirectory(oldpath);
return TRUE; + +Fail: + ConErrFormatMessage(GetLastError()); + nErrorLevel = 1; + return FALSE; }
@@ -215,8 +216,9 @@ */ INT cmd_chdir (LPTSTR param) { + BOOL bChangeDrive = FALSE; + LPTSTR tmp; TCHAR szCurrent[MAX_PATH]; - BOOL bChangeDrive = FALSE;
/* Filter out special cases first */
@@ -227,8 +229,12 @@ return 0; }
- /* Remove " */ + /* Remove extra quotes and strip trailing whitespace */ StripQuotes(param); + tmp = param + _tcslen(param) - 1; + while (tmp > param && _istspace(*tmp)) + tmp--; + *(tmp + 1) = _T('\0');
/* Set Error Level to Success */ nErrorLevel = 0; @@ -263,7 +269,10 @@ }
if (!SetRootPath(bChangeDrive ? NULL : szCurrent, param)) + { + nErrorLevel = 1; return 1; + }
return 0; } @@ -324,8 +333,8 @@ if (argc == 0) { ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING); + freep(p); nErrorLevel = 1; - freep(p); return 1; }