- Get autochk, calc, cmd, devmgr, expand, format, gettype, hostname, lsass, msconfig...
[reactos.git] / reactos / subsys / system / cmd / filecomp.c
index e3f75bc..9042a29 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
 
-VOID CompleteFilename (LPTSTR str, INT charcount)
+VOID CompleteFilename (LPTSTR str, UINT charcount)
 {
        WIN32_FIND_DATA file;
        HANDLE hFile;
@@ -171,11 +171,7 @@ VOID CompleteFilename (LPTSTR str, INT charcount)
 
                if(!perfectmatch)
                {
-#ifdef __REACTOS__
-                       Beep (440, 50);
-#else
                        MessageBeep (-1);
-#endif
                }
        }
        else
@@ -193,11 +189,7 @@ VOID CompleteFilename (LPTSTR str, INT charcount)
                        }
                }
 
-#ifdef __REACTOS__
-               Beep (440, 50);
-#else
                MessageBeep (-1);
-#endif
        }
 }
 
@@ -346,11 +338,7 @@ BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
        else
        {
                /* no match found */
-#ifdef __REACTOS__
-               Beep (440, 50);
-#else
                MessageBeep (-1);
-#endif
                return FALSE;
        }
 
@@ -376,7 +364,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
        /* number of quotes in the string */
        INT nQuotes = 0;
        /* used in for loops */
-       INT i;
+       UINT i;
        /* Char number to break the string at */
        INT PBreak = 0;
        INT SBreak = 0;
@@ -396,7 +384,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
                        nQuotes++;
 
        /* Find the prefix and suffix */
-       if(nQuotes % 2 && nQuotes)
+       if(nQuotes % 2 && nQuotes > 1)
        {
                /* Odd number of quotes.  Just start from the last " */
                /* THis is the way MS does it, and is an easy way out */
@@ -468,7 +456,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
                        bInside = !bInside;
                if(str[i] == _T(' ') && !bInside)
                        SBreak = i;
-               if((!_tcsncmp(&str[i], _T(" "),1) || !_tcsncmp(&str[i], _T("\\"),1)) && !bInside)
+               if((str[i] == _T(' ') || str[i] == _T('\\')) && !bInside)
                        PBreak = i;
 
        }
@@ -477,7 +465,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
        _tcscpy(szSuffix,&strIN[SBreak]);       
   strIN[PBreak] = _T('\0');
        _tcscpy(szPrefix,strIN);
-       if(!_tcsncmp(&szPrefix[_tcslen(szPrefix) - 2],_T("\""),1))
+       if(szPrefix[_tcslen(szPrefix) - 2] == _T('\"'))
        {
                /* need to remove the " right before a \ at the end to
                   allow the next stuff to stay inside one set of quotes
@@ -487,7 +475,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
        }
 
 }
- int compare(const void *arg1,const void *arg2)
+ int __cdecl compare(const void *arg1,const void *arg2)
  {
        FileName * File1;
        FileName * File2;
@@ -509,7 +497,7 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
        return ret;
  }
 
-VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
+VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, UINT cusor)
 {
        /* Length of string before we complete it */
        INT StartLength;
@@ -534,15 +522,22 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
        /* Number of files */
        INT FileListSize = 0;
        /* Used for loops */
-       INT i;
-       INT ii;
+       UINT i;
        /* Editable string of what was passed in */
        TCHAR str[MAX_PATH];
        /* Keeps track of what element was last selected */
        static INT Sel;
        BOOL NeededQuote = FALSE;
+       BOOL ShowAll = TRUE;
+       TCHAR * line = strIN; 
+
        strOut[0] = _T('\0');
 
+       while (_istspace (*line))
+                       line++; 
+       if(!_tcsnicmp (line, _T("rd "), 3) || !_tcsnicmp (line, _T("cd "), 3))
+               ShowAll = FALSE;
+
        /* Copy the string, str can be edited and orginal should not be */
        _tcscpy(str,strIN);
        _tcscpy(szOrginal,strIN);
@@ -573,13 +568,12 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
                        no quote at the END of the full name */
                FindPrefixAndSuffix(str,szPrefix,szBaseWord);
                /* Strip quotes */
-               for(i = 0; i < _tcslen(szBaseWord); i++)
+               for(i = 0; i < _tcslen(szBaseWord); )
                {
-                       if(!_tcsncmp(&szBaseWord[i], _T("\""),1))
-                       {
-                               for(ii = i; ii < (_tcslen(szBaseWord)); ii++)
-                                       szBaseWord[ii] = szBaseWord[ii + 1];
-                       }
+                       if(szBaseWord[i] == _T('\"'))
+                               memmove(&szBaseWord[i],&szBaseWord[i + 1], _tcslen(&szBaseWord[i]) * sizeof(TCHAR));
+                       else
+                               i++;
                }
 
                /* clear it out */
@@ -617,6 +611,15 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
                if(!_tcscmp (file.cFileName, _T(".")) ||
                        !_tcscmp (file.cFileName, _T("..")))
                        continue;
+               
+               /* Don't show files when they are doing 'cd' or 'rd' */
+               if(!ShowAll)
+               {
+                       DWORD attr = GetFileAttributes (file.cFileName);
+                       if(attr != 0xFFFFFFFF && (!(attr & FILE_ATTRIBUTE_DIRECTORY)))
+                               continue;
+               }
+
                /* Add the file to the list of files */
       if(FileList == NULL) 
       {
@@ -642,7 +645,17 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
  
        }while(FindNextFile(hFile,&file));
 
-       
+       /* Check the size of the list to see if we
+          found any matches */
+       if(FileListSize == 0)
+       {
+               _tcscpy(strOut,szOrginal);
+               CloseHandle(hFile);
+               if(FileList != NULL) 
+                       free(FileList);
+               return;
+
+       }
        /* Sort the files */
        qsort(FileList,FileListSize,sizeof(FileName), compare);
 
@@ -691,17 +704,12 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, INT cusor)
                                LastSpace = i;
 
                }
-               /* insert the space and move things around */
-               if(_tcsncmp(&szPrefix[LastSpace + 1],_T("\""),1) && LastSpace != -1)
+               /* insert the quoation and move things around */
+               if(szPrefix[LastSpace + 1] != _T('\"') && LastSpace != -1)
                {
-                       /* add another char or you will lose a null char ending */
-                       _tcsncat(szPrefix,&szPrefix[_tcslen(szPrefix) - 1],1);
-                       for(i = _tcslen(szPrefix) - 1; i > LastSpace; i--)
-                       {
-                               szPrefix[i] = szPrefix[i - 1];
-                       }
+                       memmove ( &szPrefix[LastSpace+1], &szPrefix[LastSpace], (_tcslen(szPrefix)-LastSpace+1) * sizeof(TCHAR) );
                        
-                       if(LastSpace + 1 == _tcslen(szPrefix))
+                       if((UINT)(LastSpace + 1) == _tcslen(szPrefix))
                        {
                                _tcscat(szPrefix,_T("\""));
                        }