cmd update:
[reactos.git] / reactos / subsys / system / cmd / internal.c
index a93969f..926f74b 100644 (file)
  *        Improved chdir/cd command.
  *
  *    02-Apr-2004 (Magnus Olsen <magnus@greatlord.com>)
- *        Remove all hard code string so they can be 
+ *        Remove all hard code string so they can be
  *               translate to other langues.
  */
 
@@ -153,17 +153,21 @@ VOID FreeLastPath (VOID)
  *
  */
 INT cmd_chdir (LPTSTR cmd, LPTSTR param)
-{      
+{
        LPTSTR dir;             /* pointer to the directory to change to */
        LPTSTR lpOldPath;
-       LPTSTR endofstring; /* pointer to the null character in the directory to change to */
-       LPTSTR lastquote; /* pointer to the last quotation mark in the directory to change to */
+       size_t size, str_len;
+       WIN32_FIND_DATA FileData; 
+    HANDLE hSearch; 
+    DWORD dwAttrs;  
+    BOOL fFinished = FALSE; 
 
        /*Should we better declare a variable containing _tsclen(dir) ? It's used a few times,
          but on the other hand paths are generally not very long*/
 
        if (!_tcsncmp (param, _T("/?"), 2))
-       {               
+       {
                ConOutResPuts(STRING_CD_HELP);
                return 0;
        }
@@ -171,17 +175,38 @@ INT cmd_chdir (LPTSTR cmd, LPTSTR param)
        /* The whole param string is our parameter these days. The only thing we do is eliminating every quotation mark */
        /* Is it safe to change the characters param is pointing to? I presume it is, as there doesn't seem to be any
        post-processing of it after the function call (what would that accomplish?) */
+       
+    size = _tcscspn(param,  _T("\"") );
+       str_len = _tcslen(param)-1;
 
-       dir=param;
-       endofstring=dir+_tcslen(dir);
-
-       while ((lastquote = _tcsrchr(dir, _T('\"'))))
+       if ((param[size] == _T('"')) && (str_len >1))
        {
-               endofstring--;
-               memmove(lastquote,lastquote+1,endofstring-lastquote);
-               *endofstring=_T('\0');
+        
+        if (size==0)
+        {
+         _tcsncpy(param,&param[size+1],str_len);
+         param[str_len] = _T('\0');     
+        }
+
+        size = _tcscspn(param,  _T("\"") );    
+     if (param[size] == _T('"'))
+        {
+         param[size] = _T('\0');
+        }
+
        }
 
+       str_len = _tcslen(param);
+       if (str_len==1) 
+       {
+           if (param[0] == _T('*')) 
+           {
+                   param[0] = _T('.');
+               }
+       }
+       
+       dir=param;
+       
        /* if doing a CD and no parameters given, print out current directory */
        if (!dir || !dir[0])
        {
@@ -231,9 +256,52 @@ INT cmd_chdir (LPTSTR cmd, LPTSTR param)
 
        if (!SetCurrentDirectory (dir))
        {
+
+           hSearch = FindFirstFile(dir, &FileData); 
+        if (hSearch == INVALID_HANDLE_VALUE) 
+        { 
+               ConOutFormatMessage(GetLastError());
+                       free (lpOldPath);
+                   lpOldPath = NULL;
+            return 1;
+               }
+
+               
+        while (!fFinished) 
+        { 
+            dwAttrs = GetFileAttributes(FileData.cFileName); 
+#ifdef _DEBUG
+                       DebugPrintf(_T("Search found folder :%s\n"),FileData.cFileName);
+#endif
+            if ((dwAttrs & FILE_ATTRIBUTE_DIRECTORY)) 
+            {
+                         FindClose(hSearch);                
+                 // change folder
+                        if (!SetCurrentDirectory (FileData.cFileName))
+                        {
+                                ConOutFormatMessage(GetLastError());
+                            free (lpOldPath);
+                        lpOldPath = NULL;
+                                return 1;
+                        }
+                               
+             
+                        return 0;
+             }
+        
+             else if (!FindNextFile(hSearch, &FileData)) 
+            {             
+                    FindClose(hSearch);
+                        ConOutFormatMessage(GetLastError());
+                        free (lpOldPath);
+                    lpOldPath = NULL;
+                        return 1;
+             }
+        }  
+
                //ErrorMessage (GetLastError(), _T("CD"));
                ConOutFormatMessage(GetLastError());
-               
+
                /* throw away current directory */
                free (lpOldPath);
                lpOldPath = NULL;
@@ -276,7 +344,7 @@ INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
        INT argc;
 
        if (!_tcsncmp (param, _T("/?"), 2))
-       {               
+       {
                ConOutResPuts(STRING_MKDIR_HELP);
                return 0;
        }
@@ -311,7 +379,7 @@ INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
        }
 
        if (!dir)
-       {               
+       {
                ConErrResPuts (STRING_ERROR_REQ_PARAM_MISSING);
                return 1;
        }
@@ -341,7 +409,7 @@ INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
  *
  */
 INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
-{      
+{
        LPTSTR dir;             /* pointer to the directory to change to */
        LPTSTR place;   /* used to search for the \ when no space is used */
 
@@ -349,7 +417,7 @@ INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
        INT argc;
 
        if (!_tcsncmp (param, _T("/?"), 2))
-       {               
+       {
                ConOutResPuts(STRING_RMDIR_HELP);
                return 0;
        }
@@ -383,7 +451,7 @@ INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
        }
 
        if (!dir)
-       {        
+       {
                ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
                return 1;
        }
@@ -414,7 +482,7 @@ INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
 INT CommandExit (LPTSTR cmd, LPTSTR param)
 {
        if (!_tcsncmp (param, _T("/?"), 2))
-       {               
+       {
                ConOutResPuts(STRING_EXIT_HELP);
                return 0;
        }
@@ -432,7 +500,7 @@ INT CommandExit (LPTSTR cmd, LPTSTR param)
 INT CommandRem (LPTSTR cmd, LPTSTR param)
 {
        if (!_tcsncmp (param, _T("/?"), 2))
-       {               
+       {
                ConOutResPuts(STRING_REM_HELP);
        }
 
@@ -447,4 +515,10 @@ INT CommandShowCommands (LPTSTR cmd, LPTSTR param)
        return 0;
 }
 
+INT CommandShowCommandsDetail (LPTSTR cmd, LPTSTR param)
+{
+       PrintCommandListDetail ();
+       return 0;
+}
+
 /* EOF */