From 9197a9ef026e3d57d291f05ea6f91956d152d116 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 4 Jan 2024 07:58:00 +0000 Subject: libbb: introduce last_char_is_dir_sep() Add a convenience function to determine if the last character of a string is a directory separator. Adds 16-32 bytes. --- include/libbb.h | 1 + libbb/concat_path_file.c | 4 ++-- libbb/last_char_is.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index a469bfe03..6549c8c83 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -589,6 +589,7 @@ char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC; const char *bb_basename(const char *name) FAST_FUNC; /* NB: can violate const-ness (similarly to strchr) */ char *last_char_is(const char *s, int c) FAST_FUNC; +char *last_char_is_dir_sep(const char *s) FAST_FUNC; const char* endofname(const char *name) FAST_FUNC; char *is_prefixed_with(const char *string, const char *key) FAST_FUNC; char *is_suffixed_with(const char *string, const char *key) FAST_FUNC; diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c index 81c7481ca..3afb0e3a4 100644 --- a/libbb/concat_path_file.c +++ b/libbb/concat_path_file.c @@ -22,8 +22,8 @@ char* FAST_FUNC concat_path_file(const char *path, const char *filename) if (!path) path = ""; #if ENABLE_PLATFORM_MINGW32 - lc = last_char_is(path, '/') ?: last_char_is(path, '\\'); - while (*filename == '/' || *filename == '\\') + lc = last_char_is_dir_sep(path); + while (is_dir_sep(*filename)) filename++; #else lc = last_char_is(path, '/'); diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c index fba05f974..c2cd92174 100644 --- a/libbb/last_char_is.c +++ b/libbb/last_char_is.c @@ -17,3 +17,14 @@ char* FAST_FUNC last_char_is(const char *s, int c) s++; return (*s == (char)c) ? (char *) s : NULL; } + +#if ENABLE_PLATFORM_MINGW32 +char* FAST_FUNC last_char_is_dir_sep(const char *s) +{ + if (!s[0]) + return NULL; + while (s[1]) + s++; + return is_dir_sep(*s)? (char *) s : NULL; +} +#endif -- cgit v1.2.3-55-g6feb