From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:55 +0000 (-0700) Subject: lib/file: Rework File_ReplaceExtension and File_RemoveExtension X-Git-Tag: stable-10.2.0~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a518cc08a6e6f77a665eefa0f29b19e5077a8445;p=thirdparty%2Fopen-vm-tools.git lib/file: Rework File_ReplaceExtension and File_RemoveExtension Fix a bug and simplify the code. --- diff --git a/open-vm-tools/lib/file/fileStandAlone.c b/open-vm-tools/lib/file/fileStandAlone.c index f67fd5690..b98bd90e1 100644 --- a/open-vm-tools/lib/file/fileStandAlone.c +++ b/open-vm-tools/lib/file/fileStandAlone.c @@ -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; }