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 CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
25 && *(class) == *(litmatch) \
26 && strncmp((char*)class, litmatch, len) == 0)
28 #if defined STDC_HEADERS || !defined isascii
31 # define ISASCII(c) isascii(c)
35 # define ISBLANK(c) (ISASCII(c) && isblank(c))
37 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
41 # define ISGRAPH(c) (ISASCII(c) && isgraph(c))
43 # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
46 #define ISPRINT(c) (ISASCII(c) && isprint(c))
47 #define ISDIGIT(c) (ISASCII(c) && isdigit(c))
48 #define ISALNUM(c) (ISASCII(c) && isalnum(c))
49 #define ISALPHA(c) (ISASCII(c) && isalpha(c))
50 #define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
51 #define ISLOWER(c) (ISASCII(c) && islower(c))
52 #define ISPUNCT(c) (ISASCII(c) && ispunct(c))
53 #define ISSPACE(c) (ISASCII(c) && isspace(c))
54 #define ISUPPER(c) (ISASCII(c) && isupper(c))
55 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
57 /* Match pattern "p" against "text" */
58 static int dowild(const uchar *p, const uchar *text, int force_lower_case)
62 for ( ; (p_ch = *p) != '\0'; text++, p++) {
65 if ((t_ch = *text) == '\0' && p_ch != '*')
67 if (force_lower_case && ISUPPER(t_ch))
69 if (force_lower_case && ISUPPER(p_ch))
73 /* Literal match with following character. Note that the test
74 * in "default" handles the p[1] == '\0' failure case. */
82 /* Match anything but '/'. */
88 const uchar *prev_p = p - 2;
89 while (*++p == '*') {}
90 if ((prev_p == text || *prev_p == '/') ||
91 (*p == '\0' || *p == '/' ||
92 (p[0] == '\\' && p[1] == '/'))) {
95 return ABORT_MALFORMED;
99 /* Trailing "**" matches everything. Trailing "*" matches
100 * only if there are no more slash characters. */
102 if (strchr((char*)text, '/') != NULL)
110 if ((matched = dowild(p, text, force_lower_case)) != NOMATCH) {
111 if (!special || matched != ABORT_TO_STARSTAR)
113 } else if (!special && t_ch == '/')
114 return ABORT_TO_STARSTAR;
121 if (p_ch == NEGATE_CLASS2)
124 /* Assign literal TRUE/FALSE because of "matched" comparison. */
125 special = p_ch == NEGATE_CLASS? TRUE : FALSE;
127 /* Inverted character class. */
141 } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
148 if (t_ch <= p_ch && t_ch >= prev_ch)
150 p_ch = 0; /* This makes "prev_ch" get set to 0. */
151 } else if (p_ch == '[' && p[1] == ':') {
154 for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
158 if (i < 0 || p[-1] != ':') {
159 /* Didn't find ":]", so treat like a normal set. */
166 if (CC_EQ(s,i, "alnum")) {
169 } else if (CC_EQ(s,i, "alpha")) {
172 } else if (CC_EQ(s,i, "blank")) {
175 } else if (CC_EQ(s,i, "cntrl")) {
178 } else if (CC_EQ(s,i, "digit")) {
181 } else if (CC_EQ(s,i, "graph")) {
184 } else if (CC_EQ(s,i, "lower")) {
187 } else if (CC_EQ(s,i, "print")) {
190 } else if (CC_EQ(s,i, "punct")) {
193 } else if (CC_EQ(s,i, "space")) {
196 } else if (CC_EQ(s,i, "upper")) {
199 } else if (CC_EQ(s,i, "xdigit")) {
202 } else /* malformed [:class:] string */
204 p_ch = 0; /* This makes "prev_ch" get set to 0. */
205 } else if (t_ch == p_ch)
207 } while (prev_ch = p_ch, (p_ch = *++p) != ']');
208 if (matched == special || t_ch == '/')
214 return *text ? NOMATCH : MATCH;
217 /* Match the "pattern" against the "text" string. */
218 int wildmatch(const char *pattern, const char *text, int flags)
220 return dowild((const uchar*)pattern, (const uchar*)text,
221 flags & FNM_CASEFOLD ? 1 :0);