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.
14 /* What character marks an inverted character class? */
15 #define NEGATE_CLASS '!'
16 #define NEGATE_CLASS2 '^'
21 #define ABORT_TO_STARSTAR -2
23 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
24 && *(class) == *(litmatch) \
25 && strncmp((char*)class, litmatch, len) == 0)
27 #if defined STDC_HEADERS || !defined isascii
30 # define ISASCII(c) isascii(c)
34 # define ISBLANK(c) (ISASCII(c) && isblank(c))
36 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
40 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
42 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
45 #define ISPRINT(c) (ISASCII(c) && isprint(c))
46 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
47 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
48 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
49 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
50 #define ISLOWER(c) (ISASCII(c) && islower(c))
51 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
52 #define ISSPACE(c) (ISASCII(c) && isspace(c))
53 #define ISUPPER(c) (ISASCII(c) && isupper(c))
54 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
56 #ifdef WILD_TEST_ITERATIONS
57 int wildmatch_iteration_count
;
60 static int force_lower_case
= 0;
62 /* Match pattern "p" against the a virtually-joined string consisting
63 * of "text" and any strings in array "a". */
64 static int dowild(const uchar
*p
, const uchar
*text
, const uchar
*const *a
)
68 #ifdef WILD_TEST_ITERATIONS
69 wildmatch_iteration_count
++;
72 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
75 while ((t_ch
= *text
) == '\0') {
83 if (force_lower_case
&& ISUPPER(t_ch
))
87 /* Literal match with following character. Note that the test
88 * in "default" handles the p[1] == '\0' failure case. */
96 /* Match anything but '/'. */
102 while (*++p
== '*') {}
107 /* Trailing "**" matches everything. Trailing "*" matches
108 * only if there are no more slash characters. */
111 if (strchr((char*)text
, '/') != NULL
)
113 } while ((text
= *a
++) != NULL
);
119 if ((text
= *a
++) == NULL
)
124 if ((matched
= dowild(p
, text
, a
)) != FALSE
) {
125 if (!special
|| matched
!= ABORT_TO_STARSTAR
)
127 } else if (!special
&& t_ch
== '/')
128 return ABORT_TO_STARSTAR
;
135 if (p_ch
== NEGATE_CLASS2
)
138 /* Assign literal TRUE/FALSE because of "matched" comparison. */
139 special
= p_ch
== NEGATE_CLASS
? TRUE
: FALSE
;
141 /* Inverted character class. */
155 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
162 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
164 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
165 } else if (p_ch
== '[' && p
[1] == ':') {
168 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
172 if (i
< 0 || p
[-1] != ':') {
173 /* Didn't find ":]", so treat like a normal set. */
180 if (CC_EQ(s
,i
, "alnum")) {
183 } else if (CC_EQ(s
,i
, "alpha")) {
186 } else if (CC_EQ(s
,i
, "blank")) {
189 } else if (CC_EQ(s
,i
, "cntrl")) {
192 } else if (CC_EQ(s
,i
, "digit")) {
195 } else if (CC_EQ(s
,i
, "graph")) {
198 } else if (CC_EQ(s
,i
, "lower")) {
201 } else if (CC_EQ(s
,i
, "print")) {
204 } else if (CC_EQ(s
,i
, "punct")) {
207 } else if (CC_EQ(s
,i
, "space")) {
210 } else if (CC_EQ(s
,i
, "upper")) {
213 } else if (CC_EQ(s
,i
, "xdigit")) {
216 } else /* malformed [:class:] string */
218 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
219 } else if (t_ch
== p_ch
)
221 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
222 if (matched
== special
|| t_ch
== '/')
231 } while ((text
= *a
++) != NULL
);
236 /* Match literal string "s" against the a virtually-joined string consisting
237 * of "text" and any strings in array "a". */
238 static int doliteral(const uchar
*s
, const uchar
*text
, const uchar
*const *a
)
240 for ( ; *s
!= '\0'; text
++, s
++) {
241 while (*text
== '\0') {
242 if ((text
= *a
++) == NULL
)
252 } while ((text
= *a
++) != NULL
);
257 /* Return the last "count" path elements from the concatenated string.
258 * We return a string pointer to the start of the string, and update the
259 * array pointer-pointer to point to any remaining string elements. */
260 static const uchar
*trailing_N_elements(const uchar
*const **a_ptr
, int count
)
262 const uchar
*const *a
= *a_ptr
;
263 const uchar
*const *first_a
= a
;
268 while (a
!= first_a
) {
269 const uchar
*s
= *--a
;
270 s
+= strlen((char*)s
);
272 if (*s
== '/' && !--count
) {
287 /* Match the "pattern" against the "text" string. */
288 int wildmatch(const char *pattern
, const char *text
)
290 static const uchar
*nomore
[1]; /* A NULL pointer. */
291 #ifdef WILD_TEST_ITERATIONS
292 wildmatch_iteration_count
= 0;
294 return dowild((const uchar
*)pattern
, (const uchar
*)text
, nomore
) == TRUE
;
297 /* Match the "pattern" against the forced-to-lower-case "text" string. */
298 int iwildmatch(const char *pattern
, const char *text
)
300 static const uchar
*nomore
[1]; /* A NULL pointer. */
302 #ifdef WILD_TEST_ITERATIONS
303 wildmatch_iteration_count
= 0;
305 force_lower_case
= 1;
306 ret
= dowild((const uchar
*)pattern
, (const uchar
*)text
, nomore
) == TRUE
;
307 force_lower_case
= 0;
311 /* Match pattern "p" against the a virtually-joined string consisting
312 * of all the pointers in array "texts" (which has a NULL pointer at the
313 * end). The int "where" can be 0 (normal matching), > 0 (match only
314 * the trailing N slash-separated filename components of "texts"), or < 0
315 * (match the "pattern" at the start or after any slash in "texts"). */
316 int wildmatch_array(const char *pattern
, const char*const *texts
, int where
)
318 const uchar
*p
= (const uchar
*)pattern
;
319 const uchar
*const *a
= (const uchar
*const*)texts
;
323 #ifdef WILD_TEST_ITERATIONS
324 wildmatch_iteration_count
= 0;
328 text
= trailing_N_elements(&a
, where
);
334 if ((matched
= dowild(p
, text
, a
)) != TRUE
&& where
< 0
335 && matched
!= ABORT_ALL
) {
338 if ((text
= (uchar
*)*a
++) == NULL
)
342 if (*text
++ == '/' && (matched
= dowild(p
, text
, a
)) != FALSE
343 && matched
!= ABORT_TO_STARSTAR
)
347 return matched
== TRUE
;
350 /* Match literal string "s" against the a virtually-joined string consisting
351 * of all the pointers in array "texts" (which has a NULL pointer at the
352 * end). The int "where" can be 0 (normal matching), or > 0 (match
353 * only the trailing N slash-separated filename components of "texts"). */
354 int litmatch_array(const char *string
, const char*const *texts
, int where
)
356 const uchar
*s
= (const uchar
*)string
;
357 const uchar
*const *a
= (const uchar
* const*)texts
;
361 text
= trailing_N_elements(&a
, where
);
367 return doliteral(s
, text
, a
) == TRUE
;