00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include <sys/types.h>
00040
00041
00042 #include "wildmat.h"
00043
00044
00045 #define TRUE 1
00046 #define FALSE 0
00047 #define ABORT -1
00048
00049
00050
00051 #define NEGATE_CLASS '^'
00052
00053 #define OPTIMIZE_JUST_STAR
00054
00055 #undef MATCH_TAR_PATTERN
00056
00057
00058
00059
00060
00061 static int DoMatch(text, p)
00062 register char *text;
00063 register char *p;
00064 {
00065 register int last;
00066 register int matched;
00067 register int reverse;
00068
00069 for (; *p; text++, p++) {
00070 if (*text == '\0' && *p != '*')
00071 return ABORT;
00072 switch (*p) {
00073 case '\\':
00074
00075 p++;
00076
00077 default:
00078 if (*text != *p)
00079 return FALSE;
00080 continue;
00081 case '?':
00082
00083 continue;
00084 case '*':
00085 while (*++p == '*')
00086
00087 continue;
00088 if (*p == '\0')
00089
00090 return TRUE;
00091 while (*text)
00092 if ((matched =
00093 DoMatch(text++, p)) != FALSE)
00094 return matched;
00095 return ABORT;
00096 case '[':
00097 reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE;
00098 if (reverse)
00099
00100 p++;
00101 matched = FALSE;
00102 if (p[1] == ']' || p[1] == '-')
00103 if (*++p == *text)
00104 matched = TRUE;
00105 for (last = *p; *++p && *p != ']'; last = *p)
00106
00107 if (*p == '-' && p[1] != ']'
00108 ? *text <= *++p
00109 && *text >= last : *text == *p)
00110 matched = TRUE;
00111 if (matched == reverse)
00112 return FALSE;
00113 continue;
00114 }
00115 }
00116
00117 #ifdef MATCH_TAR_PATTERN
00118 if (*text == '/')
00119 return TRUE;
00120 #endif
00121 return *text == '\0';
00122 }
00123
00124
00125
00126
00127
00128 int wildmat(const char *text, const char *p)
00129 {
00130 #ifdef OPTIMIZE_JUST_STAR
00131 if (p[0] == '*' && p[1] == '\0')
00132 return TRUE;
00133 #endif
00134 return DoMatch(text, p) == TRUE;
00135 }
00136
00137
00138
00139 #if defined(TEST)
00140
00141
00142 extern char *gets();
00143
00144
00145 int main()
00146 {
00147 char p[80];
00148 char text[80];
00149
00150 printf("Wildmat tester. Enter pattern, then strings to test.\n");
00151 printf
00152 ("A blank line gets prompts for a new pattern; a blank pattern\n");
00153 printf("exits the program.\n");
00154
00155 for (;;) {
00156 printf("\nEnter pattern: ");
00157 (void) fflush(stdout);
00158 if (gets(p) == NULL || p[0] == '\0')
00159 break;
00160 for (;;) {
00161 printf("Enter text: ");
00162 (void) fflush(stdout);
00163 if (gets(text) == NULL)
00164 exit(0);
00165 if (text[0] == '\0')
00166
00167 break;
00168 printf(" %s\n",
00169 wildmat(text, p) ? "YES" : "NO");
00170 }
00171 }
00172
00173 exit(0);
00174
00175 }
00176 #endif