From 4859ddcb20616718efbea12c6bf8b27c469b68de Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 13 Oct 2021 14:21:18 +0100 Subject: win32: fix bb_get_last_path_component_nostrip() Finding the last component of a path didn't properly handle a mixture of slashes and backslashes, paths relative to another drive or bare UNC paths. The following were incorrect: $ basename /dir\\file dir\file $ basename c:file c:file $ basename //HOST/share share With this patch the results are now: $ basename /dir\\file file $ basename c:file file $ basename //HOST/share //HOST/share --- libbb/get_last_path_component.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c index 00649620d..06033e139 100644 --- a/libbb/get_last_path_component.c +++ b/libbb/get_last_path_component.c @@ -31,17 +31,23 @@ const char* FAST_FUNC bb_basename(const char *name) */ char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path) { - char *slash = strrchr(path, '/'); - #if ENABLE_PLATFORM_MINGW32 - const char *start = has_dos_drive_prefix(path) ? path+2 : path; + const char *start = path + root_len(path); + char *slash = strrchr(start, '/'); + char *bslash = strrchr(start, '\\'); - if (!slash) - slash = strrchr(path, '\\'); + if (slash && bslash) + slash = MAX(slash, bslash); + else if (!slash) + slash = bslash; + if (!slash && has_dos_drive_prefix(path) && path[2] != '\0') + return (char *)path + 2; if (!slash || (slash == start && !slash[1])) return (char*)path; #else + char *slash = strrchr(path, '/'); + if (!slash || (slash == path && !slash[1])) return (char*)path; #endif -- cgit v1.2.3-55-g6feb