aboutsummaryrefslogtreecommitdiff
path: root/wildcard_cmp.c
blob: 8f7c7c1188959f5e5df06049a5d2faba80289475 (plain) (blame)
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 "wildcard_cmp.h"
#include <string.h>

static int wildcard_cmp(const char *s, const char *p)
{
    while (*p && *s)
    {
        const char *const wc = strchr(p, '*');

        if (!wc)
            return strcmp(s, p);

        const size_t n = wc - p;

        if (n)
        {
            const int r = strncmp(s, p, n);

            if (r)
                return r;

            p += n;
            s += n;
        }
        else if (*(wc + 1) == *s)
        {
            p = wc + 1;
            s += n;
        }
        else if (*(wc + 1) == '*')
            p++;
        else
        {
            s++;
            p += n;
        }
    }

    while (*p)
        if (*p++ != '*')
            return -1;

    return 0;
}