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 '!'
21 static int domatch(const char *p, const char *text)
26 for ( ; (ch = *p) != '\0'; text++, p++) {
27 if (*text == '\0' && ch != '*')
31 /* Literal match with following character. Note that the test
32 * in "default" handles the p[1] == '\0' failure case. */
40 /* Match anything but '/'. */
46 while (*++p == '*') {}
52 /* Trailing "**" matches everything. Trailing "*" matches
53 * only if there are no more slash characters. */
54 return special? TRUE : strchr(text, '/') == 0;
56 for ( ; *text; text++) {
57 if ((matched = domatch(p, text)) != FALSE)
59 if (!special && *text == '/')
64 special = *++p == NEGATE_CLASS ? TRUE : FALSE;
66 /* Inverted character class. */
72 if (ch == ']' || ch == '-') {
78 for ( ; ch != ']'; prev = ch, ch = *++p) {
81 if (ch == '-' && prev && p[1] && p[1] != ']') {
82 if (*text <= *++p && *text >= prev)
84 ch = 0; /* This makes "prev" get set to 0. */
89 if (matched == special)
98 int wildmatch(const char *p, const char *text)
100 return domatch(p, text) == TRUE;