/* Autoconf patching by David Hedbor, neotron@lysator.liu.se */
/*
 * match -- returns 1 if `string' satisfised `regex' and 0 otherwise
 * stolen from Spencer Sun: only recognizes * and \ as special characters
 */
int match(regex, string)
	char *regex;
	char *string;
{
	char c, *rp = regex, *sp = string, *p;
	char spn[256];
	int found;

	if (!rp || !sp)
		return 0;

	while (*rp != '\0') {
		switch (c = *rp++) {
		case '*':
			if (*sp == 0 || *rp == 0)	// match empty string at end of `string'
				return (*rp == 0);	// but only if we're done with the pattern

			p = sp + strlen(sp);

			do {
				if (match(rp, sp))
					return 1;
			} while (++sp < p);

			/*
			 * Reached end of string (i.e. `*' matches empty string) and
			 * we _still_ can't match here.  Give up.
			 */
			return 0;
		case '[':	// given [abc...] next char must match any of abc...
			p = spn;
			while (*rp != ']') {
				if (!*rp)			// Missing ']'
					return 0;
				*p++ = *rp++;
			}
			rp++;
			*p = 0;
			if (!strchr(spn, *sp++))
				return 0;
			break;
		case '\\':
			c = *rp++;	/* Fall through to match literal character */
		default:	// normal character
			if (c != *sp++)
				return 0;
			break;
		}
	}

	/*
	 * OK, we successfully matched the pattern if we got here.  Now return
	 * a match if we also reached end of string, otherwise failure
	 */
	return (*sp == 0);
}