1 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 /* Enable GNU extensions in fnmatch.h. */
26 # define _GNU_SOURCE 1
34 #if HAVE_STRING_H || defined _LIBC
40 #if defined STDC_HEADERS || defined _LIBC
44 /* For platform which support the ISO C amendement 1 functionality we
45 support user defined character classes. */
46 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
47 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
52 /* We need some of the locale data (the collation sequence information)
53 but there is no interface to get this information in general. Therefore
54 we support a correct implementation only in glibc. */
56 # include "../locale/localeinfo.h"
57 # include "../locale/elem-hash.h"
58 # include "../locale/coll-lookup.h"
59 # include <shlib-compat.h>
61 # define CONCAT(a,b) __CONCAT(a,b)
62 # define mbsrtowcs __mbsrtowcs
63 # define fnmatch __fnmatch
64 extern int fnmatch (const char *pattern
, const char *string
, int flags
);
67 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
68 #define NO_LEADING_PERIOD(flags) \
69 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
71 /* Comment out all this code if we are using the GNU C Library, and are not
72 actually compiling the library itself. This code is part of the GNU C
73 Library, but also included in many other GNU distributions. Compiling
74 and linking in this code is a waste when using the GNU C library
75 (especially if it is a shared library). Rather than having every GNU
76 program understand `configure --with-gnu-libc' and omit the object files,
77 it is simpler to just do this in the source for each such file. */
79 #if defined _LIBC || !defined __GNU_LIBRARY__
82 # if defined STDC_HEADERS || !defined isascii
85 # define ISASCII(c) isascii(c)
89 # define ISBLANK(c) (ISASCII (c) && isblank (c))
91 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
94 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
96 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
99 # define ISPRINT(c) (ISASCII (c) && isprint (c))
100 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
101 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
102 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
103 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
104 # define ISLOWER(c) (ISASCII (c) && islower (c))
105 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
106 # define ISSPACE(c) (ISASCII (c) && isspace (c))
107 # define ISUPPER(c) (ISASCII (c) && isupper (c))
108 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
110 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
112 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
113 /* The GNU C library provides support for user-defined character classes
114 and the functions from ISO C amendement 1. */
115 # ifdef CHARCLASS_NAME_MAX
116 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
118 /* This shouldn't happen but some implementation might still have this
119 problem. Use a reasonable default value. */
120 # define CHAR_CLASS_MAX_LENGTH 256
124 # define IS_CHAR_CLASS(string) __wctype (string)
126 # define IS_CHAR_CLASS(string) wctype (string)
130 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
132 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
135 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
136 /* In this case we are implementing the multibyte character handling. */
137 # define HANDLE_MULTIBYTE 1
141 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
143 # define IS_CHAR_CLASS(string) \
144 (STREQ (string, "alpha") || STREQ (string, "upper") \
145 || STREQ (string, "lower") || STREQ (string, "digit") \
146 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
147 || STREQ (string, "space") || STREQ (string, "print") \
148 || STREQ (string, "punct") || STREQ (string, "graph") \
149 || STREQ (string, "cntrl") || STREQ (string, "blank"))
152 /* Avoid depending on library functions or files
153 whose names are inconsistent. */
155 # if !defined _LIBC && !defined getenv
156 extern char *getenv ();
163 /* Global variable. */
164 static int posixly_correct
;
166 /* This function doesn't exist on most systems. */
168 # if !defined HAVE___STRCHRNUL && !defined _LIBC
174 char *result
= strchr (s
, c
);
176 result
= strchr (s
, '\0');
181 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
187 wchar_t *result
= wcschr (s
, c
);
189 result
= wcschr (s
, '\0');
194 # ifndef internal_function
195 /* Inside GNU libc we mark some function in a special way. In other
196 environments simply ignore the marking. */
197 # define internal_function
200 /* Note that this evaluates C many times. */
202 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
204 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
207 # define UCHAR unsigned char
209 # define FCT internal_fnmatch
210 # define EXT ext_match
211 # define END end_pattern
212 # define STRUCT fnmatch_struct
215 # define BTOWC(C) __btowc (C)
217 # define BTOWC(C) btowc (C)
219 # define STRLEN(S) strlen (S)
220 # define STRCAT(D, S) strcat (D, S)
221 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
222 # define MEMCHR(S, C, N) memchr (S, C, N)
223 # define STRCOLL(S1, S2) strcoll (S1, S2)
224 # include "fnmatch_loop.c"
227 # if HANDLE_MULTIBYTE
228 /* Note that this evaluates C many times. */
230 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
232 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
234 # define CHAR wchar_t
235 # define UCHAR wint_t
237 # define FCT internal_fnwmatch
238 # define EXT ext_wmatch
239 # define END end_wpattern
240 # define STRUCT fnwmatch_struct
242 # define BTOWC(C) (C)
243 # define STRLEN(S) __wcslen (S)
244 # define STRCAT(D, S) __wcscat (D, S)
245 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
246 # define MEMCHR(S, C, N) wmemchr (S, C, N)
247 # define STRCOLL(S1, S2) wcscoll (S1, S2)
248 # define WIDE_CHAR_VERSION 1
250 # undef IS_CHAR_CLASS
251 /* We have to convert the wide character string in a multibyte string. But
252 we know that the character class names consist of alphanumeric characters
253 from the portable character set, and since the wide character encoding
254 for a member of the portable character set is the same code point as
255 its single-byte encoding, we can use a simplified method to convert the
256 string to a multibyte character string. */
258 is_char_class (const wchar_t *wcs
)
260 char s
[CHAR_CLASS_MAX_LENGTH
+ 1];
265 /* Test for a printable character from the portable character set. */
267 if (*wcs
< 0x20 || *wcs
> 0x7e
268 || *wcs
== 0x24 || *wcs
== 0x40 || *wcs
== 0x60)
273 case L
' ': case L
'!': case L
'"': case L
'#': case L
'%':
274 case L
'&': case L
'\'': case L
'(': case L
')': case L
'*':
275 case L
'+': case L
',': case L
'-': case L
'.': case L
'/':
276 case L
'0': case L
'1': case L
'2': case L
'3': case L
'4':
277 case L
'5': case L
'6': case L
'7': case L
'8': case L
'9':
278 case L
':': case L
';': case L
'<': case L
'=': case L
'>':
280 case L
'A': case L
'B': case L
'C': case L
'D': case L
'E':
281 case L
'F': case L
'G': case L
'H': case L
'I': case L
'J':
282 case L
'K': case L
'L': case L
'M': case L
'N': case L
'O':
283 case L
'P': case L
'Q': case L
'R': case L
'S': case L
'T':
284 case L
'U': case L
'V': case L
'W': case L
'X': case L
'Y':
286 case L
'[': case L
'\\': case L
']': case L
'^': case L
'_':
287 case L
'a': case L
'b': case L
'c': case L
'd': case L
'e':
288 case L
'f': case L
'g': case L
'h': case L
'i': case L
'j':
289 case L
'k': case L
'l': case L
'm': case L
'n': case L
'o':
290 case L
'p': case L
'q': case L
'r': case L
's': case L
't':
291 case L
'u': case L
'v': case L
'w': case L
'x': case L
'y':
292 case L
'z': case L
'{': case L
'|': case L
'}': case L
'~':
299 /* Avoid overrunning the buffer. */
300 if (cp
== s
+ CHAR_CLASS_MAX_LENGTH
)
303 *cp
++ = (char) *wcs
++;
305 while (*wcs
!= L
'\0');
315 # define IS_CHAR_CLASS(string) is_char_class (string)
317 # include "fnmatch_loop.c"
322 fnmatch (pattern
, string
, flags
)
327 # if HANDLE_MULTIBYTE
328 if (__builtin_expect (MB_CUR_MAX
, 1) != 1)
336 /* Convert the strings into wide characters. */
337 memset (&ps
, '\0', sizeof (ps
));
340 n
= strnlen (pattern
, 1024);
342 n
= strlen (pattern
);
344 if (__builtin_expect (n
< 1024, 1))
346 wpattern
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
347 n
= mbsrtowcs (wpattern
, &p
, n
+ 1, &ps
);
348 if (__builtin_expect (n
== (size_t) -1, 0))
350 XXX Do we have to set `errno' to something which mbsrtows hasn't
355 memset (&ps
, '\0', sizeof (ps
));
356 goto prepare_wpattern
;
362 n
= mbsrtowcs (NULL
, &pattern
, 0, &ps
);
363 if (__builtin_expect (n
== (size_t) -1, 0))
365 XXX Do we have to set `errno' to something which mbsrtows hasn't
368 wpattern
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
369 assert (mbsinit (&ps
));
370 (void) mbsrtowcs (wpattern
, &pattern
, n
+ 1, &ps
);
373 assert (mbsinit (&ps
));
375 n
= strnlen (string
, 1024);
380 if (__builtin_expect (n
< 1024, 1))
382 wstring
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
383 n
= mbsrtowcs (wstring
, &p
, n
+ 1, &ps
);
384 if (__builtin_expect (n
== (size_t) -1, 0))
386 XXX Do we have to set `errno' to something which mbsrtows hasn't
391 memset (&ps
, '\0', sizeof (ps
));
392 goto prepare_wstring
;
398 n
= mbsrtowcs (NULL
, &string
, 0, &ps
);
399 if (__builtin_expect (n
== (size_t) -1, 0))
401 XXX Do we have to set `errno' to something which mbsrtows hasn't
404 wstring
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
405 assert (mbsinit (&ps
));
406 (void) mbsrtowcs (wstring
, &string
, n
+ 1, &ps
);
409 return internal_fnwmatch (wpattern
, wstring
, wstring
+ n
,
410 flags
& FNM_PERIOD
, flags
, NULL
);
412 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
414 return internal_fnmatch (pattern
, string
, string
+ strlen (string
),
415 flags
& FNM_PERIOD
, flags
, NULL
);
420 versioned_symbol (libc
, __fnmatch
, fnmatch
, GLIBC_2_2_3
);
421 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
422 strong_alias (__fnmatch
, __fnmatch_old
)
423 compat_symbol (libc
, __fnmatch_old
, fnmatch
, GLIBC_2_0
);
425 libc_hidden_ver (__fnmatch
, fnmatch
)
428 #endif /* _LIBC or not __GNU_LIBRARY__. */