Mention BZ 14050/14117 in NEWS
[glibc.git] / posix / fnmatch.c
blob8ec983675969ca9fe2c5ec5a293413430d4217dd
1 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2007,2010,2011
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, see
17 <http://www.gnu.org/licenses/>. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /* Enable GNU extensions in fnmatch.h. */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE 1
26 #endif
28 #include <assert.h>
29 #include <errno.h>
30 #include <fnmatch.h>
31 #include <ctype.h>
33 #if HAVE_STRING_H || defined _LIBC
34 # include <string.h>
35 #else
36 # include <strings.h>
37 #endif
39 #if defined STDC_HEADERS || defined _LIBC
40 # include <stdlib.h>
41 #endif
43 #ifdef _LIBC
44 # include <alloca.h>
45 #else
46 # define alloca_account(size., var) alloca (size)
47 #endif
49 /* For platform which support the ISO C amendement 1 functionality we
50 support user defined character classes. */
51 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
52 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
53 # include <wchar.h>
54 # include <wctype.h>
55 #endif
57 /* We need some of the locale data (the collation sequence information)
58 but there is no interface to get this information in general. Therefore
59 we support a correct implementation only in glibc. */
60 #ifdef _LIBC
61 # include "../locale/localeinfo.h"
62 # include "../locale/elem-hash.h"
63 # include "../locale/coll-lookup.h"
64 # include <shlib-compat.h>
66 # define CONCAT(a,b) __CONCAT(a,b)
67 # define mbsrtowcs __mbsrtowcs
68 # define fnmatch __fnmatch
69 extern int fnmatch (const char *pattern, const char *string, int flags);
70 #endif
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. 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__
87 # if defined STDC_HEADERS || !defined isascii
88 # define ISASCII(c) 1
89 # else
90 # define ISASCII(c) isascii(c)
91 # endif
93 # ifdef isblank
94 # define ISBLANK(c) (ISASCII (c) && isblank (c))
95 # else
96 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
97 # endif
98 # ifdef isgraph
99 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
100 # else
101 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
102 # endif
104 # define ISPRINT(c) (ISASCII (c) && isprint (c))
105 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
106 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
107 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
108 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
109 # define ISLOWER(c) (ISASCII (c) && islower (c))
110 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
111 # define ISSPACE(c) (ISASCII (c) && isspace (c))
112 # define ISUPPER(c) (ISASCII (c) && isupper (c))
113 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
115 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
117 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
118 /* The GNU C library provides support for user-defined character classes
119 and the functions from ISO C amendement 1. */
120 # ifdef CHARCLASS_NAME_MAX
121 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
122 # else
123 /* This shouldn't happen but some implementation might still have this
124 problem. Use a reasonable default value. */
125 # define CHAR_CLASS_MAX_LENGTH 256
126 # endif
128 # ifdef _LIBC
129 # define IS_CHAR_CLASS(string) __wctype (string)
130 # else
131 # define IS_CHAR_CLASS(string) wctype (string)
132 # endif
134 # ifdef _LIBC
135 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
136 # else
137 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
138 # endif
140 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
141 /* In this case we are implementing the multibyte character handling. */
142 # define HANDLE_MULTIBYTE 1
143 # endif
145 # else
146 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
148 # define IS_CHAR_CLASS(string) \
149 (STREQ (string, "alpha") || STREQ (string, "upper") \
150 || STREQ (string, "lower") || STREQ (string, "digit") \
151 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
152 || STREQ (string, "space") || STREQ (string, "print") \
153 || STREQ (string, "punct") || STREQ (string, "graph") \
154 || STREQ (string, "cntrl") || STREQ (string, "blank"))
155 # endif
157 /* Avoid depending on library functions or files
158 whose names are inconsistent. */
160 # if !defined _LIBC && !defined getenv
161 extern char *getenv ();
162 # endif
164 # ifndef errno
165 extern int errno;
166 # endif
168 /* Global variable. */
169 static int posixly_correct;
171 /* This function doesn't exist on most systems. */
173 # if !defined HAVE___STRCHRNUL && !defined _LIBC
174 static char *
175 __strchrnul (s, c)
176 const char *s;
177 int c;
179 char *result = strchr (s, c);
180 if (result == NULL)
181 result = strchr (s, '\0');
182 return result;
184 # endif
186 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
187 static wchar_t *
188 __wcschrnul (s, c)
189 const wchar_t *s;
190 wint_t c;
192 wchar_t *result = wcschr (s, c);
193 if (result == NULL)
194 result = wcschr (s, '\0');
195 return result;
197 # endif
199 # ifndef internal_function
200 /* Inside GNU libc we mark some function in a special way. In other
201 environments simply ignore the marking. */
202 # define internal_function
203 # endif
205 /* Note that this evaluates C many times. */
206 # ifdef _LIBC
207 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
208 # else
209 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
210 # endif
211 # define CHAR char
212 # define UCHAR unsigned char
213 # define INT int
214 # define FCT internal_fnmatch
215 # define EXT ext_match
216 # define END end_pattern
217 # define STRUCT fnmatch_struct
218 # define L(CS) CS
219 # ifdef _LIBC
220 # define BTOWC(C) __btowc (C)
221 # else
222 # define BTOWC(C) btowc (C)
223 # endif
224 # define STRLEN(S) strlen (S)
225 # define STRCAT(D, S) strcat (D, S)
226 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
227 # define MEMCHR(S, C, N) memchr (S, C, N)
228 # define STRCOLL(S1, S2) strcoll (S1, S2)
229 # include "fnmatch_loop.c"
232 # if HANDLE_MULTIBYTE
233 /* Note that this evaluates C many times. */
234 # ifdef _LIBC
235 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
236 # else
237 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
238 # endif
239 # define CHAR wchar_t
240 # define UCHAR wint_t
241 # define INT wint_t
242 # define FCT internal_fnwmatch
243 # define EXT ext_wmatch
244 # define END end_wpattern
245 # define STRUCT fnwmatch_struct
246 # define L(CS) L##CS
247 # define BTOWC(C) (C)
248 # define STRLEN(S) __wcslen (S)
249 # define STRCAT(D, S) __wcscat (D, S)
250 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
251 # define MEMCHR(S, C, N) wmemchr (S, C, N)
252 # define STRCOLL(S1, S2) wcscoll (S1, S2)
253 # define WIDE_CHAR_VERSION 1
255 # undef IS_CHAR_CLASS
256 /* We have to convert the wide character string in a multibyte string. But
257 we know that the character class names consist of alphanumeric characters
258 from the portable character set, and since the wide character encoding
259 for a member of the portable character set is the same code point as
260 its single-byte encoding, we can use a simplified method to convert the
261 string to a multibyte character string. */
262 static wctype_t
263 is_char_class (const wchar_t *wcs)
265 char s[CHAR_CLASS_MAX_LENGTH + 1];
266 char *cp = s;
270 /* Test for a printable character from the portable character set. */
271 # ifdef _LIBC
272 if (*wcs < 0x20 || *wcs > 0x7e
273 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
274 return (wctype_t) 0;
275 # else
276 switch (*wcs)
278 case L' ': case L'!': case L'"': case L'#': case L'%':
279 case L'&': case L'\'': case L'(': case L')': case L'*':
280 case L'+': case L',': case L'-': case L'.': case L'/':
281 case L'0': case L'1': case L'2': case L'3': case L'4':
282 case L'5': case L'6': case L'7': case L'8': case L'9':
283 case L':': case L';': case L'<': case L'=': case L'>':
284 case L'?':
285 case L'A': case L'B': case L'C': case L'D': case L'E':
286 case L'F': case L'G': case L'H': case L'I': case L'J':
287 case L'K': case L'L': case L'M': case L'N': case L'O':
288 case L'P': case L'Q': case L'R': case L'S': case L'T':
289 case L'U': case L'V': case L'W': case L'X': case L'Y':
290 case L'Z':
291 case L'[': case L'\\': case L']': case L'^': case L'_':
292 case L'a': case L'b': case L'c': case L'd': case L'e':
293 case L'f': case L'g': case L'h': case L'i': case L'j':
294 case L'k': case L'l': case L'm': case L'n': case L'o':
295 case L'p': case L'q': case L'r': case L's': case L't':
296 case L'u': case L'v': case L'w': case L'x': case L'y':
297 case L'z': case L'{': case L'|': case L'}': case L'~':
298 break;
299 default:
300 return (wctype_t) 0;
302 # endif
304 /* Avoid overrunning the buffer. */
305 if (cp == s + CHAR_CLASS_MAX_LENGTH)
306 return (wctype_t) 0;
308 *cp++ = (char) *wcs++;
310 while (*wcs != L'\0');
312 *cp = '\0';
314 # ifdef _LIBC
315 return __wctype (s);
316 # else
317 return wctype (s);
318 # endif
320 # define IS_CHAR_CLASS(string) is_char_class (string)
322 # include "fnmatch_loop.c"
323 # endif
327 fnmatch (pattern, string, flags)
328 const char *pattern;
329 const char *string;
330 int flags;
332 # if HANDLE_MULTIBYTE
333 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
335 mbstate_t ps;
336 size_t n;
337 const char *p;
338 wchar_t *wpattern_malloc = NULL;
339 wchar_t *wpattern;
340 wchar_t *wstring_malloc = NULL;
341 wchar_t *wstring;
342 size_t alloca_used = 0;
344 /* Convert the strings into wide characters. */
345 memset (&ps, '\0', sizeof (ps));
346 p = pattern;
347 #ifdef _LIBC
348 n = strnlen (pattern, 1024);
349 #else
350 n = strlen (pattern);
351 #endif
352 if (__builtin_expect (n < 1024, 1))
354 wpattern = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
355 alloca_used);
356 n = mbsrtowcs (wpattern, &p, n + 1, &ps);
357 if (__builtin_expect (n == (size_t) -1, 0))
358 /* Something wrong.
359 XXX Do we have to set `errno' to something which mbsrtows hasn't
360 already done? */
361 return -1;
362 if (p)
364 memset (&ps, '\0', sizeof (ps));
365 goto prepare_wpattern;
368 else
370 prepare_wpattern:
371 n = mbsrtowcs (NULL, &pattern, 0, &ps);
372 if (__builtin_expect (n == (size_t) -1, 0))
373 /* Something wrong.
374 XXX Do we have to set `errno' to something which mbsrtows hasn't
375 already done? */
376 return -1;
377 if (__builtin_expect (n >= (size_t) -1 / sizeof (wchar_t), 0))
379 __set_errno (ENOMEM);
380 return -2;
382 wpattern_malloc = wpattern
383 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
384 assert (mbsinit (&ps));
385 if (wpattern == NULL)
386 return -2;
387 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
390 assert (mbsinit (&ps));
391 #ifdef _LIBC
392 n = strnlen (string, 1024);
393 #else
394 n = strlen (string);
395 #endif
396 p = string;
397 if (__builtin_expect (n < 1024, 1))
399 wstring = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
400 alloca_used);
401 n = mbsrtowcs (wstring, &p, n + 1, &ps);
402 if (__builtin_expect (n == (size_t) -1, 0))
404 /* Something wrong.
405 XXX Do we have to set `errno' to something which
406 mbsrtows hasn't already done? */
407 free_return:
408 free (wpattern_malloc);
409 return -1;
411 if (p)
413 memset (&ps, '\0', sizeof (ps));
414 goto prepare_wstring;
417 else
419 prepare_wstring:
420 n = mbsrtowcs (NULL, &string, 0, &ps);
421 if (__builtin_expect (n == (size_t) -1, 0))
422 /* Something wrong.
423 XXX Do we have to set `errno' to something which mbsrtows hasn't
424 already done? */
425 goto free_return;
426 if (__builtin_expect (n >= (size_t) -1 / sizeof (wchar_t), 0))
428 free (wpattern_malloc);
429 __set_errno (ENOMEM);
430 return -2;
433 wstring_malloc = wstring
434 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
435 if (wstring == NULL)
437 free (wpattern_malloc);
438 return -2;
440 assert (mbsinit (&ps));
441 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
444 int res = internal_fnwmatch (wpattern, wstring, wstring + n,
445 flags & FNM_PERIOD, flags, NULL,
446 alloca_used);
448 free (wstring_malloc);
449 free (wpattern_malloc);
451 return res;
453 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
455 return internal_fnmatch (pattern, string, string + strlen (string),
456 flags & FNM_PERIOD, flags, NULL, 0);
459 # ifdef _LIBC
460 # undef fnmatch
461 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
462 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
463 strong_alias (__fnmatch, __fnmatch_old)
464 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
465 # endif
466 libc_hidden_ver (__fnmatch, fnmatch)
467 # endif
469 #endif /* _LIBC or not __GNU_LIBRARY__. */