]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: Cleanly handle directory separators
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:56 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:56 +0000 (11:23 -0700)
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.

open-vm-tools/lib/file/fileStandAlone.c
open-vm-tools/lib/include/file.h

index f7c84fd2d3cd69b734ec532a8ba62cb746de6522..00c0a9f93eb86f7dd142b0336722f87c18c9a359 100644 (file)
@@ -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 {
index 1a498adee08c563f31b7667f5d17a5eb0b634d3c..b905574552ecb2e380bba2208aea57cabef07b0d 100644 (file)
@@ -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