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