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