]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: Rework File_ReplaceExtension and File_RemoveExtension
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:55 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:55 +0000 (11:23 -0700)
Fix a bug and simplify the code.

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

index f67fd56903bb1016eebdc74b8d6f2194f7368494..b98bd90e1455994d9d75a7a5bd5a4aaa0d73bd5a 100644 (file)
@@ -724,64 +724,66 @@ File_ReplaceExtension(const char *pathName,      // IN:
                       uint32 numExtensions,      // IN:
                       ...)                       // IN:
 {
-   char *path;
-   char *base;
+   char *p;
+   char *place;
    char *result;
-   va_list arguments;
-   UnicodeIndex index;
+   size_t newExtLen;
+   size_t resultLen;
+   size_t pathNameLen;
 
    ASSERT(pathName);
    ASSERT(newExtension);
    ASSERT(*newExtension == '.');
 
-   File_GetPathName(pathName, &path, &base);
+   pathNameLen = strlen(pathName);
+   newExtLen = strlen(newExtension);
+   resultLen = pathNameLen + newExtLen + 1;
+   result = Util_SafeMalloc(resultLen);
 
-   index = Unicode_FindLast(base, ".");
+   memcpy(result, pathName, pathNameLen + 1);
 
-   if (index != UNICODE_INDEX_NOT_FOUND) {
-      char *oldBase = base;
-
-      if (numExtensions) {
-         uint32 i;
+   p = strrchr(result, DIRSEPC);
+   if (p == NULL) {
+       p = strrchr(result, '.');
+   } else {
+       p = strrchr(p, '.');
+   }
 
-         /*
-          * Only truncate the old extension from the base if it exists in
-          * in the valid extensions list.
-          */
+   if (p == NULL) {
+      /* No extension... just append */
+      place = &result[pathNameLen];  // The NUL
+   } else if (numExtensions == 0) {
+      /* Always truncate the old extension if extension list is empty. */
+      place = p;  // The '.'
+   } else {
+      uint32 i;
+      va_list arguments;
 
-         va_start(arguments, numExtensions);
+      /*
+       * Only truncate the old extension if it exists in the valid
+       * extensions list.
+       */
 
-         for (i = 0; i < numExtensions ; i++) {
-            char *oldExtension = va_arg(arguments, char *);
+      place = &result[pathNameLen];  // The NUL
 
-            ASSERT(*oldExtension == '.');
+      va_start(arguments, numExtensions);
 
-            if (Unicode_CompareRange(base, index, -1,
-                                     oldExtension, 0, -1, FALSE) == 0) {
-               base = Unicode_Truncate(oldBase, index); // remove '.'
-               break;
-            }
-         }
+      for (i = 0; i < numExtensions ; i++) {
+         char *oldExtension = va_arg(arguments, const char *);
 
-         va_end(arguments);
-      } else {
-         /* Always truncate the old extension if extension list is empty . */
-         base = Unicode_Truncate(oldBase, index); // remove '.'
-      }
+         ASSERT(*oldExtension == '.');
 
-      if (oldBase != base) {
-         Posix_Free(oldBase);
+         if (strcmp(p, oldExtension) == 0) {
+            place = p;  // The '.'
+            break;
+         }
       }
-   }
 
-   if (Unicode_IsEmpty(path)) {
-      result = Unicode_Append(base, newExtension);
-   } else {
-      result = Unicode_Join(path, DIRSEPS, base, newExtension, NULL);
+      va_end(arguments);
    }
 
-   Posix_Free(path);
-   Posix_Free(base);
+   /* Add the new extension - in the appropriate place - to pathName */
+   memcpy(place, newExtension, newExtLen + 1);
 
    return result;
 }
@@ -808,12 +810,25 @@ File_ReplaceExtension(const char *pathName,      // IN:
 char *
 File_RemoveExtension(const char *pathName)  // IN:
 {
-   UnicodeIndex index;
+   char *p;
+   char *result;
 
-   ASSERT(pathName);
+   ASSERT(pathName != NULL);
 
-   index = Unicode_FindLast(pathName, ".");
-   ASSERT(index != UNICODE_INDEX_NOT_FOUND);
+   result = Util_SafeStrdup(pathName);
 
-   return Unicode_Truncate(pathName, index);
+   p = strrchr(result, DIRSEPC);
+   if (p == NULL) {
+       p = strrchr(result, '.');
+   } else {
+       p = strrchr(p, '.');
+   }
+
+   ASSERT(p != NULL);
+
+   if (p != NULL) {
+      *p = '\0';
+   }
+
+   return result;
 }