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 '^'
27 #define ABORT_TO_STARSTAR -2
29 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
30 && *(class) == *(litmatch) \
31 && strncmp((char*)class, litmatch, len) == 0)
33 #if defined STDC_HEADERS || !defined isascii
36 # define ISASCII(c) isascii(c)
40 # define ISBLANK(c) (ISASCII(c) && isblank(c))
42 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
46 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
48 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
51 #define ISPRINT(c) (ISASCII(c) && isprint(c))
52 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
53 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
54 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
55 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
56 #define ISLOWER(c) (ISASCII(c) && islower(c))
57 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
58 #define ISSPACE(c) (ISASCII(c) && isspace(c))
59 #define ISUPPER(c) (ISASCII(c) && isupper(c))
60 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
62 static int force_lower_case
= 0;
64 /* Match pattern "p" against "text" */
65 static int dowild(const uchar
*p
, const uchar
*text
)
69 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
72 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
74 if (force_lower_case
&& ISUPPER(t_ch
))
78 /* Literal match with following character. Note that the test
79 * in "default" handles the p[1] == '\0' failure case. */
87 /* Match anything but '/'. */
93 while (*++p
== '*') {}
98 /* Trailing "**" matches everything. Trailing "*" matches
99 * only if there are no more slash characters. */
101 if (strchr((char*)text
, '/') != NULL
)
109 if ((matched
= dowild(p
, text
)) != NOMATCH
) {
110 if (!special
|| matched
!= ABORT_TO_STARSTAR
)
112 } else if (!special
&& t_ch
== '/')
113 return ABORT_TO_STARSTAR
;
120 if (p_ch
== NEGATE_CLASS2
)
123 /* Assign literal TRUE/FALSE because of "matched" comparison. */
124 special
= p_ch
== NEGATE_CLASS
? TRUE
: FALSE
;
126 /* Inverted character class. */
140 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
147 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
149 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
150 } else if (p_ch
== '[' && p
[1] == ':') {
153 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
157 if (i
< 0 || p
[-1] != ':') {
158 /* Didn't find ":]", so treat like a normal set. */
165 if (CC_EQ(s
,i
, "alnum")) {
168 } else if (CC_EQ(s
,i
, "alpha")) {
171 } else if (CC_EQ(s
,i
, "blank")) {
174 } else if (CC_EQ(s
,i
, "cntrl")) {
177 } else if (CC_EQ(s
,i
, "digit")) {
180 } else if (CC_EQ(s
,i
, "graph")) {
183 } else if (CC_EQ(s
,i
, "lower")) {
186 } else if (CC_EQ(s
,i
, "print")) {
189 } else if (CC_EQ(s
,i
, "punct")) {
192 } else if (CC_EQ(s
,i
, "space")) {
195 } else if (CC_EQ(s
,i
, "upper")) {
198 } else if (CC_EQ(s
,i
, "xdigit")) {
201 } else /* malformed [:class:] string */
203 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
204 } else if (t_ch
== p_ch
)
206 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
207 if (matched
== special
|| t_ch
== '/')
213 return *text
? NOMATCH
: MATCH
;
216 /* Match the "pattern" against the "text" string. */
217 int wildmatch(const char *pattern
, const char *text
)
219 return dowild((const uchar
*)pattern
, (const uchar
*)text
);
222 /* Match the "pattern" against the forced-to-lower-case "text" string. */
223 int iwildmatch(const char *pattern
, const char *text
)
226 force_lower_case
= 1;
227 ret
= dowild((const uchar
*)pattern
, (const uchar
*)text
);
228 force_lower_case
= 0;