Remove unused macros from !_LIBC code in glibc-borrowed files.
[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 # include "fnmatch_loop.c"
172
173
174 # if HANDLE_MULTIBYTE
175 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
176 #  define CHAR  wchar_t
177 #  define UCHAR wint_t
178 #  define INT   wint_t
179 #  define FCT   internal_fnwmatch
180 #  define EXT   ext_wmatch
181 #  define END   end_wpattern
182 #  define L_(CS)        L##CS
183 #  define BTOWC(C)      (C)
184 #  ifdef _LIBC
185 #   define STRLEN(S) __wcslen (S)
186 #   define STRCAT(D, S) __wcscat (D, S)
187 #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
188 #  else
189 #   define STRLEN(S) wcslen (S)
190 #   define STRCAT(D, S) wcscat (D, S)
191 #   if HAVE_WMEMPCPY
192 #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
193 #   else
194 #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
195 #   endif
196 #  endif
197 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
198 #  define WIDE_CHAR_VERSION 1
199
200 #  undef IS_CHAR_CLASS
201 /* We have to convert the wide character string in a multibyte string.  But
202    we know that the character class names consist of alphanumeric characters
203    from the portable character set, and since the wide character encoding
204    for a member of the portable character set is the same code point as
205    its single-byte encoding, we can use a simplified method to convert the
206    string to a multibyte character string.  */
207 static wctype_t
208 is_char_class (const wchar_t *wcs)
209 {
210   char s[CHAR_CLASS_MAX_LENGTH + 1];
211   char *cp = s;
212
213   do
214     {
215       /* Test for a printable character from the portable character set.  */
216 #  ifdef _LIBC
217       if (*wcs < 0x20 || *wcs > 0x7e
218           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
219         return (wctype_t) 0;
220 #  else
221       switch (*wcs)
222         {
223         case L' ': case L'!': case L'"': case L'#': case L'%':
224         case L'&': case L'\'': case L'(': case L')': case L'*':
225         case L'+': case L',': case L'-': case L'.': case L'/':
226         case L'0': case L'1': case L'2': case L'3': case L'4':
227         case L'5': case L'6': case L'7': case L'8': case L'9':
228         case L':': case L';': case L'<': case L'=': case L'>':
229         case L'?':
230         case L'A': case L'B': case L'C': case L'D': case L'E':
231         case L'F': case L'G': case L'H': case L'I': case L'J':
232         case L'K': case L'L': case L'M': case L'N': case L'O':
233         case L'P': case L'Q': case L'R': case L'S': case L'T':
234         case L'U': case L'V': case L'W': case L'X': case L'Y':
235         case L'Z':
236         case L'[': case L'\\': case L']': case L'^': case L'_':
237         case L'a': case L'b': case L'c': case L'd': case L'e':
238         case L'f': case L'g': case L'h': case L'i': case L'j':
239         case L'k': case L'l': case L'm': case L'n': case L'o':
240         case L'p': case L'q': case L'r': case L's': case L't':
241         case L'u': case L'v': case L'w': case L'x': case L'y':
242         case L'z': case L'{': case L'|': case L'}': case L'~':
243           break;
244         default:
245           return (wctype_t) 0;
246         }
247 #  endif
248
249       /* Avoid overrunning the buffer.  */
250       if (cp == s + CHAR_CLASS_MAX_LENGTH)
251         return (wctype_t) 0;
252
253       *cp++ = (char) *wcs++;
254     }
255   while (*wcs != L'\0');
256
257   *cp = '\0';
258
259 #  ifdef _LIBC
260   return __wctype (s);
261 #  else
262   return wctype (s);
263 #  endif
264 }
265 #  define IS_CHAR_CLASS(string) is_char_class (string)
266
267 #  include "fnmatch_loop.c"
268 # endif
269
270
271 int
272 fnmatch (const char *pattern, const char *string, int flags)
273 {
274 # if HANDLE_MULTIBYTE
275 #  define ALLOCA_LIMIT 2000
276   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
277     {
278       mbstate_t ps;
279       size_t patsize;
280       size_t strsize;
281       size_t totsize;
282       wchar_t *wpattern;
283       wchar_t *wstring;
284       int res;
285
286       /* Calculate the size needed to convert the strings to
287          wide characters.  */
288       memset (&ps, '\0', sizeof (ps));
289       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
290       if (__builtin_expect (patsize != 0, 1))
291         {
292           assert (mbsinit (&ps));
293           strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
294           if (__builtin_expect (strsize != 0, 1))
295             {
296               assert (mbsinit (&ps));
297               totsize = patsize + strsize;
298               if (__builtin_expect (! (patsize <= totsize
299                                        && totsize <= SIZE_MAX / sizeof (wchar_t)),
300                                     0))
301                 {
302                   errno = ENOMEM;
303                   return -1;
304                 }
305
306               /* Allocate room for the wide characters.  */
307               if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
308                 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
309               else
310                 {
311                   wpattern = malloc (totsize * sizeof (wchar_t));
312                   if (__builtin_expect (! wpattern, 0))
313                     {
314                       errno = ENOMEM;
315                       return -1;
316                     }
317                 }
318               wstring = wpattern + patsize;
319
320               /* Convert the strings into wide characters.  */
321               mbsrtowcs (wpattern, &pattern, patsize, &ps);
322               assert (mbsinit (&ps));
323               mbsrtowcs (wstring, &string, strsize, &ps);
324
325               res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
326                                        flags & FNM_PERIOD, flags);
327
328               if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
329                 free (wpattern);
330               return res;
331             }
332         }
333     }
334
335 # endif /* HANDLE_MULTIBYTE */
336
337   return internal_fnmatch (pattern, string, string + strlen (string),
338                            flags & FNM_PERIOD, flags);
339 }
340
341 # ifdef _LIBC
342 #  undef fnmatch
343 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
344 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
345 strong_alias (__fnmatch, __fnmatch_old)
346 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
347 #  endif
348 libc_hidden_ver (__fnmatch, fnmatch)
349 # endif
350
351 #endif  /* _LIBC or not __GNU_LIBRARY__.  */