From 3d3c5ab4006de7a80af421ddfeb2cad699fb03f0 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Thu, 30 Oct 2014 13:11:27 +0900 Subject: [PATCH] cmd: Add a helper function to check if a path ends with a backslash. --- programs/cmd/builtins.c | 6 +++--- programs/cmd/directory.c | 9 ++------- programs/cmd/wcmd.h | 5 +++++ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index f7db4a9dbe8..6751d3eec39 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -798,12 +798,12 @@ void WCMD_copy(WCHAR * args) { /* If parameter is a directory, ensure it ends in \ */ attributes = GetFileAttributesW(destname); - if ((destname[strlenW(destname) - 1] == '\\') || + if (ends_with_backslash( destname ) || ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY))) { destisdirectory = TRUE; - if (!(destname[strlenW(destname) - 1] == '\\')) strcatW(destname, slashW); + if (!ends_with_backslash( destname )) strcatW(destname, slashW); WINE_TRACE("Directory, so full name is now '%s'\n", wine_dbgstr_w(destname)); } } @@ -880,7 +880,7 @@ void WCMD_copy(WCHAR * args) { /* If parameter is a directory, ensure it ends in \* */ attributes = GetFileAttributesW(srcpath); - if (srcpath[strlenW(srcpath) - 1] == '\\') { + if (ends_with_backslash( srcpath )) { /* We need to know where the filename part starts, so append * and recalculate the full resulting path */ diff --git a/programs/cmd/directory.c b/programs/cmd/directory.c index 678e2a172d4..91142be6708 100644 --- a/programs/cmd/directory.c +++ b/programs/cmd/directory.c @@ -833,13 +833,8 @@ void WCMD_directory (WCHAR *args) if ((strchrW(path, '*') == NULL) && (strchrW(path, '%') == NULL)) { status = GetFileAttributesW(path); if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) { - if (path[strlenW(path)-1] == '\\') { - strcatW (path, starW); - } - else { - static const WCHAR slashStarW[] = {'\\','*','\0'}; - strcatW (path, slashStarW); - } + if (!ends_with_backslash( path )) strcatW( path, slashW ); + strcatW (path, starW); } } else { /* Special case wildcard search with no extension (ie parameters ending in '.') as diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h index e5a013842c8..c135621dd30 100644 --- a/programs/cmd/wcmd.h +++ b/programs/cmd/wcmd.h @@ -148,6 +148,11 @@ static inline WCHAR *heap_strdupW(const WCHAR *str) return ret; } +static inline BOOL ends_with_backslash( const WCHAR *path ) +{ + return path[0] && path[strlenW(path) - 1] == '\\'; +} + /* Data structure to hold context when executing batch files */ typedef struct _BATCH_CONTEXT {