Add SSE4.2 support for strcasecmp and strncasecmp on x86-32
[glibc.git] / posix / fnmatch.c
blob819a6a76f6bcc2de5ecf3e3ef149cf2b16a4f817
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 /* Enable GNU extensions in fnmatch.h. */
25 #ifndef _GNU_SOURCE
26 # define _GNU_SOURCE 1
27 #endif
29 #include <assert.h>
30 #include <errno.h>
31 #include <fnmatch.h>
32 #include <ctype.h>
34 #if HAVE_STRING_H || defined _LIBC
35 # include <string.h>
36 #else
37 # include <strings.h>
38 #endif
40 #if defined STDC_HEADERS || defined _LIBC
41 # include <stdlib.h>
42 #endif
44 #ifdef _LIBC
45 # include <alloca.h>
46 #else
47 # define alloca_account(size., var) alloca (size)
48 #endif
50 /* For platform which support the ISO C amendement 1 functionality we
51 support user defined character classes. */
52 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
53 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
54 # include <wchar.h>
55 # include <wctype.h>
56 #endif
58 /* We need some of the locale data (the collation sequence information)
59 but there is no interface to get this information in general. Therefore
60 we support a correct implementation only in glibc. */
61 #ifdef _LIBC
62 # include "../locale/localeinfo.h"
63 # include "../locale/elem-hash.h"
64 # include "../locale/coll-lookup.h"
65 # include <shlib-compat.h>
67 # define CONCAT(a,b) __CONCAT(a,b)
68 # define mbsrtowcs __mbsrtowcs
69 # define fnmatch __fnmatch
70 extern int fnmatch (const char *pattern, const char *string, int flags);
71 #endif
73 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
74 #define NO_LEADING_PERIOD(flags) \
75 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
77 /* Comment out all this code if we are using the GNU C Library, and are not
78 actually compiling the library itself. 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__
88 # if defined STDC_HEADERS || !defined isascii
89 # define ISASCII(c) 1
90 # else
91 # define ISASCII(c) isascii(c)
92 # endif
94 # ifdef isblank
95 # define ISBLANK(c) (ISASCII (c) && isblank (c))
96 # else
97 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
98 # endif
99 # ifdef isgraph
100 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
101 # else
102 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
103 # endif
105 # define ISPRINT(c) (ISASCII (c) && isprint (c))
106 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
107 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
108 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
109 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
110 # define ISLOWER(c) (ISASCII (c) && islower (c))
111 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
112 # define ISSPACE(c) (ISASCII (c) && isspace (c))
113 # define ISUPPER(c) (ISASCII (c) && isupper (c))
114 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
116 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
118 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
119 /* The GNU C library provides support for user-defined character classes
120 and the functions from ISO C amendement 1. */
121 # ifdef CHARCLASS_NAME_MAX
122 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
123 # else
124 /* This shouldn't happen but some implementation might still have this
125 problem. Use a reasonable default value. */
126 # define CHAR_CLASS_MAX_LENGTH 256
127 # endif
129 # ifdef _LIBC
130 # define IS_CHAR_CLASS(string) __wctype (string)
131 # else
132 # define IS_CHAR_CLASS(string) wctype (string)
133 # endif
135 # ifdef _LIBC
136 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
137 # else
138 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
139 # endif
141 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
142 /* In this case we are implementing the multibyte character handling. */
143 # define HANDLE_MULTIBYTE 1
144 # endif
146 # else
147 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
149 # define IS_CHAR_CLASS(string) \
150 (STREQ (string, "alpha") || STREQ (string, "upper") \
151 || STREQ (string, "lower") || STREQ (string, "digit") \
152 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
153 || STREQ (string, "space") || STREQ (string, "print") \
154 || STREQ (string, "punct") || STREQ (string, "graph") \
155 || STREQ (string, "cntrl") || STREQ (string, "blank"))
156 # endif
158 /* Avoid depending on library functions or files
159 whose names are inconsistent. */
161 # if !defined _LIBC && !defined getenv
162 extern char *getenv ();
163 # endif
165 # ifndef errno
166 extern int errno;
167 # endif
169 /* Global variable. */
170 static int posixly_correct;
172 /* This function doesn't exist on most systems. */
174 # if !defined HAVE___STRCHRNUL && !defined _LIBC
175 static char *
176 __strchrnul (s, c)
177 const char *s;
178 int c;
180 char *result = strchr (s, c);
181 if (result == NULL)
182 result = strchr (s, '\0');
183 return result;
185 # endif
187 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
188 static wchar_t *
189 __wcschrnul (s, c)
190 const wchar_t *s;
191 wint_t c;
193 wchar_t *result = wcschr (s, c);
194 if (result == NULL)
195 result = wcschr (s, '\0');
196 return result;
198 # endif
200 # ifndef internal_function
201 /* Inside GNU libc we mark some function in a special way. In other
202 environments simply ignore the marking. */
203 # define internal_function
204 # endif
206 /* Note that this evaluates C many times. */
207 # ifdef _LIBC
208 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
209 # else
210 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
211 # endif
212 # define CHAR char
213 # define UCHAR unsigned char
214 # define INT int
215 # define FCT internal_fnmatch
216 # define EXT ext_match
217 # define END end_pattern
218 # define STRUCT fnmatch_struct
219 # define L(CS) CS
220 # ifdef _LIBC
221 # define BTOWC(C) __btowc (C)
222 # else
223 # define BTOWC(C) btowc (C)
224 # endif
225 # define STRLEN(S) strlen (S)
226 # define STRCAT(D, S) strcat (D, S)
227 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
228 # define MEMCHR(S, C, N) memchr (S, C, N)
229 # define STRCOLL(S1, S2) strcoll (S1, S2)
230 # include "fnmatch_loop.c"
233 # if HANDLE_MULTIBYTE
234 /* Note that this evaluates C many times. */
235 # ifdef _LIBC
236 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
237 # else
238 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
239 # endif
240 # define CHAR wchar_t
241 # define UCHAR wint_t
242 # define INT wint_t
243 # define FCT internal_fnwmatch
244 # define EXT ext_wmatch
245 # define END end_wpattern
246 # define STRUCT fnwmatch_struct
247 # define L(CS) L##CS
248 # define BTOWC(C) (C)
249 # define STRLEN(S) __wcslen (S)
250 # define STRCAT(D, S) __wcscat (D, S)
251 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
252 # define MEMCHR(S, C, N) wmemchr (S, C, N)
253 # define STRCOLL(S1, S2) wcscoll (S1, S2)
254 # define WIDE_CHAR_VERSION 1
256 # undef IS_CHAR_CLASS
257 /* We have to convert the wide character string in a multibyte string. But
258 we know that the character class names consist of alphanumeric characters
259 from the portable character set, and since the wide character encoding
260 for a member of the portable character set is the same code point as
261 its single-byte encoding, we can use a simplified method to convert the
262 string to a multibyte character string. */
263 static wctype_t
264 is_char_class (const wchar_t *wcs)
266 char s[CHAR_CLASS_MAX_LENGTH + 1];
267 char *cp = s;
271 /* Test for a printable character from the portable character set. */
272 # ifdef _LIBC
273 if (*wcs < 0x20 || *wcs > 0x7e
274 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
275 return (wctype_t) 0;
276 # else
277 switch (*wcs)
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'+': case L',': case L'-': case L'.': case L'/':
282 case L'0': case L'1': case L'2': case L'3': case L'4':
283 case L'5': case L'6': case L'7': case L'8': case L'9':
284 case L':': case L';': case L'<': case L'=': case L'>':
285 case L'?':
286 case L'A': case L'B': case L'C': case L'D': case L'E':
287 case L'F': case L'G': case L'H': case L'I': case L'J':
288 case L'K': case L'L': case L'M': case L'N': case L'O':
289 case L'P': case L'Q': case L'R': case L'S': case L'T':
290 case L'U': case L'V': case L'W': case L'X': case L'Y':
291 case L'Z':
292 case L'[': case L'\\': case L']': case L'^': case L'_':
293 case L'a': case L'b': case L'c': case L'd': case L'e':
294 case L'f': case L'g': case L'h': case L'i': case L'j':
295 case L'k': case L'l': case L'm': case L'n': case L'o':
296 case L'p': case L'q': case L'r': case L's': case L't':
297 case L'u': case L'v': case L'w': case L'x': case L'y':
298 case L'z': case L'{': case L'|': case L'}': case L'~':
299 break;
300 default:
301 return (wctype_t) 0;
303 # endif
305 /* Avoid overrunning the buffer. */
306 if (cp == s + CHAR_CLASS_MAX_LENGTH)
307 return (wctype_t) 0;
309 *cp++ = (char) *wcs++;
311 while (*wcs != L'\0');
313 *cp = '\0';
315 # ifdef _LIBC
316 return __wctype (s);
317 # else
318 return wctype (s);
319 # endif
321 # define IS_CHAR_CLASS(string) is_char_class (string)
323 # include "fnmatch_loop.c"
324 # endif
328 fnmatch (pattern, string, flags)
329 const char *pattern;
330 const char *string;
331 int flags;
333 # if HANDLE_MULTIBYTE
334 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
336 mbstate_t ps;
337 size_t n;
338 const char *p;
339 wchar_t *wpattern_malloc = NULL;
340 wchar_t *wpattern;
341 wchar_t *wstring_malloc = NULL;
342 wchar_t *wstring;
343 size_t alloca_used = 0;
345 /* Convert the strings into wide characters. */
346 memset (&ps, '\0', sizeof (ps));
347 p = pattern;
348 #ifdef _LIBC
349 n = strnlen (pattern, 1024);
350 #else
351 n = strlen (pattern);
352 #endif
353 if (__builtin_expect (n < 1024, 1))
355 wpattern = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
356 alloca_used);
357 n = mbsrtowcs (wpattern, &p, n + 1, &ps);
358 if (__builtin_expect (n == (size_t) -1, 0))
359 /* Something wrong.
360 XXX Do we have to set `errno' to something which mbsrtows hasn't
361 already done? */
362 return -1;
363 if (p)
365 memset (&ps, '\0', sizeof (ps));
366 goto prepare_wpattern;
369 else
371 prepare_wpattern:
372 n = mbsrtowcs (NULL, &pattern, 0, &ps);
373 if (__builtin_expect (n == (size_t) -1, 0))
374 /* Something wrong.
375 XXX Do we have to set `errno' to something which mbsrtows hasn't
376 already done? */
377 return -1;
378 if (__builtin_expect (n >= (size_t) -1 / sizeof (wchar_t), 0))
380 __set_errno (ENOMEM);
381 return -2;
383 wpattern_malloc = wpattern
384 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
385 assert (mbsinit (&ps));
386 if (wpattern == NULL)
387 return -2;
388 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
391 assert (mbsinit (&ps));
392 #ifdef _LIBC
393 n = strnlen (string, 1024);
394 #else
395 n = strlen (string);
396 #endif
397 p = string;
398 if (__builtin_expect (n < 1024, 1))
400 wstring = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
401 alloca_used);
402 n = mbsrtowcs (wstring, &p, n + 1, &ps);
403 if (__builtin_expect (n == (size_t) -1, 0))
405 /* Something wrong.
406 XXX Do we have to set `errno' to something which
407 mbsrtows hasn't already done? */
408 free_return:
409 free (wpattern_malloc);
410 return -1;
412 if (p)
414 memset (&ps, '\0', sizeof (ps));
415 goto prepare_wstring;
418 else
420 prepare_wstring:
421 n = mbsrtowcs (NULL, &string, 0, &ps);
422 if (__builtin_expect (n == (size_t) -1, 0))
423 /* Something wrong.
424 XXX Do we have to set `errno' to something which mbsrtows hasn't
425 already done? */
426 goto free_return;
427 if (__builtin_expect (n >= (size_t) -1 / sizeof (wchar_t), 0))
429 free (wpattern_malloc);
430 __set_errno (ENOMEM);
431 return -2;
434 wstring_malloc = wstring
435 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
436 if (wstring == NULL)
438 free (wpattern_malloc);
439 return -2;
441 assert (mbsinit (&ps));
442 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
445 int res = internal_fnwmatch (wpattern, wstring, wstring + n,
446 flags & FNM_PERIOD, flags, NULL,
447 alloca_used);
449 free (wstring_malloc);
450 free (wpattern_malloc);
452 return res;
454 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
456 return internal_fnmatch (pattern, string, string + strlen (string),
457 flags & FNM_PERIOD, flags, NULL, 0);
460 # ifdef _LIBC
461 # undef fnmatch
462 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
463 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
464 strong_alias (__fnmatch, __fnmatch_old)
465 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
466 # endif
467 libc_hidden_ver (__fnmatch, fnmatch)
468 # endif
470 #endif /* _LIBC or not __GNU_LIBRARY__. */