From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:56 +0000 (-0700) Subject: lib/file: Cleanly handle directory separators X-Git-Tag: stable-10.2.0~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d04babd5bc7de9015c0fe881652723498069e7fa;p=thirdparty%2Fopen-vm-tools.git lib/file: Cleanly handle directory separators We want to handle Windows allowance of "/" and "\" as directory separators. Let's have an easy way to check things and use it. This is first in a sequence of changes to improve our handling of directory separators in lib/file. --- diff --git a/open-vm-tools/lib/file/fileStandAlone.c b/open-vm-tools/lib/file/fileStandAlone.c index f7c84fd2d..00c0a9f93 100644 --- a/open-vm-tools/lib/file/fileStandAlone.c +++ b/open-vm-tools/lib/file/fileStandAlone.c @@ -403,6 +403,43 @@ File_PathJoin(const char *dirName, // IN: } +/* + *--------------------------------------------------------------------------- + * + * FileFindLastDirsep -- + * + * Return a pointer to the last directory separator. + * + * Results: + * NULL No directory separator found + * !NULL Pointer to the last directory separator + * + * Side effects: + * None + * + *--------------------------------------------------------------------------- + */ + +static char * +FileFindLastDirsep(const char *pathName, // IN: + size_t len) // IN: +{ + char *p; + + ASSERT(pathName != NULL); + + p = (char *) pathName + len; + + while (p-- != pathName) { + if (File_IsDirsep(*p)) { + return p; + } + } + + return NULL; +} + + /* *--------------------------------------------------------------------------- * @@ -522,10 +559,9 @@ File_StripSlashes(const char *path) // IN: */ #if defined(_WIN32) - while ((i > 1) && (('/' == dir2[i - 1]) || - ('\\' == dir2[i - 1]))) { + while ((i > 1) && File_IsDirsep(dir2[i - 1])) { #else - while ((i > 0) && ('/' == dir2[i - 1])) { + while ((i > 0) && File_IsDirsep(dir2[i - 1])) { #endif i--; } @@ -742,7 +778,7 @@ File_ReplaceExtension(const char *pathName, // IN: memcpy(result, pathName, pathNameLen + 1); - p = strrchr(result, DIRSEPC); + p = FileFindLastDirsep(result, pathNameLen); if (p == NULL) { p = strrchr(result, '.'); } else { @@ -817,7 +853,7 @@ File_RemoveExtension(const char *pathName) // IN: result = Util_SafeStrdup(pathName); - p = strrchr(result, DIRSEPC); + p = FileFindLastDirsep(result, strlen(pathName)); if (p == NULL) { p = strrchr(result, '.'); } else { diff --git a/open-vm-tools/lib/include/file.h b/open-vm-tools/lib/include/file.h index 1a498adee..b90557455 100644 --- a/open-vm-tools/lib/include/file.h +++ b/open-vm-tools/lib/include/file.h @@ -414,6 +414,34 @@ int File_MakeSafeTemp(const char *tag, Bool File_DoesVolumeSupportAcls(const char *pathName); +/* + *--------------------------------------------------------------------------- + * + * File_IsDirsep -- + * + * Is the argument character a directory separator? + * + * Results: + * TRUE Yes + * FALSE No + * + * Side effects: + * None + * + *--------------------------------------------------------------------------- + */ + +static INLINE Bool +File_IsDirsep(int c) // IN: +{ +#if defined(_WIN32) + return (c == '/') || (c == '\\'); // Until util.h dependencies work out +#else + return c == '/'; +#endif +} + + #if defined(__cplusplus) } // extern "C" #endif