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 /* Match pattern "p" against string "text". */
61 static int dowild(const uchar *p, const uchar *text)
66 #ifdef WILD_TEST_ITERATIONS
67 wildmatch_iteration_count++;
70 for ( ; (ch = *p) != '\0'; text++, p++) {
71 if (*text == '\0' && 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. */
97 return special? TRUE : strchr((char*)text, '/') == NULL;
99 for ( ; *text; text++) {
100 if ((matched = dowild(p, text)) != FALSE) {
101 if (!special || matched != ABORT_TO_STARSTAR)
103 } else if (!special && *text == '/')
104 return ABORT_TO_STARSTAR;
110 if (ch == NEGATE_CLASS2)
113 /* Assign literal TRUE/FALSE because of "matched" comparison. */
114 special = ch == NEGATE_CLASS? TRUE : FALSE;
116 /* Inverted character class. */
130 } else if (ch == '-' && prev && p[1] && p[1] != ']') {
137 if (*text <= ch && *text >= prev)
139 ch = 0; /* This makes "prev" get set to 0. */
140 } else if (ch == '[' && p[1] == ':') {
143 for (s = p += 2; (ch = *p) && ch != ']'; p++) {}
147 if (i < 0 || p[-1] != ':') {
148 /* Didn't find ":]", so treat like a normal set. */
155 if (CC_EQ(s,i, "alnum")) {
158 } else if (CC_EQ(s,i, "alpha")) {
161 } else if (CC_EQ(s,i, "blank")) {
164 } else if (CC_EQ(s,i, "cntrl")) {
167 } else if (CC_EQ(s,i, "digit")) {
170 } else if (CC_EQ(s,i, "graph")) {
173 } else if (CC_EQ(s,i, "lower")) {
176 } else if (CC_EQ(s,i, "print")) {
179 } else if (CC_EQ(s,i, "punct")) {
182 } else if (CC_EQ(s,i, "space")) {
185 } else if (CC_EQ(s,i, "upper")) {
188 } else if (CC_EQ(s,i, "xdigit")) {
191 } else /* malformed [:class:] string */
193 ch = 0; /* This makes "prev" get set to 0. */
194 } else if (*text == ch)
196 } while (prev = ch, (ch = *++p) != ']');
197 if (matched == special || *text == '/')
203 return *text == '\0';
206 /* Match the "pattern" against the "text" string. */
207 int wildmatch(const char *pattern, const char *text)
209 #ifdef WILD_TEST_ITERATIONS
210 wildmatch_iteration_count = 0;
212 return dowild((const uchar*)pattern, (const uchar*)text) == TRUE;