| 37 | */ |
| 38 | |
| 39 | int glob( const char * c, const char * s ) |
| 40 | { |
| 41 | char bitlist[ BITLISTSIZE ]; |
| 42 | const char * here; |
| 43 | |
| 44 | for ( ; ; ) |
| 45 | switch ( *c++ ) |
| 46 | { |
| 47 | case '\0': |
| 48 | return *s ? -1 : 0; |
| 49 | |
| 50 | case '?': |
| 51 | if ( !*s++ ) |
| 52 | return 1; |
| 53 | break; |
| 54 | |
| 55 | case '[': |
| 56 | /* Scan for matching ]. */ |
| 57 | |
| 58 | here = c; |
| 59 | do if ( !*c++ ) return 1; |
| 60 | while ( ( here == c ) || ( *c != ']' ) ); |
| 61 | ++c; |
| 62 | |
| 63 | /* Build character class bitlist. */ |
| 64 | |
| 65 | globchars( here, c, bitlist ); |
| 66 | |
| 67 | if ( !CHECK_BIT( bitlist, *(const unsigned char *)s ) ) |
| 68 | return 1; |
| 69 | ++s; |
| 70 | break; |
| 71 | |
| 72 | case '*': |
| 73 | here = s; |
| 74 | |
| 75 | while ( *s ) |
| 76 | ++s; |
| 77 | |
| 78 | /* Try to match the rest of the pattern in a recursive */ |
| 79 | /* call. If the match fails we'll back up chars, retrying. */ |
| 80 | |
| 81 | while ( s != here ) |
| 82 | { |
| 83 | int r; |
| 84 | |
| 85 | /* A fast path for the last token in a pattern. */ |
| 86 | r = *c ? glob( c, s ) : *s ? -1 : 0; |
| 87 | |
| 88 | if ( !r ) |
| 89 | return 0; |
| 90 | if ( r < 0 ) |
| 91 | return 1; |
| 92 | --s; |
| 93 | } |
| 94 | break; |
| 95 | |
| 96 | case '\\': |
no test coverage detected