Update resolver news.
[glibc.git] / posix / fnmatch.c
blobb6f67ae0b387ff79d61f11129a229548ca2edbe5
1 /* Copyright (C) 1991-1993, 1996-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /* Enable GNU extensions in fnmatch.h. */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE 1
26 #endif
28 #include <assert.h>
29 #include <errno.h>
30 #include <fnmatch.h>
31 #include <ctype.h>
33 #if HAVE_STRING_H || defined _LIBC
34 # include <string.h>
35 #else
36 # include <strings.h>
37 #endif
39 #if defined STDC_HEADERS || defined _LIBC
40 # include <stdlib.h>
41 #endif
43 /* For platform which support the ISO C amendement 1 functionality we
44 support user defined character classes. */
45 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
46 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
47 # include <wchar.h>
48 # include <wctype.h>
49 #endif
51 /* We need some of the locale data (the collation sequence information)
52 but there is no interface to get this information in general. Therefore
53 we support a correct implementation only in glibc. */
54 #ifdef _LIBC
55 # include "../locale/localeinfo.h"
56 # include "../locale/elem-hash.h"
58 # define CONCAT(a,b) __CONCAT(a,b)
59 #endif
61 /* Comment out all this code if we are using the GNU C Library, and are not
62 actually compiling the library itself. This code is part of the GNU C
63 Library, but also included in many other GNU distributions. Compiling
64 and linking in this code is a waste when using the GNU C library
65 (especially if it is a shared library). Rather than having every GNU
66 program understand `configure --with-gnu-libc' and omit the object files,
67 it is simpler to just do this in the source for each such file. */
69 #if defined _LIBC || !defined __GNU_LIBRARY__
72 # if defined STDC_HEADERS || !defined isascii
73 # define ISASCII(c) 1
74 # else
75 # define ISASCII(c) isascii(c)
76 # endif
78 # ifdef isblank
79 # define ISBLANK(c) (ISASCII (c) && isblank (c))
80 # else
81 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
82 # endif
83 # ifdef isgraph
84 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
85 # else
86 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
87 # endif
89 # define ISPRINT(c) (ISASCII (c) && isprint (c))
90 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
91 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
92 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
93 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
94 # define ISLOWER(c) (ISASCII (c) && islower (c))
95 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
96 # define ISSPACE(c) (ISASCII (c) && isspace (c))
97 # define ISUPPER(c) (ISASCII (c) && isupper (c))
98 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
100 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
102 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
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
113 # ifdef _LIBC
114 # define IS_CHAR_CLASS(string) __wctype (string)
115 # else
116 # define IS_CHAR_CLASS(string) wctype (string)
117 # endif
119 # ifdef _LIBC
120 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
121 # else
122 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
123 # endif
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
130 # else
131 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
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
142 /* Avoid depending on library functions or files
143 whose names are inconsistent. */
145 # if !defined _LIBC && !defined getenv
146 extern char *getenv ();
147 # endif
149 # ifndef errno
150 extern int errno;
151 # endif
153 /* This function doesn't exist on most systems. */
155 # if !defined HAVE___STRCHRNUL && !defined _LIBC
156 static char *
157 __strchrnul (s, c)
158 const char *s;
159 int c;
161 char *result = strchr (s, c);
162 if (result == NULL)
163 result = strchr (s, '\0');
164 return result;
166 # endif
168 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
169 static wchar_t *
170 __wcschrnul (s, c)
171 const wchar_t *s;
172 wint_t c;
174 wchar_t *result = wcschr (s, c);
175 if (result == NULL)
176 result = wcschr (s, '\0');
177 return result;
179 # endif
181 # ifndef internal_function
182 /* Inside GNU libc we mark some function in a special way. In other
183 environments simply ignore the marking. */
184 # define internal_function
185 # endif
187 /* Note that this evaluates C many times. */
188 # ifdef _LIBC
189 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
190 # else
191 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
192 # endif
193 # define CHAR char
194 # define UCHAR unsigned char
195 # define FCT internal_fnmatch
196 # define L(CS) CS
197 # ifdef _LIBC
198 # define BTOWC(C) __btowc (C)
199 # else
200 # define BTOWC(C) btowc (C)
201 # endif
202 # define STRCHR(S, C) strchr (S, C)
203 # define STRCHRNUL(S, C) __strchrnul (S, C)
204 # define STRCOLL(S1, S2) strcoll (S1, S2)
205 # define SUFFIX MB
206 # include "fnmatch_loop.c"
209 # if HANDLE_MULTIBYTE
210 /* Note that this evaluates C many times. */
211 # ifdef _LIBC
212 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
213 # else
214 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
215 # endif
216 # define CHAR wchar_t
217 # define UCHAR wint_t
218 # define FCT internal_fnwmatch
219 # define L(CS) L##CS
220 # define BTOWC(C) (C)
221 # define STRCHR(S, C) wcschr (S, C)
222 # define STRCHRNUL(S, C) __wcschrnul (S, C)
223 # define STRCOLL(S1, S2) wcscoll (S1, S2)
224 # define SUFFIX WC
225 # define WIDE_CHAR_VERSION 1
227 # undef IS_CHAR_CLASS
228 /* We have to convert the wide character string in a multibyte string. But
229 we know that the character class names consist of alphanumeric characters
230 from the portable character set, and since the wide character encoding
231 for a member of the portable character set is the same code point as
232 its single-byte encoding, we can use a simplified method to convert the
233 string to a multibyte character string. */
234 static wctype_t
235 is_char_class (const wchar_t *wcs)
237 char s[CHAR_CLASS_MAX_LENGTH + 1];
238 char *cp = s;
242 /* Test for a printable character from the portable character set. */
243 # ifdef _LIBC
244 if (*wcs < 0x20 || *wcs > 0x7e
245 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
246 return (wctype_t) 0;
247 # else
248 switch (*wcs)
250 case L' ': case L'!': case L'"': case L'#': case L'%':
251 case L'&': case L'\'': case L'(': case L')': case L'*':
252 case L'+': case L',': case L'-': case L'.': case L'/':
253 case L'0': case L'1': case L'2': case L'3': case L'4':
254 case L'5': case L'6': case L'7': case L'8': case L'9':
255 case L':': case L';': case L'<': case L'=': case L'>':
256 case L'?':
257 case L'A': case L'B': case L'C': case L'D': case L'E':
258 case L'F': case L'G': case L'H': case L'I': case L'J':
259 case L'K': case L'L': case L'M': case L'N': case L'O':
260 case L'P': case L'Q': case L'R': case L'S': case L'T':
261 case L'U': case L'V': case L'W': case L'X': case L'Y':
262 case L'Z':
263 case L'[': case L'\\': case L']': case L'^': case L'_':
264 case L'a': case L'b': case L'c': case L'd': case L'e':
265 case L'f': case L'g': case L'h': case L'i': case L'j':
266 case L'k': case L'l': case L'm': case L'n': case L'o':
267 case L'p': case L'q': case L'r': case L's': case L't':
268 case L'u': case L'v': case L'w': case L'x': case L'y':
269 case L'z': case L'{': case L'|': case L'}': case L'~':
270 break;
271 default:
272 return (wctype_t) 0;
274 # endif
276 /* Avoid overrunning the buffer. */
277 if (cp == s + CHAR_CLASS_MAX_LENGTH)
278 return (wctype_t) 0;
280 *cp++ = (char) *wcs++;
282 while (*wcs != L'\0');
284 *cp = '\0';
286 # ifdef _LIBC
287 return __wctype (s);
288 # else
289 return wctype (s);
290 # endif
292 # define IS_CHAR_CLASS(string) is_char_class (string)
294 # include "fnmatch_loop.c"
295 # endif
299 fnmatch (pattern, string, flags)
300 const char *pattern;
301 const char *string;
302 int flags;
304 # if HANDLE_MULTIBYTE
305 mbstate_t ps;
306 size_t n;
307 wchar_t *wpattern;
308 wchar_t *wstring;
310 if (MB_CUR_MAX == 1)
311 /* This is an optimization for 8-bit character set. */
312 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
314 /* Convert the strings into wide characters. */
315 memset (&ps, '\0', sizeof (ps));
316 n = mbsrtowcs (NULL, &pattern, 0, &ps);
317 if (n == (size_t) -1)
318 /* Something wrong.
319 XXX Do we have to set `errno' to something which mbsrtows hasn't
320 already done? */
321 return -1;
322 wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
323 assert (mbsinit (&ps));
324 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
326 assert (mbsinit (&ps));
327 n = mbsrtowcs (NULL, &string, 0, &ps);
328 if (n == (size_t) -1)
329 /* Something wrong.
330 XXX Do we have to set `errno' to something which mbsrtows hasn't
331 already done? */
332 return -1;
333 wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
334 assert (mbsinit (&ps));
335 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
337 return internal_fnwmatch (wpattern, wstring, flags & FNM_PERIOD, flags);
338 # else
339 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
340 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
343 #endif /* _LIBC or not __GNU_LIBRARY__. */