* nscd/cache.c (cache_search): Give first arg type `request_type'.
[platform/upstream/glibc.git] / posix / fnmatch.c
1 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003
2         Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 /* Enable GNU extensions in fnmatch.h.  */
25 #ifndef _GNU_SOURCE
26 # define _GNU_SOURCE    1
27 #endif
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fnmatch.h>
32 #include <ctype.h>
33
34 #if HAVE_STRING_H || defined _LIBC
35 # include <string.h>
36 #else
37 # include <strings.h>
38 #endif
39
40 #if defined STDC_HEADERS || defined _LIBC
41 # include <stdlib.h>
42 #endif
43
44 /* For platform which support the ISO C amendement 1 functionality we
45    support user defined character classes.  */
46 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
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 mbsinit __mbsinit
63 # define mbsrtowcs __mbsrtowcs
64 # define fnmatch __fnmatch
65 extern int fnmatch (const char *pattern, const char *string, int flags);
66 #endif
67
68 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
69 #define NO_LEADING_PERIOD(flags) \
70   ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
71
72 /* Comment out all this code if we are using the GNU C Library, and are not
73    actually compiling the library itself.  This code is part of the GNU C
74    Library, but also included in many other GNU distributions.  Compiling
75    and linking in this code is a waste when using the GNU C library
76    (especially if it is a shared library).  Rather than having every GNU
77    program understand `configure --with-gnu-libc' and omit the object files,
78    it is simpler to just do this in the source for each such file.  */
79
80 #if defined _LIBC || !defined __GNU_LIBRARY__
81
82
83 # if defined STDC_HEADERS || !defined isascii
84 #  define ISASCII(c) 1
85 # else
86 #  define ISASCII(c) isascii(c)
87 # endif
88
89 # ifdef isblank
90 #  define ISBLANK(c) (ISASCII (c) && isblank (c))
91 # else
92 #  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
93 # endif
94 # ifdef isgraph
95 #  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
96 # else
97 #  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
98 # endif
99
100 # define ISPRINT(c) (ISASCII (c) && isprint (c))
101 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
102 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
103 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
104 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
105 # define ISLOWER(c) (ISASCII (c) && islower (c))
106 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
107 # define ISSPACE(c) (ISASCII (c) && isspace (c))
108 # define ISUPPER(c) (ISASCII (c) && isupper (c))
109 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
110
111 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
112
113 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
114 /* The GNU C library provides support for user-defined character classes
115    and the functions from ISO C amendement 1.  */
116 #  ifdef CHARCLASS_NAME_MAX
117 #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
118 #  else
119 /* This shouldn't happen but some implementation might still have this
120    problem.  Use a reasonable default value.  */
121 #   define CHAR_CLASS_MAX_LENGTH 256
122 #  endif
123
124 #  ifdef _LIBC
125 #   define IS_CHAR_CLASS(string) __wctype (string)
126 #  else
127 #   define IS_CHAR_CLASS(string) wctype (string)
128 #  endif
129
130 #  ifdef _LIBC
131 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
132 #  else
133 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
134 #  endif
135
136 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
137 /* In this case we are implementing the multibyte character handling.  */
138 #   define HANDLE_MULTIBYTE     1
139 #  endif
140
141 # else
142 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
143
144 #  define IS_CHAR_CLASS(string)                                               \
145    (STREQ (string, "alpha") || STREQ (string, "upper")                        \
146     || STREQ (string, "lower") || STREQ (string, "digit")                     \
147     || STREQ (string, "alnum") || STREQ (string, "xdigit")                    \
148     || STREQ (string, "space") || STREQ (string, "print")                     \
149     || STREQ (string, "punct") || STREQ (string, "graph")                     \
150     || STREQ (string, "cntrl") || STREQ (string, "blank"))
151 # endif
152
153 /* Avoid depending on library functions or files
154    whose names are inconsistent.  */
155
156 # if !defined _LIBC && !defined getenv
157 extern char *getenv ();
158 # endif
159
160 # ifndef errno
161 extern int errno;
162 # endif
163
164 /* Global variable.  */
165 static int posixly_correct;
166
167 /* This function doesn't exist on most systems.  */
168
169 # if !defined HAVE___STRCHRNUL && !defined _LIBC
170 static char *
171 __strchrnul (s, c)
172      const char *s;
173      int c;
174 {
175   char *result = strchr (s, c);
176   if (result == NULL)
177     result = strchr (s, '\0');
178   return result;
179 }
180 # endif
181
182 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
183 static wchar_t *
184 __wcschrnul (s, c)
185      const wchar_t *s;
186      wint_t c;
187 {
188   wchar_t *result = wcschr (s, c);
189   if (result == NULL)
190     result = wcschr (s, '\0');
191   return result;
192 }
193 # endif
194
195 # ifndef internal_function
196 /* Inside GNU libc we mark some function in a special way.  In other
197    environments simply ignore the marking.  */
198 #  define internal_function
199 # endif
200
201 /* Note that this evaluates C many times.  */
202 # ifdef _LIBC
203 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
204 # else
205 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
206 # endif
207 # define CHAR   char
208 # define UCHAR  unsigned char
209 # define INT    int
210 # define FCT    internal_fnmatch
211 # define EXT    ext_match
212 # define END    end_pattern
213 # define L(CS)  CS
214 # ifdef _LIBC
215 #  define BTOWC(C)      __btowc (C)
216 # else
217 #  define BTOWC(C)      btowc (C)
218 # endif
219 # define STRLEN(S) strlen (S)
220 # define STRCAT(D, S) strcat (D, S)
221 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
222 # define MEMCHR(S, C, N) memchr (S, C, N)
223 # define STRCOLL(S1, S2) strcoll (S1, S2)
224 # include "fnmatch_loop.c"
225
226
227 # if HANDLE_MULTIBYTE
228 /* Note that this evaluates C many times.  */
229 #  ifdef _LIBC
230 #   define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
231 #  else
232 #   define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
233 #  endif
234 #  define CHAR  wchar_t
235 #  define UCHAR wint_t
236 #  define INT   wint_t
237 #  define FCT   internal_fnwmatch
238 #  define EXT   ext_wmatch
239 # define END    end_wpattern
240 #  define L(CS) L##CS
241 #  define BTOWC(C)      (C)
242 #  define STRLEN(S) __wcslen (S)
243 #  define STRCAT(D, S) __wcscat (D, S)
244 #  define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
245 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
246 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
247 #  define WIDE_CHAR_VERSION 1
248
249 #  undef IS_CHAR_CLASS
250 /* We have to convert the wide character string in a multibyte string.  But
251    we know that the character class names consist of alphanumeric characters
252    from the portable character set, and since the wide character encoding
253    for a member of the portable character set is the same code point as
254    its single-byte encoding, we can use a simplified method to convert the
255    string to a multibyte character string.  */
256 static wctype_t
257 is_char_class (const wchar_t *wcs)
258 {
259   char s[CHAR_CLASS_MAX_LENGTH + 1];
260   char *cp = s;
261
262   do
263     {
264       /* Test for a printable character from the portable character set.  */
265 #  ifdef _LIBC
266       if (*wcs < 0x20 || *wcs > 0x7e
267           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
268         return (wctype_t) 0;
269 #  else
270       switch (*wcs)
271         {
272         case L' ': case L'!': case L'"': case L'#': case L'%':
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'0': case L'1': case L'2': case L'3': case L'4':
276         case L'5': case L'6': case L'7': case L'8': case L'9':
277         case L':': case L';': case L'<': case L'=': case L'>':
278         case L'?':
279         case L'A': case L'B': case L'C': case L'D': case L'E':
280         case L'F': case L'G': case L'H': case L'I': case L'J':
281         case L'K': case L'L': case L'M': case L'N': case L'O':
282         case L'P': case L'Q': case L'R': case L'S': case L'T':
283         case L'U': case L'V': case L'W': case L'X': case L'Y':
284         case L'Z':
285         case L'[': case L'\\': case L']': case L'^': case L'_':
286         case L'a': case L'b': case L'c': case L'd': case L'e':
287         case L'f': case L'g': case L'h': case L'i': case L'j':
288         case L'k': case L'l': case L'm': case L'n': case L'o':
289         case L'p': case L'q': case L'r': case L's': case L't':
290         case L'u': case L'v': case L'w': case L'x': case L'y':
291         case L'z': case L'{': case L'|': case L'}': case L'~':
292           break;
293         default:
294           return (wctype_t) 0;
295         }
296 #  endif
297
298       /* Avoid overrunning the buffer.  */
299       if (cp == s + CHAR_CLASS_MAX_LENGTH)
300         return (wctype_t) 0;
301
302       *cp++ = (char) *wcs++;
303     }
304   while (*wcs != L'\0');
305
306   *cp = '\0';
307
308 #  ifdef _LIBC
309   return __wctype (s);
310 #  else
311   return wctype (s);
312 #  endif
313 }
314 #  define IS_CHAR_CLASS(string) is_char_class (string)
315
316 #  include "fnmatch_loop.c"
317 # endif
318
319
320 int
321 fnmatch (pattern, string, flags)
322      const char *pattern;
323      const char *string;
324      int flags;
325 {
326 # if HANDLE_MULTIBYTE
327   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
328     {
329       mbstate_t ps;
330       size_t n;
331       wchar_t *wpattern;
332       wchar_t *wstring;
333
334       /* Convert the strings into wide characters.  */
335       memset (&ps, '\0', sizeof (ps));
336       n = mbsrtowcs (NULL, &pattern, 0, &ps);
337       if (__builtin_expect (n == (size_t) -1, 0))
338         /* Something wrong.
339            XXX Do we have to set `errno' to something which mbsrtows hasn't
340            already done?  */
341         return -1;
342       wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
343       assert (mbsinit (&ps));
344       (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
345
346       assert (mbsinit (&ps));
347       n = mbsrtowcs (NULL, &string, 0, &ps);
348       if (__builtin_expect (n == (size_t) -1, 0))
349         /* Something wrong.
350            XXX Do we have to set `errno' to something which mbsrtows hasn't
351            already done?  */
352         return -1;
353       wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
354       assert (mbsinit (&ps));
355       (void) mbsrtowcs (wstring, &string, n + 1, &ps);
356
357       return internal_fnwmatch (wpattern, wstring, wstring + n,
358                                 flags & FNM_PERIOD, flags);
359     }
360 # endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
361
362   return internal_fnmatch (pattern, string, string + strlen (string),
363                            flags & FNM_PERIOD, flags);
364 }
365
366 # ifdef _LIBC
367 #  undef fnmatch
368 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
369 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
370 strong_alias (__fnmatch, __fnmatch_old)
371 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
372 #  endif
373 libc_hidden_ver (__fnmatch, fnmatch)
374 # endif
375
376 #endif  /* _LIBC or not __GNU_LIBRARY__.  */