fstat: Fix module dependency conditions.
[gnulib/ericb.git] / lib / fnmatch.c
blob90134bd113a59c9c5ab2dc8dfd209fe83cf7b0a2
1 /* Copyright (C) 1991-1993, 1996-2007, 2009-2017 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, see <http://www.gnu.org/licenses/>. */
16 #ifndef _LIBC
17 # include <config.h>
18 #endif
20 /* Enable GNU extensions in fnmatch.h. */
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE 1
23 #endif
25 #include <fnmatch.h>
27 #include <alloca.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #define WIDE_CHAR_SUPPORT \
37 (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
38 && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
40 /* For platform which support the ISO C amendment 1 functionality we
41 support user defined character classes. */
42 #if defined _LIBC || WIDE_CHAR_SUPPORT
43 # include <wctype.h>
44 # include <wchar.h>
45 #endif
47 /* We need some of the locale data (the collation sequence information)
48 but there is no interface to get this information in general. Therefore
49 we support a correct implementation only in glibc. */
50 #ifdef _LIBC
51 # include "../locale/localeinfo.h"
52 # include "../locale/elem-hash.h"
53 # include "../locale/coll-lookup.h"
54 # include <shlib-compat.h>
56 # define CONCAT(a,b) __CONCAT(a,b)
57 # define mbsrtowcs __mbsrtowcs
58 # define fnmatch __fnmatch
59 extern int fnmatch (const char *pattern, const char *string, int flags);
60 #endif
62 #ifndef SIZE_MAX
63 # define SIZE_MAX ((size_t) -1)
64 #endif
66 #include "flexmember.h"
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))
72 /* Comment out all this code if we are using the GNU C Library, and are not
73 actually compiling the library itself, and have not detected a bug
74 in the library. This code is part of the GNU C
75 Library, but also included in many other GNU distributions. Compiling
76 and linking in this code is a waste when using the GNU C library
77 (especially if it is a shared library). Rather than having every GNU
78 program understand 'configure --with-gnu-libc' and omit the object files,
79 it is simpler to just do this in the source for each such file. */
81 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
84 # if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
85 # define isblank(c) ((c) == ' ' || (c) == '\t')
86 # endif
88 # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
90 # if defined _LIBC || WIDE_CHAR_SUPPORT
91 /* The GNU C library provides support for user-defined character classes
92 and the functions from ISO C amendment 1. */
93 # ifdef CHARCLASS_NAME_MAX
94 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
95 # else
96 /* This shouldn't happen but some implementation might still have this
97 problem. Use a reasonable default value. */
98 # define CHAR_CLASS_MAX_LENGTH 256
99 # endif
101 # ifdef _LIBC
102 # define IS_CHAR_CLASS(string) __wctype (string)
103 # else
104 # define IS_CHAR_CLASS(string) wctype (string)
105 # endif
107 # ifdef _LIBC
108 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
109 # else
110 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
111 # endif
113 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
114 /* In this case we are implementing the multibyte character handling. */
115 # define HANDLE_MULTIBYTE 1
116 # endif
118 # else
119 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, 'xdigit'. */
121 # define IS_CHAR_CLASS(string) \
122 (STREQ (string, "alpha") || STREQ (string, "upper") \
123 || STREQ (string, "lower") || STREQ (string, "digit") \
124 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
125 || STREQ (string, "space") || STREQ (string, "print") \
126 || STREQ (string, "punct") || STREQ (string, "graph") \
127 || STREQ (string, "cntrl") || STREQ (string, "blank"))
128 # endif
130 /* Avoid depending on library functions or files
131 whose names are inconsistent. */
133 /* Global variable. */
134 static int posixly_correct;
136 # ifndef internal_function
137 /* Inside GNU libc we mark some function in a special way. In other
138 environments simply ignore the marking. */
139 # define internal_function
140 # endif
142 /* Note that this evaluates C many times. */
143 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
144 # define CHAR char
145 # define UCHAR unsigned char
146 # define INT int
147 # define FCT internal_fnmatch
148 # define EXT ext_match
149 # define END end_pattern
150 # define L_(CS) CS
151 # ifdef _LIBC
152 # define BTOWC(C) __btowc (C)
153 # else
154 # define BTOWC(C) btowc (C)
155 # endif
156 # define STRLEN(S) strlen (S)
157 # define STRCAT(D, S) strcat (D, S)
158 # ifdef _LIBC
159 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
160 # else
161 # if HAVE_MEMPCPY
162 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
163 # else
164 # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
165 # endif
166 # endif
167 # define MEMCHR(S, C, N) memchr (S, C, N)
168 # include "fnmatch_loop.c"
171 # if HANDLE_MULTIBYTE
172 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
173 # define CHAR wchar_t
174 # define UCHAR wint_t
175 # define INT wint_t
176 # define FCT internal_fnwmatch
177 # define EXT ext_wmatch
178 # define END end_wpattern
179 # define L_(CS) L##CS
180 # define BTOWC(C) (C)
181 # ifdef _LIBC
182 # define STRLEN(S) __wcslen (S)
183 # define STRCAT(D, S) __wcscat (D, S)
184 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
185 # else
186 # define STRLEN(S) wcslen (S)
187 # define STRCAT(D, S) wcscat (D, S)
188 # if HAVE_WMEMPCPY
189 # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
190 # else
191 # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
192 # endif
193 # endif
194 # define MEMCHR(S, C, N) wmemchr (S, C, N)
195 # define WIDE_CHAR_VERSION 1
197 # undef IS_CHAR_CLASS
198 /* We have to convert the wide character string in a multibyte string. But
199 we know that the character class names consist of alphanumeric characters
200 from the portable character set, and since the wide character encoding
201 for a member of the portable character set is the same code point as
202 its single-byte encoding, we can use a simplified method to convert the
203 string to a multibyte character string. */
204 static wctype_t
205 is_char_class (const wchar_t *wcs)
207 char s[CHAR_CLASS_MAX_LENGTH + 1];
208 char *cp = s;
212 /* Test for a printable character from the portable character set. */
213 # ifdef _LIBC
214 if (*wcs < 0x20 || *wcs > 0x7e
215 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
216 return (wctype_t) 0;
217 # else
218 switch (*wcs)
220 case L' ': case L'!': case L'"': case L'#': case L'%':
221 case L'&': case L'\'': case L'(': case L')': case L'*':
222 case L'+': case L',': case L'-': case L'.': case L'/':
223 case L'0': case L'1': case L'2': case L'3': case L'4':
224 case L'5': case L'6': case L'7': case L'8': case L'9':
225 case L':': case L';': case L'<': case L'=': case L'>':
226 case L'?':
227 case L'A': case L'B': case L'C': case L'D': case L'E':
228 case L'F': case L'G': case L'H': case L'I': case L'J':
229 case L'K': case L'L': case L'M': case L'N': case L'O':
230 case L'P': case L'Q': case L'R': case L'S': case L'T':
231 case L'U': case L'V': case L'W': case L'X': case L'Y':
232 case L'Z':
233 case L'[': case L'\\': case L']': case L'^': case L'_':
234 case L'a': case L'b': case L'c': case L'd': case L'e':
235 case L'f': case L'g': case L'h': case L'i': case L'j':
236 case L'k': case L'l': case L'm': case L'n': case L'o':
237 case L'p': case L'q': case L'r': case L's': case L't':
238 case L'u': case L'v': case L'w': case L'x': case L'y':
239 case L'z': case L'{': case L'|': case L'}': case L'~':
240 break;
241 default:
242 return (wctype_t) 0;
244 # endif
246 /* Avoid overrunning the buffer. */
247 if (cp == s + CHAR_CLASS_MAX_LENGTH)
248 return (wctype_t) 0;
250 *cp++ = (char) *wcs++;
252 while (*wcs != L'\0');
254 *cp = '\0';
256 # ifdef _LIBC
257 return __wctype (s);
258 # else
259 return wctype (s);
260 # endif
262 # define IS_CHAR_CLASS(string) is_char_class (string)
264 # include "fnmatch_loop.c"
265 # endif
269 fnmatch (const char *pattern, const char *string, int flags)
271 # if HANDLE_MULTIBYTE
272 # define ALLOCA_LIMIT 2000
273 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
275 mbstate_t ps;
276 size_t patsize;
277 size_t strsize;
278 size_t totsize;
279 wchar_t *wpattern;
280 wchar_t *wstring;
281 int res;
283 /* Calculate the size needed to convert the strings to
284 wide characters. */
285 memset (&ps, '\0', sizeof (ps));
286 patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
287 if (__builtin_expect (patsize != 0, 1))
289 assert (mbsinit (&ps));
290 strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
291 if (__builtin_expect (strsize != 0, 1))
293 assert (mbsinit (&ps));
294 totsize = patsize + strsize;
295 if (__builtin_expect (! (patsize <= totsize
296 && totsize <= SIZE_MAX / sizeof (wchar_t)),
299 errno = ENOMEM;
300 return -1;
303 /* Allocate room for the wide characters. */
304 if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
305 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
306 else
308 wpattern = malloc (totsize * sizeof (wchar_t));
309 if (__builtin_expect (! wpattern, 0))
311 errno = ENOMEM;
312 return -1;
315 wstring = wpattern + patsize;
317 /* Convert the strings into wide characters. */
318 mbsrtowcs (wpattern, &pattern, patsize, &ps);
319 assert (mbsinit (&ps));
320 mbsrtowcs (wstring, &string, strsize, &ps);
322 res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
323 flags & FNM_PERIOD, flags);
325 if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
326 free (wpattern);
327 return res;
332 # endif /* HANDLE_MULTIBYTE */
334 return internal_fnmatch (pattern, string, string + strlen (string),
335 flags & FNM_PERIOD, flags);
338 # ifdef _LIBC
339 # undef fnmatch
340 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
341 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
342 strong_alias (__fnmatch, __fnmatch_old)
343 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
344 # endif
345 libc_hidden_ver (__fnmatch, fnmatch)
346 # endif
348 #endif /* _LIBC or not __GNU_LIBRARY__. */