/*
 * Copyright (c) 1996-2007, OpenFWTK Development Group
 * All rights reserved. See LICENSE.
 */

/* 
 * (C) by Guido van Rossum, poblic domain license (?)
 */

#include <ctype.h>

int
namatch(char *pattern,char *string)
{
	register	char	c;
			char	*s,*p;

	s = string;
	p = pattern;

	while(1)
		switch(c = *p++) {
		case '\0':
			return(*s == '\0');
		case '*':
			c = *p;
			while(c == '*')
				c = *++p;
			if(c == '\0')
				return(1);
			/* general case, use recursion */
			while(*s != '\0') {
				if(namatch(p,s))
					return(1);
				++s;
			}
			return(0);
		case '?':
			if(*s++ == '\0')
				return(0);
			break;
		default:
			if(c != *s++)
				return(0);
			break;
		}
}

int
nacasematch(char *pattern,char *string)
{
	register	char	c;
			char	*s,*p;

	s = string;
	p = pattern;

	while(1)
		switch(c = *p++) {
		case '\0':
			return(*s == '\0');
		case '*':
			c = *p;
			while(c == '*')
				c = *++p;
			if(c == '\0')
				return(1);
			/* general case, use recursion */
			while(*s != '\0') {
				if(nacasematch(p,s))
					return(1);
				++s;
			}
			return(0);
		case '?':
			if(*s++ == '\0')
				return(0);
			break;
		default:
			if(tolower(c) != tolower(*s++))
				return(0);
			break;
		}
}

