#define TRUE 1 #define FALSE 0 #define ABORT -1 #define NEGATE_CLASS '^' int domatch(char *text, char *p) { register int last; register int matched; register int reverse; for ( ; *p; text++, p++) { if (*text == '\0' && *p != '*') return ABORT; switch (*p) { case '\\': /* Literal match with following character. */ p++; /* FALLTHROUGH */ default: if (*text != *p) return FALSE; continue; case '?': /* Match anything. */ continue; case '*': while (*++p == '*') /* Consecutive stars act just like one. */ continue; if (*p == '\0') /* Trailing star matches everything. */ return TRUE; while (*text) if ((matched = domatch(text++, p)) != FALSE) return matched; return ABORT; case '[': reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE; if (reverse) /* Inverted character class. */ p++; matched = FALSE; if (p[1] == ']' || p[1] == '-') if (*++p == *text) matched = TRUE; for (last = *p; *++p && *p != ']'; last = *p) if (*p == '-' && p[1] != ']' ? *text <= *++p && *text >= last : *text == *p) matched = TRUE; if (matched == reverse) return FALSE; continue; } } return *text == '\0'; } int match_wild(char *p,char *text) { if (p[0] == '*' && p[1] == '\0') return TRUE; return domatch(text, p) == TRUE; } #if defined(TESTINGIT) #include int main() { char bf[80]; char text[80]; for ( ; ; ) { printf("Enter WILD "); gets(bf); printf("Enter string "); gets(text); printf(" %s\n", match_wild(bf, text) ? "YES" : "NO"); } exit(0); } #endif