-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathglibc_fnmatch.c
71 lines (58 loc) · 1.71 KB
/
glibc_fnmatch.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define _GNU_SOURCE 1
#include <fnmatch.h>
#include <stdio.h>
/*
* BUG: When FNM_EXTMATCH is passed, you should be able to escape | as \|
* just like you can escape * as \*.
*
* (Remember that \| is written "\\|" in C syntax)
*/
void test(char* pattern, char* str, int flags) {
int ret = fnmatch(pattern, str, flags);
char* prefix = (flags == FNM_EXTMATCH) ? "[ext]" : " ";
switch (ret) {
case 0:
printf("%s %-10s matches %-10s\n", prefix, str, pattern);
break;
case FNM_NOMATCH:
printf("%s %-10s doesn't match %-10s\n", prefix, str, pattern);
break;
default:
printf("other error: %s\n", str);
break;
}
}
int main() {
char* pattern = 0;
// Demonstrate that \| and \* are valid patterns, whether or not FNM_EXTMATCH
// is set
pattern = "\\*";
test(pattern, "*", FNM_EXTMATCH);
test(pattern, "x", FNM_EXTMATCH);
printf("\n");
pattern = "\\|";
test(pattern, "|", 0);
test(pattern, "x", 0);
test(pattern, "|", FNM_EXTMATCH);
test(pattern, "x", FNM_EXTMATCH);
printf("\n");
// Wrap in @() works for \*
pattern = "@(\\*)";
test(pattern, "*", FNM_EXTMATCH);
test(pattern, "x", FNM_EXTMATCH);
printf("\n");
// Wrap in @() doesn't work for \|
pattern = "@(\\|)";
test(pattern, "|", FNM_EXTMATCH); // BUG: doesn't match
test(pattern, "x", FNM_EXTMATCH);
printf("\n");
// More realistic example
pattern = "@(spam|foo\\||bar\\*)";
// Demonstrate that \* escaping works
test(pattern, "bar*", FNM_EXTMATCH);
test(pattern, "bar\\", FNM_EXTMATCH);
test(pattern, "foo|",
FNM_EXTMATCH); // BUG: this should match, but it doesn't
test(pattern, "foo\\|", FNM_EXTMATCH); // shouldn't match and doesn't match
return 0;
}