10 the original, recursive function. Needs replacing, but with exactly
13 static int fnmatch_orig(const char *p, const char *n)
26 if (! n[1] && fnmatch_orig(p, n+1) == 0) return 0;
27 if (fnmatch_orig(p, n) == 0) return 0;
30 if (! *n) return fnmatch_orig(p, n);
36 if (fnmatch_orig(p, n) == 0) return 0;
42 if (fnmatch_orig(p, n) == 0) return 0;
43 if (*n == '.' && !strchr(n+1,'.')) {
51 if (*n == 0 && fnmatch_orig(p, n) == 0) return 0;
52 if (*n != '.') return -1;
57 if (c != *n && toupper(c) != toupper(*n)) return -1;
68 static int null_match(const char *p)
79 static int min_p_chars(const char *p)
98 the original, recursive function. Needs replacing, but with exactly
101 static int fnmatch_test2(const char *p, const char *n)
105 if (min_p_chars(p) > strlen(n)) return -1;
107 // printf("p=%s n=%s\n", p, n);
120 if (! n[1] && null_match(p) == 0) {
125 if (! *n) return null_match(p);
131 if (fnmatch_test2(p, n) == 0) {
135 if (! *n) return null_match(p);
140 if (fnmatch_test2(p, n) == 0) {
153 null_match(p) == 0) {
156 if (*n != '.') return -1;
177 static char *compress_pattern(const char *pattern)
181 p = new = strdup(pattern);
182 while (p[0] && p[1]) {
185 if (p[0] == '*' && (p[1] == '*' || p[1] == '<'))
186 memmove(p+1, p+2, strlen(p+1));
188 else if (p[0] == '<' && p[1] == '<')
197 the new, hopefully better function. Fiddle this until it works and is fast
199 static int fnmatch_test(const char *p, const char *n)
201 char *new = compress_pattern(p);
204 ret = fnmatch_test2(new, n);
209 static void randstring(char *s, int len, const char *chars)
212 *s++ = chars[random() % strlen(chars)];
217 static const char *p_used;
218 static const char *n_used;
220 static void sig_alrm(int sig)
222 printf("Too slow!!\np='%s'\ns='%s'\n", p_used, n_used);
223 printf("compresed: '%s'\n", compress_pattern(p_used));
232 signal(SIGALRM, sig_alrm);
235 p_used = "*c*c*cc*c*c*c*c*cc*cc*cc*c*c*c*cc*c*cc*c*c*cc*cc*ccc*c*c*cc*c*c";
236 n_used = "c.cccccccccccccccccccccccccccc";
237 fnmatch_test(p_used, n_used);
240 for (i=0;i<100000;i++) {
241 int len1 = random() % 20;
242 int len2 = random() % 20;
243 char *p = malloc(len1+1);
244 char *n = malloc(len2+1);
247 randstring(p, len1, "*?a");
248 randstring(n, len2, "a.");
254 ret1 = fnmatch_orig(p, n);
256 ret2 = fnmatch_test(p, n);
260 printf("mismatch: ret1=%d ret2=%d pattern='%s' string='%s'\n",
262 printf("Pattern actually used: '%s' => '%s'\n",
263 p, compress_pattern(p));