/* Glob/lazy pattern matching */ static bool __match_glob(const char *str, const char *pat, bool ignore_space, bool case_ins) { while (*str && *pat && *pat != '*') { if (ignore_space) { /* Ignore spaces for lazy matching */ if (isspace(*str)) { str++; continue; } if (isspace(*pat)) { pat++; continue; } } if (*pat == '?') { /* Matches any single character */ str++; pat++; continue; } else if (*pat == '[') /* Character classes/Ranges */ if (__match_charclass(pat + 1, *str, &pat)) { str++; continue; } else return false; else if (*pat == '\\') /* Escaped char match as normal char */ pat++; if (case_ins) { if (tolower(*str) != tolower(*pat)) return false; } else if (*str != *pat) return false; str++; pat++; } /* Check wild card */ if (*pat == '*') { while (*pat == '*') pat++; if (!*pat) /* Tail wild card matches all */ return true; while (*str) if (__match_glob(str++, pat, ignore_space, case_ins)) return true; } return !*str && !*pat; }
/** * strglobmatch - glob expression pattern matching * @str: the target string to match * @pat: the pattern string to match * * This returns true if the @str matches @pat. @pat can includes wildcards * ('*','?') and character classes ([CHARS], complementation and ranges are * also supported). Also, this supports escape character ('\') to use special * characters as normal character. * * Note: if @pat syntax is broken, this always returns false. */ bool strglobmatch(const char *str, const char *pat) { return __match_glob(str, pat, false); }
/** * strlazymatch - matching pattern strings lazily with glob pattern * @str: the target string to match * @pat: the pattern string to match * * This is similar to strglobmatch, except this ignores spaces in * the target string. */ bool strlazymatch(const char *str, const char *pat) { return __match_glob(str, pat, true); }
bool strglobmatch_nocase(const char *str, const char *pat) { return __match_glob(str, pat, false, true); }