2 * Copyright 1994 Christopher Seiwald. All rights reserved.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * glob.c - match a string against a simple pattern
10 * Understands the following patterns:
12 * * any number of characters
13 * ? any single character
14 * [a-z] any single character in the range a-z
15 * [^a-z] any single character not in the range a-z
20 * glob() - match a string against a simple pattern
24 * globchars() - build a bitlist to check for character group match
29 # define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) )
30 # define BITLISTSIZE 16 /* bytes used for [chars] in compiled expr */
32 static void globchars( char * s, char * e, char * b );
36 * glob() - match a string against a simple pattern.
39 int glob( char * c, char * s )
41 char bitlist[ BITLISTSIZE ];
56 /* Scan for matching ]. */
59 do if ( !*c++ ) return 1;
60 while ( ( here == c ) || ( *c != ']' ) );
63 /* Build character class bitlist. */
65 globchars( here, c, bitlist );
67 if ( !CHECK_BIT( bitlist, *(unsigned char *)s ) )
78 /* Try to match the rest of the pattern in a recursive */
79 /* call. If the match fails we'll back up chars, retrying. */
85 /* A fast path for the last token in a pattern. */
86 r = *c ? glob( c, s ) : *s ? -1 : 0;
97 /* Force literal match of next char. */
98 if ( !*c || ( *s++ != *c++ ) )
103 if ( *s++ != c[ -1 ] )
111 * globchars() - build a bitlist to check for character group match.
114 static void globchars( char * s, char * e, char * b )
118 memset( b, '\0', BITLISTSIZE );
130 if ( ( s + 2 < e ) && ( s[1] == '-' ) )
132 for ( c = s[0]; c <= s[2]; ++c )
133 b[ c/8 ] |= ( 1 << ( c % 8 ) );
139 b[ c/8 ] |= ( 1 << ( c % 8 ) );
146 for ( i = 0; i < BITLISTSIZE; ++i )
150 /* Do not include \0 in either $[chars] or $[^chars]. */