diff --git a/userspace/libraries/LibC/fnmatch.cpp b/userspace/libraries/LibC/fnmatch.cpp index cae3b6ea..110d7642 100644 --- a/userspace/libraries/LibC/fnmatch.cpp +++ b/userspace/libraries/LibC/fnmatch.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,8 @@ static int fnmatch_impl(const char* pattern, const char* string, int flags, bool leading) { + const bool ignore_case = !!(flags & FNM_IGNORECASE); + while (*pattern) { if ((flags & FNM_PERIOD) && leading && *string == '.' && *pattern != '.') @@ -34,9 +37,13 @@ static int fnmatch_impl(const char* pattern, const char* string, int flags, bool uint8_t ch; uint32_t bitmap[0x100 / 8] {}; while ((ch = *pattern++) != ']') + { + if (ignore_case) + ch = tolower(ch); bitmap[ch / 32] |= 1 << (ch % 32); + } - ch = *string++; + ch = ignore_case ? tolower(*string++) : *string++; if (!!(bitmap[ch / 32] & (1 << (ch % 32))) == negate) return FNM_NOMATCH; @@ -63,7 +70,10 @@ static int fnmatch_impl(const char* pattern, const char* string, int flags, bool if (*pattern == '\0') break; - if (*pattern != *string) + const char lhs = ignore_case ? tolower(*pattern) : *pattern; + const char rhs = ignore_case ? tolower(*string) : *string; + + if (lhs != rhs) return FNM_NOMATCH; if ((flags & FNM_PATHNAME) && *string == '/') leading = true; diff --git a/userspace/libraries/LibC/include/fnmatch.h b/userspace/libraries/LibC/include/fnmatch.h index 54fbfd11..0f3f4a3c 100644 --- a/userspace/libraries/LibC/include/fnmatch.h +++ b/userspace/libraries/LibC/include/fnmatch.h @@ -11,6 +11,8 @@ __BEGIN_DECLS #define FNM_PATHNAME 0x01 #define FNM_PERIOD 0x02 #define FNM_NOESCAPE 0x04 +#define FNM_CASEFOLD 0x08 +#define FNM_IGNORECASE FNM_CASEFOLD int fnmatch(const char* pattern, const char* string, int flags);