obstack.m4 (gl_PREREQ_OBSTACK): Require
[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 <stdbool.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
43
44 /* For platform which support the ISO C amendement 1 functionality we
45    support user defined character classes.  */
46 #if defined _LIBC || WIDE_CHAR_SUPPORT
47 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
48 # include <wchar.h>
49 # include <wctype.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 STDC_HEADERS || !defined isascii
88 #  define ISASCII(c) 1
89 # else
90 #  define ISASCII(c) isascii(c)
91 # endif
92
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
103
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))
114
115 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
116
117 # if defined _LIBC || WIDE_CHAR_SUPPORT
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
127
128 #  ifdef _LIBC
129 #   define IS_CHAR_CLASS(string) __wctype (string)
130 #  else
131 #   define IS_CHAR_CLASS(string) wctype (string)
132 #  endif
133
134 #  ifdef _LIBC
135 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
136 #  else
137 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
138 #  endif
139
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
144
145 # else
146 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
147
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
156
157 /* Avoid depending on library functions or files
158    whose names are inconsistent.  */
159
160 /* Global variable.  */
161 static int posixly_correct;
162
163 # ifndef internal_function
164 /* Inside GNU libc we mark some function in a special way.  In other
165    environments simply ignore the marking.  */
166 #  define internal_function
167 # endif
168
169 /* Note that this evaluates C many times.  */
170 # ifdef _LIBC
171 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
172 # else
173 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
174 # endif
175 # define CHAR   char
176 # define UCHAR  unsigned char
177 # define INT    int
178 # define FCT    internal_fnmatch
179 # define EXT    ext_match
180 # define END    end_pattern
181 # define L(CS)  CS
182 # ifdef _LIBC
183 #  define BTOWC(C)      __btowc (C)
184 # else
185 #  define BTOWC(C)      btowc (C)
186 # endif
187 # define STRLEN(S) strlen (S)
188 # define STRCAT(D, S) strcat (D, S)
189 # ifdef _LIBC
190 #  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
191 # else
192 #  if HAVE_MEMPCPY
193 #   define MEMPCPY(D, S, N) mempcpy (D, S, N)
194 #  else
195 #   define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
196 #  endif
197 # endif
198 # define MEMCHR(S, C, N) memchr (S, C, N)
199 # define STRCOLL(S1, S2) strcoll (S1, S2)
200 # include "fnmatch_loop.c"
201
202
203 # if HANDLE_MULTIBYTE
204 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
205 #  define CHAR  wchar_t
206 #  define UCHAR wint_t
207 #  define INT   wint_t
208 #  define FCT   internal_fnwmatch
209 #  define EXT   ext_wmatch
210 #  define END   end_wpattern
211 #  define L(CS) L##CS
212 #  define BTOWC(C)      (C)
213 #  ifdef _LIBC
214 #   define STRLEN(S) __wcslen (S)
215 #   define STRCAT(D, S) __wcscat (D, S)
216 #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
217 #  else
218 #   define STRLEN(S) wcslen (S)
219 #   define STRCAT(D, S) wcscat (D, S)
220 #   if HAVE_WMEMPCPY
221 #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
222 #   else
223 #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
224 #   endif
225 #  endif
226 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
227 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
228 #  define WIDE_CHAR_VERSION 1
229
230 #  undef IS_CHAR_CLASS
231 /* We have to convert the wide character string in a multibyte string.  But
232    we know that the character class names consist of alphanumeric characters
233    from the portable character set, and since the wide character encoding
234    for a member of the portable character set is the same code point as
235    its single-byte encoding, we can use a simplified method to convert the
236    string to a multibyte character string.  */
237 static wctype_t
238 is_char_class (const wchar_t *wcs)
239 {
240   char s[CHAR_CLASS_MAX_LENGTH + 1];
241   char *cp = s;
242
243   do
244     {
245       /* Test for a printable character from the portable character set.  */
246 #  ifdef _LIBC
247       if (*wcs < 0x20 || *wcs > 0x7e
248           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
249         return (wctype_t) 0;
250 #  else
251       switch (*wcs)
252         {
253         case L' ': case L'!': case L'"': case L'#': case L'%':
254         case L'&': case L'\'': case L'(': case L')': case L'*':
255         case L'+': case L',': case L'-': case L'.': case L'/':
256         case L'0': case L'1': case L'2': case L'3': case L'4':
257         case L'5': case L'6': case L'7': case L'8': case L'9':
258         case L':': case L';': case L'<': case L'=': case L'>':
259         case L'?':
260         case L'A': case L'B': case L'C': case L'D': case L'E':
261         case L'F': case L'G': case L'H': case L'I': case L'J':
262         case L'K': case L'L': case L'M': case L'N': case L'O':
263         case L'P': case L'Q': case L'R': case L'S': case L'T':
264         case L'U': case L'V': case L'W': case L'X': case L'Y':
265         case L'Z':
266         case L'[': case L'\\': case L']': case L'^': case L'_':
267         case L'a': case L'b': case L'c': case L'd': case L'e':
268         case L'f': case L'g': case L'h': case L'i': case L'j':
269         case L'k': case L'l': case L'm': case L'n': case L'o':
270         case L'p': case L'q': case L'r': case L's': case L't':
271         case L'u': case L'v': case L'w': case L'x': case L'y':
272         case L'z': case L'{': case L'|': case L'}': case L'~':
273           break;
274         default:
275           return (wctype_t) 0;
276         }
277 #  endif
278
279       /* Avoid overrunning the buffer.  */
280       if (cp == s + CHAR_CLASS_MAX_LENGTH)
281         return (wctype_t) 0;
282
283       *cp++ = (char) *wcs++;
284     }
285   while (*wcs != L'\0');
286
287   *cp = '\0';
288
289 #  ifdef _LIBC
290   return __wctype (s);
291 #  else
292   return wctype (s);
293 #  endif
294 }
295 #  define IS_CHAR_CLASS(string) is_char_class (string)
296
297 #  include "fnmatch_loop.c"
298 # endif
299
300
301 int
302 fnmatch (const char *pattern, const char *string, int flags)
303 {
304 # if HANDLE_MULTIBYTE
305 #  define ALLOCA_LIMIT 2000
306   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
307     {
308       mbstate_t ps;
309       size_t patsize;
310       size_t strsize;
311       size_t totsize;
312       wchar_t *wpattern;
313       wchar_t *wstring;
314       int res;
315
316       /* Calculate the size needed to convert the strings to
317          wide characters.  */
318       memset (&ps, '\0', sizeof (ps));
319       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
320       if (__builtin_expect (patsize == 0, 0))
321         /* Something wrong.
322            XXX Do we have to set `errno' to something which mbsrtows hasn't
323            already done?  */
324         return -1;
325       assert (mbsinit (&ps));
326       strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
327       if (__builtin_expect (strsize == 0, 0))
328         /* Something wrong.
329            XXX Do we have to set `errno' to something which mbsrtows hasn't
330            already done?  */
331         return -1;
332       assert (mbsinit (&ps));
333       totsize = patsize + strsize;
334       if (__builtin_expect (! (patsize <= totsize
335                                && totsize <= SIZE_MAX / sizeof (wchar_t)),
336                             0))
337         {
338           errno = ENOMEM;
339           return -1;
340         }
341
342       /* Allocate room for the wide characters.  */
343       if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
344         wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
345       else
346         {
347           wpattern = malloc (totsize * sizeof (wchar_t));
348           if (__builtin_expect (! wpattern, 0))
349             {
350               errno = ENOMEM;
351               return -1;
352             }
353         }
354       wstring = wpattern + patsize;
355
356       /* Convert the strings into wide characters.  */
357       mbsrtowcs (wpattern, &pattern, patsize, &ps);
358       assert (mbsinit (&ps));
359       mbsrtowcs (wstring, &string, strsize, &ps);
360
361       res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
362                                flags & FNM_PERIOD, flags);
363
364       if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
365         free (wpattern);
366       return res;
367     }
368 # endif /* HANDLE_MULTIBYTE */
369
370   return internal_fnmatch (pattern, string, string + strlen (string),
371                            flags & FNM_PERIOD, flags);
372 }
373
374 # ifdef _LIBC
375 #  undef fnmatch
376 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
377 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
378 strong_alias (__fnmatch, __fnmatch_old)
379 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
380 #  endif
381 libc_hidden_ver (__fnmatch, fnmatch)
382 # endif
383
384 #endif  /* _LIBC or not __GNU_LIBRARY__.  */