NEWS.stable: log cherry-pick [24bfc89]->[1d71dc7] strtoumax: Avoid link error on...
[gnulib.git] / lib / fnmatch.c
1 /* Copyright (C) 1991-1993, 1996-2007, 2009-2011 Free Software Foundation, Inc.
2
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)
6    any later version.
7
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.
12
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.  */
16
17 #ifndef _LIBC
18 # include <config.h>
19 #endif
20
21 /* Enable GNU extensions in fnmatch.h.  */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE    1
24 #endif
25
26 #if ! defined __builtin_expect && __GNUC__ < 3
27 # define __builtin_expect(expr, expected) (expr)
28 #endif
29
30 #include <fnmatch.h>
31
32 #include <alloca.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stddef.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #define WIDE_CHAR_SUPPORT \
42   (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
43    && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
44
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
48 # include <wctype.h>
49 # include <wchar.h>
50 #endif
51
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.  */
55 #ifdef _LIBC
56 # include "../locale/localeinfo.h"
57 # include "../locale/elem-hash.h"
58 # include "../locale/coll-lookup.h"
59 # include <shlib-compat.h>
60
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);
65 #endif
66
67 #ifndef SIZE_MAX
68 # define SIZE_MAX ((size_t) -1)
69 #endif
70
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))
74
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.  */
83
84 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
85
86
87 # if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
88 #  define isblank(c) ((c) == ' ' || (c) == '\t')
89 # endif
90
91 # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
92
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
98 #  else
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
102 #  endif
103
104 #  ifdef _LIBC
105 #   define IS_CHAR_CLASS(string) __wctype (string)
106 #  else
107 #   define IS_CHAR_CLASS(string) wctype (string)
108 #  endif
109
110 #  ifdef _LIBC
111 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
112 #  else
113 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
114 #  endif
115
116 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
117 /* In this case we are implementing the multibyte character handling.  */
118 #   define HANDLE_MULTIBYTE     1
119 #  endif
120
121 # else
122 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
123
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"))
131 # endif
132
133 /* Avoid depending on library functions or files
134    whose names are inconsistent.  */
135
136 /* Global variable.  */
137 static int posixly_correct;
138
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
143 # endif
144
145 /* Note that this evaluates C many times.  */
146 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
147 # define CHAR   char
148 # define UCHAR  unsigned char
149 # define INT    int
150 # define FCT    internal_fnmatch
151 # define EXT    ext_match
152 # define END    end_pattern
153 # define L_(CS) CS
154 # ifdef _LIBC
155 #  define BTOWC(C)      __btowc (C)
156 # else
157 #  define BTOWC(C)      btowc (C)
158 # endif
159 # define STRLEN(S) strlen (S)
160 # define STRCAT(D, S) strcat (D, S)
161 # ifdef _LIBC
162 #  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
163 # else
164 #  if HAVE_MEMPCPY
165 #   define MEMPCPY(D, S, N) mempcpy (D, S, N)
166 #  else
167 #   define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
168 #  endif
169 # endif
170 # define MEMCHR(S, C, N) memchr (S, C, N)
171 # define STRCOLL(S1, S2) strcoll (S1, S2)
172 # include "fnmatch_loop.c"
173
174
175 # if HANDLE_MULTIBYTE
176 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
177 #  define CHAR  wchar_t
178 #  define UCHAR wint_t
179 #  define INT   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)
185 #  ifdef _LIBC
186 #   define STRLEN(S) __wcslen (S)
187 #   define STRCAT(D, S) __wcscat (D, S)
188 #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
189 #  else
190 #   define STRLEN(S) wcslen (S)
191 #   define STRCAT(D, S) wcscat (D, S)
192 #   if HAVE_WMEMPCPY
193 #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
194 #   else
195 #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
196 #   endif
197 #  endif
198 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
199 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
200 #  define WIDE_CHAR_VERSION 1
201
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.  */
209 static wctype_t
210 is_char_class (const wchar_t *wcs)
211 {
212   char s[CHAR_CLASS_MAX_LENGTH + 1];
213   char *cp = s;
214
215   do
216     {
217       /* Test for a printable character from the portable character set.  */
218 #  ifdef _LIBC
219       if (*wcs < 0x20 || *wcs > 0x7e
220           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
221         return (wctype_t) 0;
222 #  else
223       switch (*wcs)
224         {
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'>':
231         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':
237         case L'Z':
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'~':
245           break;
246         default:
247           return (wctype_t) 0;
248         }
249 #  endif
250
251       /* Avoid overrunning the buffer.  */
252       if (cp == s + CHAR_CLASS_MAX_LENGTH)
253         return (wctype_t) 0;
254
255       *cp++ = (char) *wcs++;
256     }
257   while (*wcs != L'\0');
258
259   *cp = '\0';
260
261 #  ifdef _LIBC
262   return __wctype (s);
263 #  else
264   return wctype (s);
265 #  endif
266 }
267 #  define IS_CHAR_CLASS(string) is_char_class (string)
268
269 #  include "fnmatch_loop.c"
270 # endif
271
272
273 int
274 fnmatch (const char *pattern, const char *string, int flags)
275 {
276 # if HANDLE_MULTIBYTE
277 #  define ALLOCA_LIMIT 2000
278   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
279     {
280       mbstate_t ps;
281       size_t patsize;
282       size_t strsize;
283       size_t totsize;
284       wchar_t *wpattern;
285       wchar_t *wstring;
286       int res;
287
288       /* Calculate the size needed to convert the strings to
289          wide characters.  */
290       memset (&ps, '\0', sizeof (ps));
291       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
292       if (__builtin_expect (patsize != 0, 1))
293         {
294           assert (mbsinit (&ps));
295           strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
296           if (__builtin_expect (strsize != 0, 1))
297             {
298               assert (mbsinit (&ps));
299               totsize = patsize + strsize;
300               if (__builtin_expect (! (patsize <= totsize
301                                        && totsize <= SIZE_MAX / sizeof (wchar_t)),
302                                     0))
303                 {
304                   errno = ENOMEM;
305                   return -1;
306                 }
307
308               /* Allocate room for the wide characters.  */
309               if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
310                 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
311               else
312                 {
313                   wpattern = malloc (totsize * sizeof (wchar_t));
314                   if (__builtin_expect (! wpattern, 0))
315                     {
316                       errno = ENOMEM;
317                       return -1;
318                     }
319                 }
320               wstring = wpattern + patsize;
321
322               /* Convert the strings into wide characters.  */
323               mbsrtowcs (wpattern, &pattern, patsize, &ps);
324               assert (mbsinit (&ps));
325               mbsrtowcs (wstring, &string, strsize, &ps);
326
327               res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
328                                        flags & FNM_PERIOD, flags);
329
330               if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
331                 free (wpattern);
332               return res;
333             }
334         }
335     }
336
337 # endif /* HANDLE_MULTIBYTE */
338
339   return internal_fnmatch (pattern, string, string + strlen (string),
340                            flags & FNM_PERIOD, flags);
341 }
342
343 # ifdef _LIBC
344 #  undef fnmatch
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);
349 #  endif
350 libc_hidden_ver (__fnmatch, fnmatch)
351 # endif
352
353 #endif  /* _LIBC or not __GNU_LIBRARY__.  */