From 6691b2e8d230e1c85d1a31a752f9f5b6933edbb0 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 9 Dec 2018 14:46:14 +0000 Subject: win32: add a case-insensitive version of is_suffixed_with() --- include/mingw.h | 1 + libbb/compare_string_array.c | 28 ++++++++++++++++++++++++++++ win32/mingw.c | 3 +-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index b8c0b12b5..a1869227d 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -448,6 +448,7 @@ int kill_SIGTERM_by_handle(HANDLE process, int exit_code); #define find_mount_point(n, s) find_mount_point(n) char *is_prefixed_with_case(const char *string, const char *key) FAST_FUNC; +char *is_suffixed_with_case(const char *string, const char *key) FAST_FUNC; void qsort_string_vector_case(char **sv, unsigned count) FAST_FUNC; /* diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index 7ccdaef8a..856739c41 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -45,6 +45,7 @@ char* FAST_FUNC is_prefixed_with_case(const char *string, const char *key) * beginning of prefix key in string. If key is an empty string return pointer * to the end of string. */ +#if !ENABLE_PLATFORM_MINGW32 char* FAST_FUNC is_suffixed_with(const char *string, const char *key) { size_t key_len = strlen(key); @@ -59,6 +60,33 @@ char* FAST_FUNC is_suffixed_with(const char *string, const char *key) return NULL; } +#else +static char* FAST_FUNC is_suffixed(const char *string, const char *key, + int (*fn)(const char *, const char*)) +{ + size_t key_len = strlen(key); + ssize_t len_diff = strlen(string) - key_len; + + if (len_diff >= 0) { + string += len_diff; + if (fn(string, key) == 0) { + return (char*)string; + } + } + + return NULL; +} + +char* FAST_FUNC is_suffixed_with(const char *string, const char *key) +{ + return is_suffixed(string, key, strcmp); +} + +char* FAST_FUNC is_suffixed_with_case(const char *string, const char *key) +{ + return is_suffixed(string, key, strcasecmp); +} +#endif /* returns the array index of the string */ /* (index of first match is returned, or -1) */ diff --git a/win32/mingw.c b/win32/mingw.c index 10169266d..162fae45b 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -332,8 +332,7 @@ static int has_exec_format(const char *name) unsigned char buf[1024]; /* special case: skip DLLs, there are thousands of them! */ - n = strlen(name); - if (n > 4 && !strcasecmp(name+n-4, ".dll")) + if (is_suffixed_with_case(name, ".dll")) return 0; n = open_read_close(name, buf, sizeof(buf)); -- cgit v1.2.3-55-g6feb