Initial import
This commit is contained in:
60
match.c
Normal file
60
match.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @PROJECT Kagera uHTTP Daemon
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE match.c
|
||||
* @PURPOSE Simple shell-style filename matcher
|
||||
* @DEVELOPERS Rafal Kupiec <belliash@asiotec.eu.org>
|
||||
* Jef Poskanzer <jef@acme.com>
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "match.h"
|
||||
|
||||
int match(const char* pattern, const char* string) {
|
||||
const char* or;
|
||||
|
||||
for(;;) {
|
||||
or = strchr( pattern, '|' );
|
||||
if(or == (char*) 0) {
|
||||
return match_one(pattern, strlen(pattern), string);
|
||||
}
|
||||
if(match_one( pattern, or - pattern, string)) {
|
||||
return 1;
|
||||
}
|
||||
pattern = or + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int match_one(const char* pattern, int patternlen, const char* string) {
|
||||
const char* p;
|
||||
for(p = pattern; p - pattern < patternlen; ++p, ++string) {
|
||||
if(*p == '?' && *string != '\0') {
|
||||
continue;
|
||||
}
|
||||
if(*p == '*') {
|
||||
int i, pl;
|
||||
++p;
|
||||
if(*p == '*') {
|
||||
++p;
|
||||
i = strlen(string);
|
||||
} else {
|
||||
i = strcspn(string, "/");
|
||||
}
|
||||
pl = patternlen - (p - pattern);
|
||||
for(; i >= 0; --i) {
|
||||
if(match_one(p, pl, &(string[i]))) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(*p != *string) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(*string == '\0') {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user