Line | Count | Source |
1 | ||
2 | #include <stdlib.h> | |
3 | #include "wildcardcmp.h" | |
4 | ||
5 | int | |
6 | 0 | wildcardcmp(const char *pattern, const char *string) { |
7 | 0 | const char *w = NULL; // last `*` |
8 | 0 | const char *s = NULL; // last checked char |
9 | ||
10 | // malformed | |
11 | 0 | if (!pattern || !string) return 0; |
12 | ||
13 | // loop 1 char at a time | |
14 | 0 | while (1) { |
15 | 0 | if (!*string) { |
16 | 0 | if (!*pattern) return 1; |
17 | 0 | if ('*' == *pattern) { |
18 | 0 | pattern++; |
19 | 0 | continue; |
20 | 0 | } |
21 | 0 | if (!*s) return 0; |
22 | 0 | string = s++; |
23 | 0 | pattern = w; |
24 | 0 | continue; |
25 | 0 | } else { |
26 | 0 | if (*pattern != *string) { |
27 | 0 | if ('*' == *pattern) { |
28 | 0 | w = ++pattern; |
29 | 0 | s = string; |
30 | // "*" -> "foobar" | |
31 | 0 | if (*pattern) continue; |
32 | 0 | return 1; |
33 | 0 | } else if (w) { |
34 | 0 | string++; |
35 | // "*ooba*" -> "foobar" | |
36 | 0 | continue; |
37 | 0 | } |
38 | 0 | return 0; |
39 | 0 | } |
40 | 0 | } |
41 | ||
42 | 0 | string++; |
43 | 0 | pattern++; |
44 | 0 | } |
45 | ||
46 | 0 | return 1; |
47 | 0 | } |