From dd512f56a91600314840bfdd499dbbbd32fb93bf Mon Sep 17 00:00:00 2001 From: Pierre Schweitzer Date: Mon, 1 May 2017 21:14:38 +0000 Subject: [PATCH] [CMD] 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 | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/reactos/base/shell/cmd/if.c b/reactos/base/shell/cmd/if.c index 72688ce3a41..8f9c5e71b00 100644 --- a/reactos/base/shell/cmd/if.c +++ b/reactos/base/shell/cmd/if.c @@ -114,23 +114,36 @@ INT ExecuteIf(PARSED_COMMAND *Cmd) } 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 (_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; - FindClose(hFind); } + FindClose(hFind); } - else - { - result = (GetFileAttributes(Right) != INVALID_FILE_ATTRIBUTES); - } + + if (IsDir) + Right[Size - 1] = '\\'; } else {