alloca cleanup: always include <alloca.h>.
[gnulib.git] / lib / fnmatch.c
1 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2004
2         Free Software Foundation, Inc.
3
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 2, or (at your option)
7    any later version.
8
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.
13
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 /* Enable GNU extensions in fnmatch.h.  */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE    1
25 #endif
26
27 #if ! defined __builtin_expect && __GNUC__ < 3
28 # define __builtin_expect(expr, expected) (expr)
29 #endif
30
31 #include <fnmatch.h>
32
33 #include <alloca.h>
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
42
43 /* For platform which support the ISO C amendement 1 functionality we
44    support user defined character classes.  */
45 #if defined _LIBC || WIDE_CHAR_SUPPORT
46 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
47 # include <wchar.h>
48 # include <wctype.h>
49 #endif
50
51 /* We need some of the locale data (the collation sequence information)
52    but there is no interface to get this information in general.  Therefore
53    we support a correct implementation only in glibc.  */
54 #ifdef _LIBC
55 # include "../locale/localeinfo.h"
56 # include "../locale/elem-hash.h"
57 # include "../locale/coll-lookup.h"
58 # include <shlib-compat.h>
59
60 # define CONCAT(a,b) __CONCAT(a,b)
61 # define mbsrtowcs __mbsrtowcs
62 # define fnmatch __fnmatch
63 extern int fnmatch (const char *pattern, const char *string, int flags);
64 #endif
65
66 #ifndef SIZE_MAX
67 # define SIZE_MAX ((size_t) -1)
68 #endif
69
70 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
71 #define NO_LEADING_PERIOD(flags) \
72   ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
73
74 /* Comment out all this code if we are using the GNU C Library, and are not
75    actually compiling the library itself, and have not detected a bug
76    in the library.  This code is part of the GNU C
77    Library, but also included in many other GNU distributions.  Compiling
78    and linking in this code is a waste when using the GNU C library
79    (especially if it is a shared library).  Rather than having every GNU
80    program understand `configure --with-gnu-libc' and omit the object files,
81    it is simpler to just do this in the source for each such file.  */
82
83 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
84
85
86 # if defined STDC_HEADERS || !defined isascii
87 #  define ISASCII(c) 1
88 # else
89 #  define ISASCII(c) isascii(c)
90 # endif
91
92 # ifdef isblank
93 #  define ISBLANK(c) (ISASCII (c) && isblank (c))
94 # else
95 #  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
96 # endif
97 # ifdef isgraph
98 #  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
99 # else
100 #  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
101 # endif
102
103 # define ISPRINT(c) (ISASCII (c) && isprint (c))
104 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
105 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
106 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
107 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
108 # define ISLOWER(c) (ISASCII (c) && islower (c))
109 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
110 # define ISSPACE(c) (ISASCII (c) && isspace (c))
111 # define ISUPPER(c) (ISASCII (c) && isupper (c))
112 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
113
114 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
115
116 # if defined _LIBC || WIDE_CHAR_SUPPORT
117 /* The GNU C library provides support for user-defined character classes
118    and the functions from ISO C amendement 1.  */
119 #  ifdef CHARCLASS_NAME_MAX
120 #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
121 #  else
122 /* This shouldn't happen but some implementation might still have this
123    problem.  Use a reasonable default value.  */
124 #   define CHAR_CLASS_MAX_LENGTH 256
125 #  endif
126
127 #  ifdef _LIBC
128 #   define IS_CHAR_CLASS(string) __wctype (string)
129 #  else
130 #   define IS_CHAR_CLASS(string) wctype (string)
131 #  endif
132
133 #  ifdef _LIBC
134 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
135 #  else
136 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
137 #  endif
138
139 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
140 /* In this case we are implementing the multibyte character handling.  */
141 #   define HANDLE_MULTIBYTE     1
142 #  endif
143
144 # else
145 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
146
147 #  define IS_CHAR_CLASS(string)                                               \
148    (STREQ (string, "alpha") || STREQ (string, "upper")                        \
149     || STREQ (string, "lower") || STREQ (string, "digit")                     \
150     || STREQ (string, "alnum") || STREQ (string, "xdigit")                    \
151     || STREQ (string, "space") || STREQ (string, "print")                     \
152     || STREQ (string, "punct") || STREQ (string, "graph")                     \
153     || STREQ (string, "cntrl") || STREQ (string, "blank"))
154 # endif
155
156 /* Avoid depending on library functions or files
157    whose names are inconsistent.  */
158
159 # ifndef errno
160 extern int errno;
161 # endif
162
163 /* Global variable.  */
164 static int posixly_correct;
165
166 # ifndef internal_function
167 /* Inside GNU libc we mark some function in a special way.  In other
168    environments simply ignore the marking.  */
169 #  define internal_function
170 # endif
171
172 /* Note that this evaluates C many times.  */
173 # ifdef _LIBC
174 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
175 # else
176 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
177 # endif
178 # define CHAR   char
179 # define UCHAR  unsigned char
180 # define INT    int
181 # define FCT    internal_fnmatch
182 # define EXT    ext_match
183 # define END    end_pattern
184 # define L(CS)  CS
185 # ifdef _LIBC
186 #  define BTOWC(C)      __btowc (C)
187 # else
188 #  define BTOWC(C)      btowc (C)
189 # endif
190 # define STRLEN(S) strlen (S)
191 # define STRCAT(D, S) strcat (D, S)
192 # ifdef _LIBC
193 #  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
194 # else
195 #  if HAVE_MEMPCPY
196 #   define MEMPCPY(D, S, N) mempcpy (D, S, N)
197 #  else
198 #   define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
199 #  endif
200 # endif
201 # define MEMCHR(S, C, N) memchr (S, C, N)
202 # define STRCOLL(S1, S2) strcoll (S1, S2)
203 # include "fnmatch_loop.c"
204
205
206 # if HANDLE_MULTIBYTE
207 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
208 #  define CHAR  wchar_t
209 #  define UCHAR wint_t
210 #  define INT   wint_t
211 #  define FCT   internal_fnwmatch
212 #  define EXT   ext_wmatch
213 #  define END   end_wpattern
214 #  define L(CS) L##CS
215 #  define BTOWC(C)      (C)
216 #  ifdef _LIBC
217 #   define STRLEN(S) __wcslen (S)
218 #   define STRCAT(D, S) __wcscat (D, S)
219 #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
220 #  else
221 #   define STRLEN(S) wcslen (S)
222 #   define STRCAT(D, S) wcscat (D, S)
223 #   if HAVE_WMEMPCPY
224 #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
225 #   else
226 #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
227 #   endif
228 #  endif
229 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
230 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
231 #  define WIDE_CHAR_VERSION 1
232
233 #  undef IS_CHAR_CLASS
234 /* We have to convert the wide character string in a multibyte string.  But
235    we know that the character class names consist of alphanumeric characters
236    from the portable character set, and since the wide character encoding
237    for a member of the portable character set is the same code point as
238    its single-byte encoding, we can use a simplified method to convert the
239    string to a multibyte character string.  */
240 static wctype_t
241 is_char_class (const wchar_t *wcs)
242 {
243   char s[CHAR_CLASS_MAX_LENGTH + 1];
244   char *cp = s;
245
246   do
247     {
248       /* Test for a printable character from the portable character set.  */
249 #  ifdef _LIBC
250       if (*wcs < 0x20 || *wcs > 0x7e
251           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
252         return (wctype_t) 0;
253 #  else
254       switch (*wcs)
255         {
256         case L' ': case L'!': case L'"': case L'#': case L'%':
257         case L'&': case L'\'': case L'(': case L')': case L'*':
258         case L'+': case L',': case L'-': case L'.': case L'/':
259         case L'0': case L'1': case L'2': case L'3': case L'4':
260         case L'5': case L'6': case L'7': case L'8': case L'9':
261         case L':': case L';': case L'<': case L'=': case L'>':
262         case L'?':
263         case L'A': case L'B': case L'C': case L'D': case L'E':
264         case L'F': case L'G': case L'H': case L'I': case L'J':
265         case L'K': case L'L': case L'M': case L'N': case L'O':
266         case L'P': case L'Q': case L'R': case L'S': case L'T':
267         case L'U': case L'V': case L'W': case L'X': case L'Y':
268         case L'Z':
269         case L'[': case L'\\': case L']': case L'^': case L'_':
270         case L'a': case L'b': case L'c': case L'd': case L'e':
271         case L'f': case L'g': case L'h': case L'i': case L'j':
272         case L'k': case L'l': case L'm': case L'n': case L'o':
273         case L'p': case L'q': case L'r': case L's': case L't':
274         case L'u': case L'v': case L'w': case L'x': case L'y':
275         case L'z': case L'{': case L'|': case L'}': case L'~':
276           break;
277         default:
278           return (wctype_t) 0;
279         }
280 #  endif
281
282       /* Avoid overrunning the buffer.  */
283       if (cp == s + CHAR_CLASS_MAX_LENGTH)
284         return (wctype_t) 0;
285
286       *cp++ = (char) *wcs++;
287     }
288   while (*wcs != L'\0');
289
290   *cp = '\0';
291
292 #  ifdef _LIBC
293   return __wctype (s);
294 #  else
295   return wctype (s);
296 #  endif
297 }
298 #  define IS_CHAR_CLASS(string) is_char_class (string)
299
300 #  include "fnmatch_loop.c"
301 # endif
302
303
304 int
305 fnmatch (const char *pattern, const char *string, int flags)
306 {
307 # if HANDLE_MULTIBYTE
308 #  define ALLOCA_LIMIT 2000
309   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
310     {
311       mbstate_t ps;
312       size_t patsize;
313       size_t strsize;
314       size_t totsize;
315       wchar_t *wpattern;
316       wchar_t *wstring;
317       int res;
318
319       /* Calculate the size needed to convert the strings to
320          wide characters.  */
321       memset (&ps, '\0', sizeof (ps));
322       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
323       if (__builtin_expect (patsize == 0, 0))
324         /* Something wrong.
325            XXX Do we have to set `errno' to something which mbsrtows hasn't
326            already done?  */
327         return -1;
328       assert (mbsinit (&ps));
329       strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
330       if (__builtin_expect (strsize == 0, 0))
331         /* Something wrong.
332            XXX Do we have to set `errno' to something which mbsrtows hasn't
333            already done?  */
334         return -1;
335       assert (mbsinit (&ps));
336       totsize = patsize + strsize;
337       if (__builtin_expect (! (patsize <= totsize
338                                && totsize <= SIZE_MAX / sizeof (wchar_t)),
339                             0))
340         {
341           errno = ENOMEM;
342           return -1;
343         }
344
345       /* Allocate room for the wide characters.  */
346       if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
347         wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
348       else
349         {
350           wpattern = malloc (totsize * sizeof (wchar_t));
351           if (__builtin_expect (! wpattern, 0))
352             {
353               errno = ENOMEM;
354               return -1;
355             }
356         }
357       wstring = wpattern + patsize;
358
359       /* Convert the strings into wide characters.  */
360       mbsrtowcs (wpattern, &pattern, patsize, &ps);
361       assert (mbsinit (&ps));
362       mbsrtowcs (wstring, &string, strsize, &ps);
363
364       res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
365                                flags & FNM_PERIOD, flags);
366
367       if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
368         free (wpattern);
369       return res;
370     }
371 # endif /* HANDLE_MULTIBYTE */
372
373   return internal_fnmatch (pattern, string, string + strlen (string),
374                            flags & FNM_PERIOD, flags);
375 }
376
377 # ifdef _LIBC
378 #  undef fnmatch
379 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
380 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
381 strong_alias (__fnmatch, __fnmatch_old)
382 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
383 #  endif
384 libc_hidden_ver (__fnmatch, fnmatch)
385 # endif
386
387 #endif  /* _LIBC or not __GNU_LIBRARY__.  */