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 '^'
24 #define ABORT_TO_STARSTAR -2
26 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
27 && *(class) == *(litmatch) \
28 && strncmp((char*)class, litmatch, len) == 0)
30 #if defined STDC_HEADERS || !defined isascii
33 # define ISASCII(c) isascii(c)
37 # define ISBLANK(c) (ISASCII(c) && isblank(c))
39 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
43 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
45 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
48 #define ISPRINT(c) (ISASCII(c) && isprint(c))
49 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
50 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
51 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
52 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
53 #define ISLOWER(c) (ISASCII(c) && islower(c))
54 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
55 #define ISSPACE(c) (ISASCII(c) && isspace(c))
56 #define ISUPPER(c) (ISASCII(c) && isupper(c))
57 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
59 static int force_lower_case
= 0;
61 /* Match pattern "p" against "text" */
62 static int dowild(const uchar
*p
, const uchar
*text
)
66 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
69 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
71 if (force_lower_case
&& ISUPPER(t_ch
))
75 /* Literal match with following character. Note that the test
76 * in "default" handles the p[1] == '\0' failure case. */
84 /* Match anything but '/'. */
90 while (*++p
== '*') {}
95 /* Trailing "**" matches everything. Trailing "*" matches
96 * only if there are no more slash characters. */
98 if (strchr((char*)text
, '/') != NULL
)
106 if ((matched
= dowild(p
, text
)) != FALSE
) {
107 if (!special
|| matched
!= ABORT_TO_STARSTAR
)
109 } else if (!special
&& t_ch
== '/')
110 return ABORT_TO_STARSTAR
;
117 if (p_ch
== NEGATE_CLASS2
)
120 /* Assign literal TRUE/FALSE because of "matched" comparison. */
121 special
= p_ch
== NEGATE_CLASS
? TRUE
: FALSE
;
123 /* Inverted character class. */
137 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
144 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
146 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
147 } else if (p_ch
== '[' && p
[1] == ':') {
150 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
154 if (i
< 0 || p
[-1] != ':') {
155 /* Didn't find ":]", so treat like a normal set. */
162 if (CC_EQ(s
,i
, "alnum")) {
165 } else if (CC_EQ(s
,i
, "alpha")) {
168 } else if (CC_EQ(s
,i
, "blank")) {
171 } else if (CC_EQ(s
,i
, "cntrl")) {
174 } else if (CC_EQ(s
,i
, "digit")) {
177 } else if (CC_EQ(s
,i
, "graph")) {
180 } else if (CC_EQ(s
,i
, "lower")) {
183 } else if (CC_EQ(s
,i
, "print")) {
186 } else if (CC_EQ(s
,i
, "punct")) {
189 } else if (CC_EQ(s
,i
, "space")) {
192 } else if (CC_EQ(s
,i
, "upper")) {
195 } else if (CC_EQ(s
,i
, "xdigit")) {
198 } else /* malformed [:class:] string */
200 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
201 } else if (t_ch
== p_ch
)
203 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
204 if (matched
== special
|| t_ch
== '/')
210 return *text
? FALSE
: TRUE
;
213 /* Match the "pattern" against the "text" string. */
214 int wildmatch(const char *pattern
, const char *text
)
216 return dowild((const uchar
*)pattern
, (const uchar
*)text
) == TRUE
;
219 /* Match the "pattern" against the forced-to-lower-case "text" string. */
220 int iwildmatch(const char *pattern
, const char *text
)
223 force_lower_case
= 1;
224 ret
= dowild((const uchar
*)pattern
, (const uchar
*)text
) == TRUE
;
225 force_lower_case
= 0;