1
/* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2
2003, 2004, 2005, 2006, 2007, 2009, 2010 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22
/* Enable GNU extensions in fnmatch.h. */
24
# define _GNU_SOURCE 1
27
#if ! defined __builtin_expect && __GNUC__ < 3
28
# define __builtin_expect(expr, expected) (expr)
42
#define WIDE_CHAR_SUPPORT \
43
(HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
44
&& HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
46
/* For platform which support the ISO C amendement 1 functionality we
47
support user defined character classes. */
48
#if defined _LIBC || WIDE_CHAR_SUPPORT
53
/* We need some of the locale data (the collation sequence information)
54
but there is no interface to get this information in general. Therefore
55
we support a correct implementation only in glibc. */
57
# include "../locale/localeinfo.h"
58
# include "../locale/elem-hash.h"
59
# include "../locale/coll-lookup.h"
60
# include <shlib-compat.h>
62
# define CONCAT(a,b) __CONCAT(a,b)
63
# define mbsrtowcs __mbsrtowcs
64
# define fnmatch __fnmatch
65
extern int fnmatch (const char *pattern, const char *string, int flags);
69
# define SIZE_MAX ((size_t) -1)
72
/* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
73
#define NO_LEADING_PERIOD(flags) \
74
((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
76
/* Comment out all this code if we are using the GNU C Library, and are not
77
actually compiling the library itself, and have not detected a bug
78
in the library. This code is part of the GNU C
79
Library, but also included in many other GNU distributions. Compiling
80
and linking in this code is a waste when using the GNU C library
81
(especially if it is a shared library). Rather than having every GNU
82
program understand `configure --with-gnu-libc' and omit the object files,
83
it is simpler to just do this in the source for each such file. */
85
#if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
88
# if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
89
# define isblank(c) ((c) == ' ' || (c) == '\t')
92
# define STREQ(s1, s2) (strcmp (s1, s2) == 0)
94
# if defined _LIBC || WIDE_CHAR_SUPPORT
95
/* The GNU C library provides support for user-defined character classes
96
and the functions from ISO C amendement 1. */
97
# ifdef CHARCLASS_NAME_MAX
98
# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
100
/* This shouldn't happen but some implementation might still have this
101
problem. Use a reasonable default value. */
102
# define CHAR_CLASS_MAX_LENGTH 256
106
# define IS_CHAR_CLASS(string) __wctype (string)
108
# define IS_CHAR_CLASS(string) wctype (string)
112
# define ISWCTYPE(WC, WT) __iswctype (WC, WT)
114
# define ISWCTYPE(WC, WT) iswctype (WC, WT)
117
# if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
118
/* In this case we are implementing the multibyte character handling. */
119
# define HANDLE_MULTIBYTE 1
123
# define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
125
# define IS_CHAR_CLASS(string) \
126
(STREQ (string, "alpha") || STREQ (string, "upper") \
127
|| STREQ (string, "lower") || STREQ (string, "digit") \
128
|| STREQ (string, "alnum") || STREQ (string, "xdigit") \
129
|| STREQ (string, "space") || STREQ (string, "print") \
130
|| STREQ (string, "punct") || STREQ (string, "graph") \
131
|| STREQ (string, "cntrl") || STREQ (string, "blank"))
134
/* Avoid depending on library functions or files
135
whose names are inconsistent. */
137
/* Global variable. */
138
static int posixly_correct;
140
# ifndef internal_function
141
/* Inside GNU libc we mark some function in a special way. In other
142
environments simply ignore the marking. */
143
# define internal_function
146
/* Note that this evaluates C many times. */
147
# define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
149
# define UCHAR unsigned char
151
# define FCT internal_fnmatch
152
# define EXT ext_match
153
# define END end_pattern
156
# define BTOWC(C) __btowc (C)
158
# define BTOWC(C) btowc (C)
160
# define STRLEN(S) strlen (S)
161
# define STRCAT(D, S) strcat (D, S)
163
# define MEMPCPY(D, S, N) __mempcpy (D, S, N)
166
# define MEMPCPY(D, S, N) mempcpy (D, S, N)
168
# define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
171
# define MEMCHR(S, C, N) memchr (S, C, N)
172
# define STRCOLL(S1, S2) strcoll (S1, S2)
173
# include "fnmatch_loop.c"
176
# if HANDLE_MULTIBYTE
177
# define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
178
# define CHAR wchar_t
179
# define UCHAR wint_t
181
# define FCT internal_fnwmatch
182
# define EXT ext_wmatch
183
# define END end_wpattern
184
# define L_(CS) L##CS
185
# define BTOWC(C) (C)
187
# define STRLEN(S) __wcslen (S)
188
# define STRCAT(D, S) __wcscat (D, S)
189
# define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
191
# define STRLEN(S) wcslen (S)
192
# define STRCAT(D, S) wcscat (D, S)
194
# define MEMPCPY(D, S, N) wmempcpy (D, S, N)
196
# define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
199
# define MEMCHR(S, C, N) wmemchr (S, C, N)
200
# define STRCOLL(S1, S2) wcscoll (S1, S2)
201
# define WIDE_CHAR_VERSION 1
203
# undef IS_CHAR_CLASS
204
/* We have to convert the wide character string in a multibyte string. But
205
we know that the character class names consist of alphanumeric characters
206
from the portable character set, and since the wide character encoding
207
for a member of the portable character set is the same code point as
208
its single-byte encoding, we can use a simplified method to convert the
209
string to a multibyte character string. */
211
is_char_class (const wchar_t *wcs)
213
char s[CHAR_CLASS_MAX_LENGTH + 1];
218
/* Test for a printable character from the portable character set. */
220
if (*wcs < 0x20 || *wcs > 0x7e
221
|| *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
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'+': case L',': case L'-': case L'.': case L'/':
229
case L'0': case L'1': case L'2': case L'3': case L'4':
230
case L'5': case L'6': case L'7': case L'8': case L'9':
231
case L':': case L';': case L'<': case L'=': case L'>':
233
case L'A': case L'B': case L'C': case L'D': case L'E':
234
case L'F': case L'G': case L'H': case L'I': case L'J':
235
case L'K': case L'L': case L'M': case L'N': case L'O':
236
case L'P': case L'Q': case L'R': case L'S': case L'T':
237
case L'U': case L'V': case L'W': case L'X': case L'Y':
239
case L'[': case L'\\': case L']': case L'^': case L'_':
240
case L'a': case L'b': case L'c': case L'd': case L'e':
241
case L'f': case L'g': case L'h': case L'i': case L'j':
242
case L'k': case L'l': case L'm': case L'n': case L'o':
243
case L'p': case L'q': case L'r': case L's': case L't':
244
case L'u': case L'v': case L'w': case L'x': case L'y':
245
case L'z': case L'{': case L'|': case L'}': case L'~':
252
/* Avoid overrunning the buffer. */
253
if (cp == s + CHAR_CLASS_MAX_LENGTH)
256
*cp++ = (char) *wcs++;
258
while (*wcs != L'\0');
268
# define IS_CHAR_CLASS(string) is_char_class (string)
270
# include "fnmatch_loop.c"
275
fnmatch (const char *pattern, const char *string, int flags)
277
# if HANDLE_MULTIBYTE
278
# define ALLOCA_LIMIT 2000
279
if (__builtin_expect (MB_CUR_MAX, 1) != 1)
289
/* Calculate the size needed to convert the strings to
291
memset (&ps, '\0', sizeof (ps));
292
patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
293
if (__builtin_expect (patsize != 0, 1))
295
assert (mbsinit (&ps));
296
strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
297
if (__builtin_expect (strsize != 0, 1))
299
assert (mbsinit (&ps));
300
totsize = patsize + strsize;
301
if (__builtin_expect (! (patsize <= totsize
302
&& totsize <= SIZE_MAX / sizeof (wchar_t)),
309
/* Allocate room for the wide characters. */
310
if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
311
wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
314
wpattern = malloc (totsize * sizeof (wchar_t));
315
if (__builtin_expect (! wpattern, 0))
321
wstring = wpattern + patsize;
323
/* Convert the strings into wide characters. */
324
mbsrtowcs (wpattern, &pattern, patsize, &ps);
325
assert (mbsinit (&ps));
326
mbsrtowcs (wstring, &string, strsize, &ps);
328
res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
329
flags & FNM_PERIOD, flags);
331
if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
338
# endif /* HANDLE_MULTIBYTE */
340
return internal_fnmatch (pattern, string, string + strlen (string),
341
flags & FNM_PERIOD, flags);
346
versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
347
# if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
348
strong_alias (__fnmatch, __fnmatch_old)
349
compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
351
libc_hidden_ver (__fnmatch, fnmatch)
354
#endif /* _LIBC or not __GNU_LIBRARY__. */