2 ** Do shell-style pattern matching for ?, \, [], and * characters.
5 ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6 ** Rich $alz is now <rsalz@bbn.com>.
8 ** Modified by Wayne Davison to special-case '/' matching, to make '**'
9 ** work differently than '*', and to fix the character-class code.
13 #include "wildmatch.h"
15 typedef unsigned char uchar
;
17 /* What character marks an inverted character class? */
18 #define NEGATE_CLASS '!'
19 #define NEGATE_CLASS2 '^'
21 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
22 && *(class) == *(litmatch) \
23 && strncmp((char*)class, litmatch, len) == 0)
25 #if defined STDC_HEADERS || !defined isascii
28 # define ISASCII(c) isascii(c)
32 # define ISBLANK(c) (ISASCII(c) && isblank(c))
34 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
38 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
40 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
43 #define ISPRINT(c) (ISASCII(c) && isprint(c))
44 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
45 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
46 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
47 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
48 #define ISLOWER(c) (ISASCII(c) && islower(c))
49 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
50 #define ISSPACE(c) (ISASCII(c) && isspace(c))
51 #define ISUPPER(c) (ISASCII(c) && isupper(c))
52 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
54 /* Match pattern "p" against "text" */
55 static int dowild(const uchar
*p
, const uchar
*text
, unsigned int flags
)
58 const uchar
*pattern
= p
;
60 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
61 int matched
, match_slash
, negated
;
63 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
65 if ((flags
& WM_CASEFOLD
) && ISUPPER(t_ch
))
67 if ((flags
& WM_CASEFOLD
) && ISUPPER(p_ch
))
71 /* Literal match with following character. Note that the test
72 * in "default" handles the p[1] == '\0' failure case. */
80 /* Match anything but '/'. */
81 if ((flags
& WM_PATHNAME
) && t_ch
== '/')
86 const uchar
*prev_p
= p
- 2;
87 while (*++p
== '*') {}
88 if (!(flags
& WM_PATHNAME
))
89 /* without WM_PATHNAME, '*' == '**' */
91 else if ((prev_p
< pattern
|| *prev_p
== '/') &&
92 (*p
== '\0' || *p
== '/' ||
93 (p
[0] == '\\' && p
[1] == '/'))) {
95 * Assuming we already match 'foo/' and are at
96 * <star star slash>, just assume it matches
97 * nothing and go ahead match the rest of the
98 * pattern with the remaining string. This
99 * helps make foo/<*><*>/bar (<> because
100 * otherwise it breaks C comment syntax) match
101 * both foo/bar and foo/a/bar.
104 dowild(p
+ 1, text
, flags
) == WM_MATCH
)
108 return WM_ABORT_MALFORMED
;
110 /* without WM_PATHNAME, '*' == '**' */
111 match_slash
= flags
& WM_PATHNAME
? 0 : 1;
113 /* Trailing "**" matches everything. Trailing "*" matches
114 * only if there are no more slash characters. */
116 if (strchr((char*)text
, '/') != NULL
)
120 } else if (!match_slash
&& *p
== '/') {
122 * _one_ asterisk followed by a slash
123 * with WM_PATHNAME matches the next
126 const char *slash
= strchr((char*)text
, '/');
129 text
= (const uchar
*)slash
;
130 /* the slash is consumed by the top-level for loop */
137 * Try to advance faster when an asterisk is
138 * followed by a literal. We know in this case
139 * that the string before the literal
140 * must belong to "*".
141 * If match_slash is false, do not look past
142 * the first slash as it cannot belong to '*'.
144 if (!is_glob_special(*p
)) {
146 if ((flags
& WM_CASEFOLD
) && ISUPPER(p_ch
))
147 p_ch
= tolower(p_ch
);
148 while ((t_ch
= *text
) != '\0' &&
149 (match_slash
|| t_ch
!= '/')) {
150 if ((flags
& WM_CASEFOLD
) && ISUPPER(t_ch
))
151 t_ch
= tolower(t_ch
);
159 if ((matched
= dowild(p
, text
, flags
)) != WM_NOMATCH
) {
160 if (!match_slash
|| matched
!= WM_ABORT_TO_STARSTAR
)
162 } else if (!match_slash
&& t_ch
== '/')
163 return WM_ABORT_TO_STARSTAR
;
170 if (p_ch
== NEGATE_CLASS2
)
173 /* Assign literal 1/0 because of "matched" comparison. */
174 negated
= p_ch
== NEGATE_CLASS
? 1 : 0;
176 /* Inverted character class. */
190 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
197 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
199 else if ((flags
& WM_CASEFOLD
) && ISLOWER(t_ch
)) {
200 uchar t_ch_upper
= toupper(t_ch
);
201 if (t_ch_upper
<= p_ch
&& t_ch_upper
>= prev_ch
)
204 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
205 } else if (p_ch
== '[' && p
[1] == ':') {
208 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
212 if (i
< 0 || p
[-1] != ':') {
213 /* Didn't find ":]", so treat like a normal set. */
220 if (CC_EQ(s
,i
, "alnum")) {
223 } else if (CC_EQ(s
,i
, "alpha")) {
226 } else if (CC_EQ(s
,i
, "blank")) {
229 } else if (CC_EQ(s
,i
, "cntrl")) {
232 } else if (CC_EQ(s
,i
, "digit")) {
235 } else if (CC_EQ(s
,i
, "graph")) {
238 } else if (CC_EQ(s
,i
, "lower")) {
241 } else if (CC_EQ(s
,i
, "print")) {
244 } else if (CC_EQ(s
,i
, "punct")) {
247 } else if (CC_EQ(s
,i
, "space")) {
250 } else if (CC_EQ(s
,i
, "upper")) {
253 else if ((flags
& WM_CASEFOLD
) && ISLOWER(t_ch
))
255 } else if (CC_EQ(s
,i
, "xdigit")) {
258 } else /* malformed [:class:] string */
260 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
261 } else if (t_ch
== p_ch
)
263 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
264 if (matched
== negated
||
265 ((flags
& WM_PATHNAME
) && t_ch
== '/'))
271 return *text
? WM_NOMATCH
: WM_MATCH
;
274 /* Match the "pattern" against the "text" string. */
275 int wildmatch(const char *pattern
, const char *text
,
276 unsigned int flags
, struct wildopts
*wo
)
278 return dowild((const uchar
*)pattern
, (const uchar
*)text
, flags
);