FUNCTION
See if a string matches a wildcard expression. The wildcard expression can consists of any literal character and the two wildcards characters '*' and '?'. '*' matches the longest string of zero or more characters that fit. '?' matches any single characters. Examples: "aapaapaapaap" matches "*aap" "linux" matches "?inux" "linux" matches "lin*ux" "linux" matches "linux*" This is a recursive function.
SYNOPSIS
int RB_Match( char* target, char* wildcard_expression )
INPUTS
o target -- the string to be matched agains the wildcard_expression. o wildcard_expression -- the wildcard expression RETURN TRUE -- the target matches the wildcard expression FALSE -- it does not match.
SOURCE
int RB_Match( char* target, char* wildcard_expression ) { if ( ( *wildcard_expression == '\0' ) && ( *target == '\0' ) ) { /* a match, since both strings are "" */ return TRUE; } else if ( *wildcard_expression == '\0' ) { /* we reached the end of the wildcard_expression, * but not the end of the target, this is not * a match. */ return FALSE; } else if ( *target == '\0' ) { /* we reached the end of the target but not the end of the * wildcard_expression. Only if the whole wildcard_expression * consists of * we have a match. */ int i; for ( i = 0; i < strlen( wildcard_expression ); ++i ) { if ( wildcard_expression[ i ] != '*' ) { return FALSE; } } return TRUE; } else { /* There are wildcard_expression characters left * and target characters left. */ char wildcard = wildcard_expression[ 0 ]; if ( wildcard == '?' ) { /* Match a single character and see if the * rest of the target matches. */ return RB_Match( target + 1, wildcard_expression + 1 ); } else if ( wildcard == '*' ) { int match = FALSE; int l = strlen( target ); int i; /* First try to match all of the target string, and * then work back to the begin of the target string. * ( including the "" string. ) */ for ( i = l; i >= 0; --i ) { if ( RB_Match( target + i, wildcard_expression + 1 ) ) { match = TRUE; break; } } return match; } else { int l_w = strlen( wildcard_expression ); int l_t = strlen( target ); /* The minimum of the length of the wildcard_expression * and target expression */ int l = ( l_w <= l_t ) ? l_w : l_t ; int i; for ( i = 0; i < l; ++i ) { if( ( wildcard_expression[ i ] != '*' ) && ( wildcard_expression[ i ] != '?' ) ) { if ( wildcard_expression[ i ] != target[ i ] ) { return FALSE; } } else { return RB_Match( target + i, wildcard_expression + i ); } } /* The first l characters of the target and * wildcard_expression matched, now see if the rest * matches too. */ return RB_Match( target + l, wildcard_expression + l ); } } }