Match pattern "p" against "text" */
| 57 | |
| 58 | /* Match pattern "p" against "text" */ |
| 59 | static int dowild(const uchar *p, const uchar *text, unsigned int flags) |
| 60 | { |
| 61 | uchar p_ch; |
| 62 | const uchar *pattern = p; |
| 63 | |
| 64 | for ( ; (p_ch = *p) != '\0'; text++, p++) { |
| 65 | int matched, match_slash, negated; |
| 66 | uchar t_ch, prev_ch; |
| 67 | if ((t_ch = *text) == '\0' && p_ch != '*') |
| 68 | return WM_ABORT_ALL; |
| 69 | if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) |
| 70 | t_ch = tolower(t_ch); |
| 71 | if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) |
| 72 | p_ch = tolower(p_ch); |
| 73 | switch (p_ch) { |
| 74 | case '\\': |
| 75 | /* Literal match with following character. Note that the test |
| 76 | * in "default" handles the p[1] == '\0' failure case. */ |
| 77 | p_ch = *++p; |
| 78 | /* FALLTHROUGH */ |
| 79 | default: |
| 80 | if (t_ch != p_ch) |
| 81 | return WM_NOMATCH; |
| 82 | continue; |
| 83 | case '?': |
| 84 | /* Match anything but '/'. */ |
| 85 | if ((flags & WM_PATHNAME) && t_ch == '/') |
| 86 | return WM_NOMATCH; |
| 87 | continue; |
| 88 | case '*': |
| 89 | if (*++p == '*') { |
| 90 | const uchar *prev_p = p; |
| 91 | while (*++p == '*') {} |
| 92 | if (!(flags & WM_PATHNAME)) |
| 93 | /* without WM_PATHNAME, '*' == '**' */ |
| 94 | match_slash = 1; |
| 95 | else if ((prev_p - pattern < 2 || *(prev_p - 2) == '/') && |
| 96 | (*p == '\0' || *p == '/' || |
| 97 | (p[0] == '\\' && p[1] == '/'))) { |
| 98 | /* |
| 99 | * Assuming we already match 'foo/' and are at |
| 100 | * <star star slash>, just assume it matches |
| 101 | * nothing and go ahead match the rest of the |
| 102 | * pattern with the remaining string. This |
| 103 | * helps make foo/<*><*>/bar (<> because |
| 104 | * otherwise it breaks C comment syntax) match |
| 105 | * both foo/bar and foo/a/bar. |
| 106 | */ |
| 107 | if (p[0] == '/' && |
| 108 | dowild(p + 1, text, flags) == WM_MATCH) |
| 109 | return WM_MATCH; |
| 110 | match_slash = 1; |
| 111 | } else /* WM_PATHNAME is set */ |
| 112 | match_slash = 0; |
| 113 | } else |
| 114 | /* without WM_PATHNAME, '*' == '**' */ |
| 115 | match_slash = flags & WM_PATHNAME ? 0 : 1; |
| 116 | if (*p == '\0') { |