wildcard * is used to search for a particular character(s) for zero or more times.
static int word_compare(char **filename, char *wild, int *idx)
{
while (**filename && wild[*idx] && wild[*idx] != '*')
{
if (wild[*idx] == **filename)
{
++(*idx);
++(*filename);
}
else
return (0);
}
if (wild[*idx] != '*' && **filename != wild[*idx])
return (0);
if (!wild[*idx] && **filename)
return (0);
return (1);
}
int cmp_wildcard(char *filename, char *wild)
{
int idx;
int i;
idx = 0;
while (wild[idx])
{
if (wild[idx] == '*')
{
i = ft_strichr(filename, wild[idx + 1]); // find the character right after *
while (i >= 0)
{
if (cmp_wildcard(&filename[i], &wild[idx + 1]))
return (1);
++filename;
i = ft_strichr(&filename[i], wild[idx + 1]);
}
++idx;
}
else
if (!word_compare(&filename, wild, &idx))
return (0);
}
return (1);
}