Update.
[glibc.git] / posix / fnmatch.c
blobc4b11080fe90e500de40b9485cef1e2bd7a0e81a
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"
57 # define CONCAT(a,b) __CONCAT(a,b)
58 #endif
60 /* Comment out all this code if we are using the GNU C Library, and are not
61 actually compiling the library itself. This code is part of the GNU C
62 Library, but also included in many other GNU distributions. Compiling
63 and linking in this code is a waste when using the GNU C library
64 (especially if it is a shared library). Rather than having every GNU
65 program understand `configure --with-gnu-libc' and omit the object files,
66 it is simpler to just do this in the source for each such file. */
68 #if defined _LIBC || !defined __GNU_LIBRARY__
71 # if defined STDC_HEADERS || !defined isascii
72 # define ISASCII(c) 1
73 # else
74 # define ISASCII(c) isascii(c)
75 # endif
77 # ifdef isblank
78 # define ISBLANK(c) (ISASCII (c) && isblank (c))
79 # else
80 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
81 # endif
82 # ifdef isgraph
83 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
84 # else
85 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
86 # endif
88 # define ISPRINT(c) (ISASCII (c) && isprint (c))
89 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
90 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
91 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
92 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
93 # define ISLOWER(c) (ISASCII (c) && islower (c))
94 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
95 # define ISSPACE(c) (ISASCII (c) && isspace (c))
96 # define ISUPPER(c) (ISASCII (c) && isupper (c))
97 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
99 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
101 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
102 /* The GNU C library provides support for user-defined character classes
103 and the functions from ISO C amendement 1. */
104 # ifdef CHARCLASS_NAME_MAX
105 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
106 # else
107 /* This shouldn't happen but some implementation might still have this
108 problem. Use a reasonable default value. */
109 # define CHAR_CLASS_MAX_LENGTH 256
110 # endif
112 # ifdef _LIBC
113 # define IS_CHAR_CLASS(string) __wctype (string)
114 # else
115 # define IS_CHAR_CLASS(string) wctype (string)
116 # endif
118 # ifdef _LIBC
119 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
120 # else
121 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
122 # endif
124 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
125 /* In this case we are implementing the multibyte character handling. */
126 # define HANDLE_MULTIBYTE 1
127 # endif
129 # else
130 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
132 # define IS_CHAR_CLASS(string) \
133 (STREQ (string, "alpha") || STREQ (string, "upper") \
134 || STREQ (string, "lower") || STREQ (string, "digit") \
135 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
136 || STREQ (string, "space") || STREQ (string, "print") \
137 || STREQ (string, "punct") || STREQ (string, "graph") \
138 || STREQ (string, "cntrl") || STREQ (string, "blank"))
139 # endif
141 /* Avoid depending on library functions or files
142 whose names are inconsistent. */
144 # if !defined _LIBC && !defined getenv
145 extern char *getenv ();
146 # endif
148 # ifndef errno
149 extern int errno;
150 # endif
152 /* This function doesn't exist on most systems. */
154 # if !defined HAVE___STRCHRNUL && !defined _LIBC
155 static char *
156 __strchrnul (s, c)
157 const char *s;
158 int c;
160 char *result = strchr (s, c);
161 if (result == NULL)
162 result = strchr (s, '\0');
163 return result;
165 # endif
167 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
168 static wchar_t *
169 __wcschrnul (s, c)
170 const wchar_t *s;
171 wint_t c;
173 wchar_t *result = wcschr (s, c);
174 if (result == NULL)
175 result = wcschr (s, '\0');
176 return result;
178 # endif
180 # ifndef internal_function
181 /* Inside GNU libc we mark some function in a special way. In other
182 environments simply ignore the marking. */
183 # define internal_function
184 # endif
186 /* Note that this evaluates C many times. */
187 # ifdef _LIBC
188 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
189 # else
190 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
191 # endif
192 # define CHAR char
193 # define UCHAR unsigned char
194 # define FCT internal_fnmatch
195 # define L(CS) CS
196 # ifdef _LIBC
197 # define BTOWC(C) __btowc (C)
198 # else
199 # define BTOWC(C) btowc (C)
200 # endif
201 # define STRCHR(S, C) strchr (S, C)
202 # define STRCHRNUL(S, C) __strchrnul (S, C)
203 # define STRCOLL(S1, S2) strcoll (S1, S2)
204 # define SUFFIX MB
205 # include "fnmatch_loop.c"
208 # if HANDLE_MULTIBYTE
209 /* Note that this evaluates C many times. */
210 # ifdef _LIBC
211 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
212 # else
213 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
214 # endif
215 # define CHAR wchar_t
216 # define UCHAR wint_t
217 # define FCT internal_fnwmatch
218 # define L(CS) L##CS
219 # define BTOWC(C) (C)
220 # define STRCHR(S, C) wcschr (S, C)
221 # define STRCHRNUL(S, C) __wcschrnul (S, C)
222 # define STRCOLL(S1, S2) wcscoll (S1, S2)
223 # define SUFFIX WC
224 # define WIDE_CHAR_VERSION 1
227 # undef IS_CHAR_CLASS
228 # ifdef _LIBC
229 /* We have to convert the wide character string in a multibyte string. But
230 we know that the character class names are ASCII strings and since the
231 internal wide character encoding is UCS4 we can use a simplified method
232 to convert the string to a multibyte character string. */
233 static wctype_t
234 is_char_class (const wchar_t *wcs)
236 char s[CHAR_CLASS_MAX_LENGTH + 1];
237 char *cp = s;
241 if (*wcs < 0x20 || *wcs >= 0x7f)
242 return 0;
244 *cp++ = (char) *wcs;
246 while (*wcs++ != L'\0');
248 return __wctype (s);
250 # else
251 /* Since we cannot assume anything about the internal encoding we have to
252 convert the string back to multibyte representation the hard way. */
253 static wctype_t
254 is_char_class (const wchar_t *wcs)
256 mbstate_t ps;
257 const wchar_t *pwc;
258 char *s;
259 size_t n;
261 memset (&ps, '\0', sizeof (ps));
263 pwc = wcs;
264 n = wcsrtombs (NULL, &pwc, 0, &ps);
265 if (n == (size_t) -1)
266 /* Something went wrong. */
267 return 0;
269 s = alloca (n + 1);
270 assert (mbsinit (&ps));
271 pwc = wcs;
272 (void) wcsrtombs (s, &pwc, n + 1, &ps);
274 return wctype (s);
276 # endif
277 # define IS_CHAR_CLASS(string) is_char_class (string)
279 # include "fnmatch_loop.c"
280 # endif
283 fnmatch (pattern, string, flags)
284 const char *pattern;
285 const char *string;
286 int flags;
288 # if HANDLE_MULTIBYTE
289 mbstate_t ps;
290 size_t n;
291 wchar_t *wpattern;
292 wchar_t *wstring;
294 if (MB_CUR_MAX == 1)
295 /* This is an optimization for 8-bit character set. */
296 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
298 /* Convert the strings into wide characters. */
299 memset (&ps, '\0', sizeof (ps));
300 n = mbsrtowcs (NULL, &pattern, 0, &ps);
301 if (n == (size_t) -1)
302 /* Something wrong.
303 XXX Do we have to set `errno' to something which mbsrtows hasn't
304 already done? */
305 return -1;
306 wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
307 assert (mbsinit (&ps));
308 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
310 assert (mbsinit (&ps));
311 n = mbsrtowcs (NULL, &string, 0, &ps);
312 if (n == (size_t) -1)
313 /* Something wrong.
314 XXX Do we have to set `errno' to something which mbsrtows hasn't
315 already done? */
316 return -1;
317 wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
318 assert (mbsinit (&ps));
319 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
321 return internal_fnwmatch (wpattern, wstring, flags & FNM_PERIOD, flags);
322 # else
323 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
324 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
327 #endif /* _LIBC or not __GNU_LIBRARY__. */