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 domatch(const unsigned char *p, const unsigned char *text)
63 unsigned char ch, prev;
65 #ifdef WILD_TEST_ITERATIONS
66 wildmatch_iteration_count++;
69 for ( ; (ch = *p) != '\0'; text++, p++) {
70 if (*text == '\0' && ch != '*')
74 /* Literal match with following character. Note that the test
75 * in "default" handles the p[1] == '\0' failure case. */
83 /* Match anything but '/'. */
89 while (*++p == '*') {}
95 /* Trailing "**" matches everything. Trailing "*" matches
96 * only if there are no more slash characters. */
97 return special? TRUE : strchr((char*)text, '/') == NULL;
99 for ( ; *text; text++) {
100 if ((matched = domatch(p, text)) != FALSE) {
101 if (!special || matched != ABORT_TO_STARSTAR)
104 else if (!special && *text == '/')
105 return ABORT_TO_STARSTAR;
111 if (ch == NEGATE_CLASS2)
114 /* Assign literal TRUE/FALSE because of "matched" comparison. */
115 special = ch == NEGATE_CLASS? TRUE : FALSE;
117 /* Inverted character class. */
132 else if (ch == '-' && prev && p[1] && p[1] != ']') {
139 if (*text <= ch && *text >= prev)
141 ch = 0; /* This makes "prev" get set to 0. */
143 else if (ch == '[' && p[1] == ':') {
144 const unsigned char *s = p += 2;
146 while ((ch = *p) && ch != ']') p++;
150 if (i < 0 || p[-1] != ':') {
151 /* Didn't find ":]", so treat like a normal set. */
158 if (CC_EQ(s,i, "alnum")) {
162 else if (CC_EQ(s,i, "alpha")) {
166 else if (CC_EQ(s,i, "blank")) {
170 else if (CC_EQ(s,i, "cntrl")) {
174 else if (CC_EQ(s,i, "digit")) {
178 else if (CC_EQ(s,i, "graph")) {
182 else if (CC_EQ(s,i, "lower")) {
186 else if (CC_EQ(s,i, "print")) {
190 else if (CC_EQ(s,i, "punct")) {
194 else if (CC_EQ(s,i, "space")) {
198 else if (CC_EQ(s,i, "upper")) {
202 else if (CC_EQ(s,i, "xdigit")) {
206 else /* malformed [:class:] string */
208 ch = 0; /* This makes "prev" get set to 0. */
210 else if (*text == ch)
212 } while (prev = ch, (ch = *++p) != ']');
213 if (matched == special || *text == '/')
219 return *text == '\0';
222 /* Find the pattern (p) in the text string (t). */
223 int wildmatch(const char *p, const char *t)
225 #ifdef WILD_TEST_ITERATIONS
226 wildmatch_iteration_count = 0;
228 return domatch((const unsigned char*)p, (const unsigned char*)t) == TRUE;