1 /* Copyright (C) 1991-1993, 1996-2007, 2009-2011 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Enable GNU extensions in fnmatch.h. */
23 # define _GNU_SOURCE 1
26 #if ! defined __builtin_expect && __GNUC__ < 3
27 # define __builtin_expect(expr, expected) (expr)
41 #define WIDE_CHAR_SUPPORT \
42 (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
43 && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
45 /* For platform which support the ISO C amendement 1 functionality we
46 support user defined character classes. */
47 #if defined _LIBC || WIDE_CHAR_SUPPORT
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);
68 # define SIZE_MAX ((size_t) -1)
71 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
72 #define NO_LEADING_PERIOD(flags) \
73 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
75 /* Comment out all this code if we are using the GNU C Library, and are not
76 actually compiling the library itself, and have not detected a bug
77 in the library. This code is part of the GNU C
78 Library, but also included in many other GNU distributions. Compiling
79 and linking in this code is a waste when using the GNU C library
80 (especially if it is a shared library). Rather than having every GNU
81 program understand `configure --with-gnu-libc' and omit the object files,
82 it is simpler to just do this in the source for each such file. */
84 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
87 # if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
88 # define isblank(c) ((c) == ' ' || (c) == '\t')
91 # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
93 # if defined _LIBC || WIDE_CHAR_SUPPORT
94 /* The GNU C library provides support for user-defined character classes
95 and the functions from ISO C amendement 1. */
96 # ifdef CHARCLASS_NAME_MAX
97 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
99 /* This shouldn't happen but some implementation might still have this
100 problem. Use a reasonable default value. */
101 # define CHAR_CLASS_MAX_LENGTH 256
105 # define IS_CHAR_CLASS(string) __wctype (string)
107 # define IS_CHAR_CLASS(string) wctype (string)
111 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
113 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
116 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
117 /* In this case we are implementing the multibyte character handling. */
118 # define HANDLE_MULTIBYTE 1
122 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
124 # define IS_CHAR_CLASS(string) \
125 (STREQ (string, "alpha") || STREQ (string, "upper") \
126 || STREQ (string, "lower") || STREQ (string, "digit") \
127 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
128 || STREQ (string, "space") || STREQ (string, "print") \
129 || STREQ (string, "punct") || STREQ (string, "graph") \
130 || STREQ (string, "cntrl") || STREQ (string, "blank"))
133 /* Avoid depending on library functions or files
134 whose names are inconsistent. */
136 /* Global variable. */
137 static int posixly_correct;
139 # ifndef internal_function
140 /* Inside GNU libc we mark some function in a special way. In other
141 environments simply ignore the marking. */
142 # define internal_function
145 /* Note that this evaluates C many times. */
146 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
148 # define UCHAR unsigned char
150 # define FCT internal_fnmatch
151 # define EXT ext_match
152 # define END end_pattern
155 # define BTOWC(C) __btowc (C)
157 # define BTOWC(C) btowc (C)
159 # define STRLEN(S) strlen (S)
160 # define STRCAT(D, S) strcat (D, S)
162 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
165 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
167 # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
170 # define MEMCHR(S, C, N) memchr (S, C, N)
171 # define STRCOLL(S1, S2) strcoll (S1, S2)
172 # include "fnmatch_loop.c"
175 # if HANDLE_MULTIBYTE
176 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
177 # define CHAR wchar_t
178 # define UCHAR wint_t
180 # define FCT internal_fnwmatch
181 # define EXT ext_wmatch
182 # define END end_wpattern
183 # define L_(CS) L##CS
184 # define BTOWC(C) (C)
186 # define STRLEN(S) __wcslen (S)
187 # define STRCAT(D, S) __wcscat (D, S)
188 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
190 # define STRLEN(S) wcslen (S)
191 # define STRCAT(D, S) wcscat (D, S)
193 # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
195 # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
198 # define MEMCHR(S, C, N) wmemchr (S, C, N)
199 # define STRCOLL(S1, S2) wcscoll (S1, S2)
200 # define WIDE_CHAR_VERSION 1
202 # undef IS_CHAR_CLASS
203 /* We have to convert the wide character string in a multibyte string. But
204 we know that the character class names consist of alphanumeric characters
205 from the portable character set, and since the wide character encoding
206 for a member of the portable character set is the same code point as
207 its single-byte encoding, we can use a simplified method to convert the
208 string to a multibyte character string. */
210 is_char_class (const wchar_t *wcs)
212 char s[CHAR_CLASS_MAX_LENGTH + 1];
217 /* Test for a printable character from the portable character set. */
219 if (*wcs < 0x20 || *wcs > 0x7e
220 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
225 case L' ': case L'!': case L'"': case L'#': case L'%':
226 case L'&': case L'\'': case L'(': case L')': case L'*':
227 case L'+': case L',': case L'-': case L'.': case L'/':
228 case L'0': case L'1': case L'2': case L'3': case L'4':
229 case L'5': case L'6': case L'7': case L'8': case L'9':
230 case L':': case L';': case L'<': case L'=': case L'>':
232 case L'A': case L'B': case L'C': case L'D': case L'E':
233 case L'F': case L'G': case L'H': case L'I': case L'J':
234 case L'K': case L'L': case L'M': case L'N': case L'O':
235 case L'P': case L'Q': case L'R': case L'S': case L'T':
236 case L'U': case L'V': case L'W': case L'X': case L'Y':
238 case L'[': case L'\\': case L']': case L'^': case L'_':
239 case L'a': case L'b': case L'c': case L'd': case L'e':
240 case L'f': case L'g': case L'h': case L'i': case L'j':
241 case L'k': case L'l': case L'm': case L'n': case L'o':
242 case L'p': case L'q': case L'r': case L's': case L't':
243 case L'u': case L'v': case L'w': case L'x': case L'y':
244 case L'z': case L'{': case L'|': case L'}': case L'~':
251 /* Avoid overrunning the buffer. */
252 if (cp == s + CHAR_CLASS_MAX_LENGTH)
255 *cp++ = (char) *wcs++;
257 while (*wcs != L'\0');
267 # define IS_CHAR_CLASS(string) is_char_class (string)
269 # include "fnmatch_loop.c"
274 fnmatch (const char *pattern, const char *string, int flags)
276 # if HANDLE_MULTIBYTE
277 # define ALLOCA_LIMIT 2000
278 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
288 /* Calculate the size needed to convert the strings to
290 memset (&ps, '\0', sizeof (ps));
291 patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
292 if (__builtin_expect (patsize != 0, 1))
294 assert (mbsinit (&ps));
295 strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
296 if (__builtin_expect (strsize != 0, 1))
298 assert (mbsinit (&ps));
299 totsize = patsize + strsize;
300 if (__builtin_expect (! (patsize <= totsize
301 && totsize <= SIZE_MAX / sizeof (wchar_t)),
308 /* Allocate room for the wide characters. */
309 if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
310 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
313 wpattern = malloc (totsize * sizeof (wchar_t));
314 if (__builtin_expect (! wpattern, 0))
320 wstring = wpattern + patsize;
322 /* Convert the strings into wide characters. */
323 mbsrtowcs (wpattern, &pattern, patsize, &ps);
324 assert (mbsinit (&ps));
325 mbsrtowcs (wstring, &string, strsize, &ps);
327 res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
328 flags & FNM_PERIOD, flags);
330 if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
337 # endif /* HANDLE_MULTIBYTE */
339 return internal_fnmatch (pattern, string, string + strlen (string),
340 flags & FNM_PERIOD, flags);
345 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
346 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
347 strong_alias (__fnmatch, __fnmatch_old)
348 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
350 libc_hidden_ver (__fnmatch, fnmatch)
353 #endif /* _LIBC or not __GNU_LIBRARY__. */