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 static int force_lower_case
= 0;
58 /* Match pattern "p" against "text" */
59 static int dowild(const uchar
*p
, const uchar
*text
)
63 for ( ; (p_ch
= *p
) != '\0'; text
++, p
++) {
66 if ((t_ch
= *text
) == '\0' && p_ch
!= '*')
68 if (force_lower_case
&& ISUPPER(t_ch
))
72 /* Literal match with following character. Note that the test
73 * in "default" handles the p[1] == '\0' failure case. */
81 /* Match anything but '/'. */
87 while (*++p
== '*') {}
92 /* Trailing "**" matches everything. Trailing "*" matches
93 * only if there are no more slash characters. */
95 if (strchr((char*)text
, '/') != NULL
)
103 if ((matched
= dowild(p
, text
)) != FALSE
) {
104 if (!special
|| matched
!= ABORT_TO_STARSTAR
)
106 } else if (!special
&& t_ch
== '/')
107 return ABORT_TO_STARSTAR
;
114 if (p_ch
== NEGATE_CLASS2
)
117 /* Assign literal TRUE/FALSE because of "matched" comparison. */
118 special
= p_ch
== NEGATE_CLASS
? TRUE
: FALSE
;
120 /* Inverted character class. */
134 } else if (p_ch
== '-' && prev_ch
&& p
[1] && p
[1] != ']') {
141 if (t_ch
<= p_ch
&& t_ch
>= prev_ch
)
143 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
144 } else if (p_ch
== '[' && p
[1] == ':') {
147 for (s
= p
+= 2; (p_ch
= *p
) && p_ch
!= ']'; p
++) {} /*SHARED ITERATOR*/
151 if (i
< 0 || p
[-1] != ':') {
152 /* Didn't find ":]", so treat like a normal set. */
159 if (CC_EQ(s
,i
, "alnum")) {
162 } else if (CC_EQ(s
,i
, "alpha")) {
165 } else if (CC_EQ(s
,i
, "blank")) {
168 } else if (CC_EQ(s
,i
, "cntrl")) {
171 } else if (CC_EQ(s
,i
, "digit")) {
174 } else if (CC_EQ(s
,i
, "graph")) {
177 } else if (CC_EQ(s
,i
, "lower")) {
180 } else if (CC_EQ(s
,i
, "print")) {
183 } else if (CC_EQ(s
,i
, "punct")) {
186 } else if (CC_EQ(s
,i
, "space")) {
189 } else if (CC_EQ(s
,i
, "upper")) {
192 } else if (CC_EQ(s
,i
, "xdigit")) {
195 } else /* malformed [:class:] string */
197 p_ch
= 0; /* This makes "prev_ch" get set to 0. */
198 } else if (t_ch
== p_ch
)
200 } while (prev_ch
= p_ch
, (p_ch
= *++p
) != ']');
201 if (matched
== special
|| t_ch
== '/')
207 return *text
? FALSE
: TRUE
;
210 /* Match the "pattern" against the "text" string. */
211 int wildmatch(const char *pattern
, const char *text
)
213 return dowild((const uchar
*)pattern
, (const uchar
*)text
) == TRUE
;
216 /* Match the "pattern" against the forced-to-lower-case "text" string. */
217 int iwildmatch(const char *pattern
, const char *text
)
220 force_lower_case
= 1;
221 ret
= dowild((const uchar
*)pattern
, (const uchar
*)text
) == TRUE
;
222 force_lower_case
= 0;