-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwildcard_cmp.c
73 lines (62 loc) · 1.5 KB
/
wildcard_cmp.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
72
73
#include "libweb/wildcard_cmp.h"
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
int wildcard_cmp(const char *s, const char *p, const bool casecmp)
{
const char *last = NULL;
int (*const cmp)(const char *, const char *) =
casecmp ? strcmp : strcasecmp;
int (*const ncmp)(const char *, const char *, size_t) =
casecmp ? strncmp : strncasecmp;
while (*p && *s)
{
const char *const wc = strchr(p, '*');
if (!wc)
{
const int r = cmp(s, p);
if (r && last)
{
p = last;
s += strlen(p);
continue;
}
else
return r;
}
const size_t auxn = wc - p, rem = strlen(s),
n = auxn > rem ? rem : auxn;
if (n)
{
const int r = ncmp(s, p, n);
if (r)
{
if (last)
p = last;
else
return r;
}
else
p += n;
s += n;
}
else
{
const char next = *(wc + 1), wca[2] = {next}, sa[sizeof wca] = {*s};
if (!cmp(wca, sa))
{
last = p;
p = wc + 1;
}
else if (next == '*')
p++;
else
s++;
}
}
while (*p)
if (*p++ != '*')
return -1;
return 0;
}