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