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