61 line
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			61 line
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/**
 | 
						|
 * @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;
 | 
						|
}
 |