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