filename pattern matching
Here's a function that matches a filter (like *.c) to a filename (like hippo.c).
Code:
int match(char *f, char *s) {
char *t;
while(1) {
if(*f == '?' || *s == '?') ;
else if(*f == '*') {
t = s;
while(1) {
if(match(f+1, t)) return 1;
if(!*t++) break;
}
return 0;
}
else if(*s == '*') {
t = f;
while(1) {
if(match(s+1, t)) return 1;
if(!*t++) break;
}
return 0;
}
else if(*f != *s) return 0;
if(!*s || !*f) break;
s++, f++;
}
return *f || *s ? 0 : 1;
}
It returns 1 if there's a match, 0 otherwise.
There's nothing wrong with it (as far as I can see), I was just wondering how it could be improved. It's a little too slow for me.
Before anyone points it out, the * matching could be removed from one of the arguments.