wildcard_cmp.c: Fix a couple of bugs

wildcard_cmp would otherwise fail with the following use cases:

s = "mymi", p = "*mi*"
s = "mymi", p = "*mi"
This commit is contained in:
Xavier Del Campo Romero 2023-06-06 03:35:20 +02:00
parent 0d7ead4638
commit 6adf1c44ad
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
1 changed files with 22 additions and 3 deletions

View File

@ -6,6 +6,7 @@
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) =
@ -16,7 +17,18 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp)
const char *const wc = strchr(p, '*');
if (!wc)
return cmp(s, p);
{
const int r = cmp(s, p);
if (r && last)
{
p = last;
s += strlen(p);
continue;
}
else
return r;
}
const size_t n = wc - p;
@ -25,9 +37,15 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp)
const int r = ncmp(s, p, n);
if (r)
return r;
{
if (last)
p = last;
else
return r;
}
else
p += n;
p += n;
s += n;
}
else
@ -36,6 +54,7 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp)
if (!cmp(wca, sa))
{
last = p;
p = wc + 1;
}
else if (next == '*')