[PATCH 4/5] [programs/cmd] Fix 'if exist' with a directory\ as a parameter

Jason Edmeades us at edmeades.me.uk
Sun Aug 5 17:15:29 CDT 2018


Fixes regression reported under bug#45506

'if exists' takes a parameter which can be directory, directory\ or directory\. for example, and should
equate to true if the directory exists. The syntax directory\ is explicitly rejected by FindFirstFile and
hence was not working - look for this specific case, and if found append a '.'.

Note I did consider just removing the trailing \, but that could lead to false positives if there really
was a file with the name being looked up as file\. By appending a '.' it forces treatment of the parameter
as a directory.

Signed-off-by: Jason Edmeades <us at edmeades.me.uk>
---
 programs/cmd/builtins.c                  |  9 ++++++++-
 programs/cmd/tests/test_builtins.cmd     | 20 ++++++++++++++++++++
 programs/cmd/tests/test_builtins.cmd.exp |  4 ++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c
index 133ee53df2..bb6367699c 100644
--- a/programs/cmd/builtins.c
+++ b/programs/cmd/builtins.c
@@ -2872,7 +2872,14 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
   }
   else if (!lstrcmpiW (condition, existW)) {
     WIN32_FIND_DATAW fd;
-    HANDLE hff = FindFirstFileW(WCMD_parameter(p, 1+negate, NULL, FALSE, FALSE), &fd);
+    HANDLE hff;
+    WCHAR *param = WCMD_parameter(p, 1+negate, NULL, FALSE, FALSE);
+    int    strlen = strlenW(param);
+
+    /* FindFirstFile does not like a directory path ending in '\', append a '.' */
+    if (strlen && param[strlen-1] == '\\') strcatW(param, dotW);
+
+    hff = FindFirstFileW(param, &fd);
     test = (hff != INVALID_HANDLE_VALUE );
     if (test) FindClose(hff);
 
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index b838485f45..d9338003c9 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -1029,6 +1029,26 @@ if exist idontexist\ba* (
 ) else (
    echo exist wildcard bad subdir broken works
 )
+if exist subdir (
+   echo exist subdir ok
+) else (
+   echo ERROR exist subdir not working
+)
+if exist subdir\. (
+   echo exist subdir with . ok
+) else (
+   echo ERROR exist subdir with . not working
+)
+if exist subdir\ (
+   echo exist subdir with \ ok
+) else (
+   echo ERROR exist subdir with \ not working
+)
+if exist "subdir\" (
+   echo exist subdir with \ and quotes ok
+) else (
+   echo ERROR exist subdir with \ and quotes not working
+)
 del foo subdir\bar
 rd subdir
 
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index ffdd316cb6..539014ce21 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -780,6 +780,10 @@ exist simple wildcard works
 exist wildcard works
 negate exist wildcard works
 exist wildcard bad subdir broken works
+exist subdir ok
+exist subdir with . ok
+exist subdir with \ ok
+exist subdir with \ and quotes ok
 ------ for numbers
 negative numbers handled
 negative numbers handled
-- 
2.17.1




More information about the wine-devel mailing list