[CMD]
authorPierre Schweitzer <pierre@reactos.org>
Mon, 1 May 2017 21:14:38 +0000 (21:14 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Mon, 1 May 2017 21:14:38 +0000 (21:14 +0000)
Fix and simplify implementation of "IF EXIST":
- Don't make any difference between wildcard search and normal search
- This fixes handling DOS devices search (ie, IF EXIST C:\ReactOS\NUL now works)
- This fixes handling pagefile.sys without requiring specifing rights
- Also fix handling directory search, terminated with a \

CORE-11784

svn path=/trunk/; revision=74444

reactos/base/shell/cmd/if.c

index 72688ce..8f9c5e7 100644 (file)
@@ -114,23 +114,36 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
     }
     else if (Cmd->If.Operator == IF_EXIST)
     {
     }
     else if (Cmd->If.Operator == IF_EXIST)
     {
+        BOOL IsDir;
+        INT Size;
+        WIN32_FIND_DATA f;
+        HANDLE hFind;
+
         /* IF EXIST filename: check if file exists (wildcards allowed) */
         StripQuotes(Right);
 
         /* IF EXIST filename: check if file exists (wildcards allowed) */
         StripQuotes(Right);
 
-        if (_tcschr(Right, _T('*')) || _tcschr(Right, _T('?')))
+        Size = _tcslen(Right);
+        IsDir = (Right[Size - 1] == '\\');
+        if (IsDir)
+            Right[Size - 1] = 0;
+
+
+        hFind = FindFirstFile(Right, &f);
+        if (hFind != INVALID_HANDLE_VALUE)
         {
         {
-            WIN32_FIND_DATA f;
-            HANDLE hFind = FindFirstFile(Right, &f);
-            if (hFind != INVALID_HANDLE_VALUE)
+            if (IsDir)
+            {
+                result = ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
+            }
+            else
             {
                 result = TRUE;
             {
                 result = TRUE;
-                FindClose(hFind);
             }
             }
+            FindClose(hFind);
         }
         }
-        else
-        {
-            result = (GetFileAttributes(Right) != INVALID_FILE_ATTRIBUTES);
-        }
+
+        if (IsDir)
+            Right[Size - 1] = '\\';
     }
     else
     {
     }
     else
     {