#include "includes.h"
#include "externs.h"

int wildcardfit(char *wildcard, char *test)
{
	int fit = 1;
  
	for (; (0 != *wildcard) && (1 == fit) && (0 != *test); wildcard++)
	{
		switch (*wildcard)
		{
			case '[':
				wildcard++;
				fit = set (&wildcard, &test);
				break;
			case '?':
				test++;
				break;
			case '*':
				fit = asterisk (&wildcard, &test);
				wildcard--;
				break;
			default:
				fit = (int) (*wildcard == *test);
				test++;
		}
	}
	while ((*wildcard == '*') && (1 == fit)) 
		wildcard++;

	return ((int) ((1 == fit) && (0 == *test) && (0 == *wildcard)));
}

int set(char **wildcard, char **test)
{
	int fit = 0, negation = 0, at_beginning = 1;

	if ('!' == **wildcard) {
		negation = 1;
		(*wildcard)++;
	}
	while ((']' != **wildcard) || (1 == at_beginning)) {
		if (0 == fit) {
			if (('-' == **wildcard) && ((*(*wildcard - 1)) < (*(*wildcard + 1)))
				&& (']' != *(*wildcard + 1)) && (0 == at_beginning)) {
				if (((**test) >= (*(*wildcard - 1))) && ((**test) <= (*(*wildcard + 1)))) {
					fit = 1;
					(*wildcard)++;
				}
			}
			else if ((**wildcard) == (**test))
              fit = 1;
		}
		(*wildcard)++;
		at_beginning = 0;
	}
	if (1 == negation)
		fit = 1 - fit;
	if (1 == fit) 
		(*test)++;

	return (fit);
}

int asterisk(char **wildcard, char **test)
{
	int fit = 1;

	(*wildcard)++; 
	while ((0 != (**test)) && (('?' == **wildcard) || ('*' == **wildcard))) {
		if ('?' == **wildcard) 
			(*test)++;
		(*wildcard)++;
	}
	while ('*' == (**wildcard))
		(*wildcard)++;

	if ((0 == (**test)) && (0 != (**wildcard)))
		return (fit = 0);
	if ((0 == (**test)) && (0 == (**wildcard)))
		return (fit = 1); 
	else {
		if (0 == wildcardfit(*wildcard, (*test))) {
			do {
				(*test)++;
				while (((**wildcard) != (**test)) && ('['  != (**wildcard)) && (0 != (**test)))
					(*test)++;
			} while (((0 != **test))?(0 == wildcardfit ((char *)*wildcard, (*test))):(0 != (fit = 0)));
		}
		if ((0 == **test) && (0 == **wildcard))
			fit = 1;
		return fit;
	}
}