-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwildcardcmp.c
More file actions
44 lines (38 loc) · 846 Bytes
/
wildcardcmp.c
File metadata and controls
44 lines (38 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdlib.h>
#include "wildcardcmp.h"
int
wildcardcmp(const char *pattern, const char *string) {
const char *w = NULL; // last `*`
const char *s = NULL; // last checked char
// malformed
if (!pattern || !string) return 0;
// loop 1 char at a time
while (1) {
if (!*string) {
if (!*pattern) return 1;
if ('*' == *pattern) return 1;
if (!*s) return 0;
string = s++;
pattern = w;
continue;
} else {
if (*pattern != *string) {
if ('*' == *pattern) {
w = ++pattern;
s = string;
// "*" -> "foobar"
if (*pattern) continue;
return 1;
} else if (w) {
string++;
// "*ooba*" -> "foobar"
continue;
}
return 0;
}
}
string++;
pattern++;
}
return 1;
}