1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2000, 2001,
2 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Enable GNU extensions in fnmatch.h. */
24 # define _GNU_SOURCE 1
28 # define alloca __builtin_alloca
29 # define HAVE_ALLOCA 1
31 # if defined HAVE_ALLOCA_H || defined _LIBC
44 #if ! defined __builtin_expect && __GNUC__ < 3
45 # define __builtin_expect(expr, expected) (expr)
53 #if HAVE_STRING_H || defined _LIBC
61 #if defined STDC_HEADERS || defined _LIBC
66 /* For platform which support the ISO C amendement 1 functionality we
67 support user defined character classes. */
68 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
69 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
74 /* We need some of the locale data (the collation sequence information)
75 but there is no interface to get this information in general. Therefore
76 we support a correct implementation only in glibc. */
78 # include "../locale/localeinfo.h"
79 # include "../locale/elem-hash.h"
80 # include "../locale/coll-lookup.h"
81 # include <shlib-compat.h>
83 # define CONCAT(a,b) __CONCAT(a,b)
84 # define mbsinit __mbsinit
85 # define mbsrtowcs __mbsrtowcs
86 # define fnmatch __fnmatch
87 extern int fnmatch (const char *pattern, const char *string, int flags);
90 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
91 #define NO_LEADING_PERIOD(flags) \
92 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
94 /* Comment out all this code if we are using the GNU C Library, are not
95 actually compiling the library itself, and have not detected a bug
96 in the library. This code is part of the GNU C
97 Library, but also included in many other GNU distributions. Compiling
98 and linking in this code is a waste when using the GNU C library
99 (especially if it is a shared library). Rather than having every GNU
100 program understand `configure --with-gnu-libc' and omit the object files,
101 it is simpler to just do this in the source for each such file. */
103 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
106 # if defined STDC_HEADERS || !defined isascii
107 # define ISASCII(c) 1
109 # define ISASCII(c) isascii(c)
113 # define ISBLANK(c) (ISASCII (c) && isblank (c))
115 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
118 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
120 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
123 # define ISPRINT(c) (ISASCII (c) && isprint (c))
124 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
125 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
126 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
127 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
128 # define ISLOWER(c) (ISASCII (c) && islower (c))
129 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
130 # define ISSPACE(c) (ISASCII (c) && isspace (c))
131 # define ISUPPER(c) (ISASCII (c) && isupper (c))
132 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
134 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
136 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
137 /* The GNU C library provides support for user-defined character classes
138 and the functions from ISO C amendement 1. */
139 # ifdef CHARCLASS_NAME_MAX
140 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
142 /* This shouldn't happen but some implementation might still have this
143 problem. Use a reasonable default value. */
144 # define CHAR_CLASS_MAX_LENGTH 256
148 # define IS_CHAR_CLASS(string) __wctype (string)
150 # define IS_CHAR_CLASS(string) wctype (string)
154 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
156 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
159 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
160 /* In this case we are implementing the multibyte character handling. */
161 # define HANDLE_MULTIBYTE 1
165 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
167 # define IS_CHAR_CLASS(string) \
168 (STREQ (string, "alpha") || STREQ (string, "upper") \
169 || STREQ (string, "lower") || STREQ (string, "digit") \
170 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
171 || STREQ (string, "space") || STREQ (string, "print") \
172 || STREQ (string, "punct") || STREQ (string, "graph") \
173 || STREQ (string, "cntrl") || STREQ (string, "blank"))
176 /* Avoid depending on library functions or files
177 whose names are inconsistent. */
179 # if !defined _LIBC && !defined getenv && !HAVE_DECL_GETENV
180 extern char *getenv ();
187 /* Global variable. */
188 static int posixly_correct;
190 # ifndef internal_function
191 /* Inside GNU libc we mark some function in a special way. In other
192 environments simply ignore the marking. */
193 # define internal_function
196 /* Note that this evaluates C many times. */
198 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
200 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
203 # define UCHAR unsigned char
205 # define FCT internal_fnmatch
206 # define EXT ext_match
207 # define END end_pattern
210 # define BTOWC(C) __btowc (C)
212 # define BTOWC(C) btowc (C)
214 # define STRLEN(S) strlen (S)
215 # define STRCAT(D, S) strcat (D, S)
217 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
220 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
222 # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
225 # define MEMCHR(S, C, N) memchr (S, C, N)
226 # define STRCOLL(S1, S2) strcoll (S1, S2)
227 # include "fnmatch_loop.c"
230 # if HANDLE_MULTIBYTE
231 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
232 # define CHAR wchar_t
233 # define UCHAR wint_t
235 # define FCT internal_fnwmatch
236 # define EXT ext_wmatch
237 # define END end_wpattern
239 # define BTOWC(C) (C)
241 # define STRLEN(S) __wcslen (S)
242 # define STRCAT(D, S) __wcscat (D, S)
243 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
245 # define STRLEN(S) wcslen (S)
246 # define STRCAT(D, S) wcscat (D, S)
248 # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
250 # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
253 # define MEMCHR(S, C, N) wmemchr (S, C, N)
254 # define STRCOLL(S1, S2) wcscoll (S1, S2)
255 # define WIDE_CHAR_VERSION 1
257 # undef IS_CHAR_CLASS
258 /* We have to convert the wide character string in a multibyte string. But
259 we know that the character class names consist of alphanumeric characters
260 from the portable character set, and since the wide character encoding
261 for a member of the portable character set is the same code point as
262 its single-byte encoding, we can use a simplified method to convert the
263 string to a multibyte character string. */
265 is_char_class (const wchar_t *wcs)
267 char s[CHAR_CLASS_MAX_LENGTH + 1];
272 /* Test for a printable character from the portable character set. */
274 if (*wcs < 0x20 || *wcs > 0x7e
275 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
280 case L' ': case L'!': case L'"': case L'#': case L'%':
281 case L'&': case L'\'': case L'(': case L')': case L'*':
282 case L'+': case L',': case L'-': case L'.': case L'/':
283 case L'0': case L'1': case L'2': case L'3': case L'4':
284 case L'5': case L'6': case L'7': case L'8': case L'9':
285 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':
293 case L'[': case L'\\': case L']': case L'^': case L'_':
294 case L'a': case L'b': case L'c': case L'd': case L'e':
295 case L'f': case L'g': case L'h': case L'i': case L'j':
296 case L'k': case L'l': case L'm': case L'n': case L'o':
297 case L'p': case L'q': case L'r': case L's': case L't':
298 case L'u': case L'v': case L'w': case L'x': case L'y':
299 case L'z': case L'{': case L'|': case L'}': case L'~':
306 /* Avoid overrunning the buffer. */
307 if (cp == s + CHAR_CLASS_MAX_LENGTH)
310 *cp++ = (char) *wcs++;
312 while (*wcs != L'\0');
322 # define IS_CHAR_CLASS(string) is_char_class (string)
324 # include "fnmatch_loop.c"
329 fnmatch (pattern, string, flags)
334 # if HANDLE_MULTIBYTE
335 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
342 /* Convert the strings into wide characters. */
343 memset (&ps, '\0', sizeof (ps));
344 n = mbsrtowcs (NULL, &pattern, 0, &ps);
345 if (__builtin_expect (n, 0) == (size_t) -1)
347 XXX Do we have to set `errno' to something which mbsrtows hasn't
350 wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
351 assert (mbsinit (&ps));
352 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
354 assert (mbsinit (&ps));
355 n = mbsrtowcs (NULL, &string, 0, &ps);
356 if (__builtin_expect (n, 0) == (size_t) -1)
358 XXX Do we have to set `errno' to something which mbsrtows hasn't
361 wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
362 assert (mbsinit (&ps));
363 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
365 return internal_fnwmatch (wpattern, wstring, wstring + n,
366 flags & FNM_PERIOD, flags);
368 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
370 return internal_fnmatch (pattern, string, string + strlen (string),
371 flags & FNM_PERIOD, flags);
376 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
377 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
378 strong_alias (__fnmatch, __fnmatch_old)
379 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
383 #endif /* _LIBC or not __GNU_LIBRARY__. */