diff options
author | Ron Yorston <rmy@pobox.com> | 2021-10-13 14:21:18 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-10-13 14:21:18 +0100 |
commit | 4859ddcb20616718efbea12c6bf8b27c469b68de (patch) | |
tree | 46c2cf67214f70f3361125d28e3560047b7b384c | |
parent | 35b9217405d9c3ad8ebeab23f072d35327d25cfe (diff) | |
download | busybox-w32-4859ddcb20616718efbea12c6bf8b27c469b68de.tar.gz busybox-w32-4859ddcb20616718efbea12c6bf8b27c469b68de.tar.bz2 busybox-w32-4859ddcb20616718efbea12c6bf8b27c469b68de.zip |
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
-rw-r--r-- | libbb/get_last_path_component.c | 16 |
1 files 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) | |||
31 | */ | 31 | */ |
32 | char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path) | 32 | char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path) |
33 | { | 33 | { |
34 | char *slash = strrchr(path, '/'); | ||
35 | |||
36 | #if ENABLE_PLATFORM_MINGW32 | 34 | #if ENABLE_PLATFORM_MINGW32 |
37 | const char *start = has_dos_drive_prefix(path) ? path+2 : path; | 35 | const char *start = path + root_len(path); |
36 | char *slash = strrchr(start, '/'); | ||
37 | char *bslash = strrchr(start, '\\'); | ||
38 | 38 | ||
39 | if (!slash) | 39 | if (slash && bslash) |
40 | slash = strrchr(path, '\\'); | 40 | slash = MAX(slash, bslash); |
41 | else if (!slash) | ||
42 | slash = bslash; | ||
41 | 43 | ||
44 | if (!slash && has_dos_drive_prefix(path) && path[2] != '\0') | ||
45 | return (char *)path + 2; | ||
42 | if (!slash || (slash == start && !slash[1])) | 46 | if (!slash || (slash == start && !slash[1])) |
43 | return (char*)path; | 47 | return (char*)path; |
44 | #else | 48 | #else |
49 | char *slash = strrchr(path, '/'); | ||
50 | |||
45 | if (!slash || (slash == path && !slash[1])) | 51 | if (!slash || (slash == path && !slash[1])) |
46 | return (char*)path; | 52 | return (char*)path; |
47 | #endif | 53 | #endif |