cwildcardfnmatch

fnmatches fails to match '?' to zero occurences


I'm writing a basic test for fnmatch before integrating in my code. Here is the test:

int main (int argc, char *argv[])
{
    for (int i = 2; i < argc; i++)
        if (!fnmatch(argv[1], argv[i], 0))
            printf("%s matches %s\n", argv[i], argv[1]);
        else
            printf("%s doesn't matches %s\n", argv[i], argv[1]);
    return EXIT_SUCCESS;
}

I compiled it to test and run the following command:

$ ./test a*b? a ab aa acb abc aabc acbc aaaaaaaaaaaaaaab aaaaaaaaaaaaaaaba abb aabb

Expexted output:

a doesn't matches a*b?
ab matches a*b?
aa doesn't matches a*b?
acb matches a*b?
abc matches a*b?
aabc matches a*b?
acbc matches a*b?
aaaaaaaaaaaaaaaba matches a*b?
aaaaaaaaaaaaaaab matches a*b?
abb matches a*b?
aabb matches a*b? 

Real output:

a doesn't matches a*b?
ab doesn't matches a*b?
aa doesn't matches a*b?
acb doesn't matches a*b?
abc matches a*b?
aabc matches a*b?
acbc matches a*b?
aaaaaaaaaaaaaaab doesn't matches a*b?
aaaaaaaaaaaaaaaba matches a*b?
abb matches a*b?
aabb matches a*b?

The problem being that the '?' metacharacter fails to match 0 characters (requiring exactly one).

Does anyone know why is it behaving this way and how to fix it?

Thank you in advance.


Solution

  • This is expected behavior. Posix glob matches ? to a single character, not an empty group. See glob matching.

    ? can be used to match empty group in regular expressions, though - where it does match zero or one occurrence.