1 /* Copyright (C) 1991-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 # include <libc-config.h>
22 /* Enable GNU extensions in fnmatch.h. */
24 # define _GNU_SOURCE 1
34 #if defined _LIBC || HAVE_ALLOCA
42 /* We need some of the locale data (the collation sequence information)
43 but there is no interface to get this information in general. Therefore
44 we support a correct implementation only in glibc. */
46 # include "../locale/localeinfo.h"
47 # include "../locale/coll-lookup.h"
48 # include <shlib-compat.h>
50 # define CONCAT(a,b) __CONCAT(a,b)
51 # define btowc __btowc
52 # define iswctype __iswctype
53 # define mbsrtowcs __mbsrtowcs
54 # define mempcpy __mempcpy
55 # define strnlen __strnlen
56 # define towlower __towlower
57 # define wcscat __wcscat
58 # define wcslen __wcslen
59 # define wctype __wctype
60 # define wmemchr __wmemchr
61 # define wmempcpy __wmempcpy
62 # define fnmatch __fnmatch
63 extern int fnmatch (const char *pattern
, const char *string
, int flags
);
67 # if (__GNUC__ >= 7) || (__clang_major__ >= 10)
68 # define FALLTHROUGH __attribute__ ((__fallthrough__))
70 # define FALLTHROUGH ((void) 0)
73 # include "attribute.h"
77 #include <flexmember.h>
79 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
80 #define NO_LEADING_PERIOD(flags) \
81 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
85 /* The OS usually guarantees only one guard page at the bottom of the stack,
86 and a page size can be as small as 4096 bytes. So we cannot safely
87 allocate anything larger than 4096 bytes. Also care for the possibility
88 of a few compiler-allocated temporary stack slots. */
89 # define __libc_use_alloca(n) ((n) < 4032)
91 /* Just use malloc. */
92 # define __libc_use_alloca(n) false
94 # define alloca(n) malloc (n)
96 # define alloca_account(size, avar) ((avar) += (size), alloca (size))
99 /* Provide support for user-defined character classes, based on the functions
100 from ISO C 90 amendment 1. */
101 #ifdef CHARCLASS_NAME_MAX
102 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
104 /* This shouldn't happen but some implementation might still have this
105 problem. Use a reasonable default value. */
106 # define CHAR_CLASS_MAX_LENGTH 256
109 #define IS_CHAR_CLASS(string) wctype (string)
111 /* Avoid depending on library functions or files
112 whose names are inconsistent. */
114 /* Global variable. */
115 static int posixly_correct
;
117 /* Note that this evaluates C many times. */
118 #define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
120 #define UCHAR unsigned char
122 #define FCT internal_fnmatch
123 #define EXT ext_match
124 #define END end_pattern
125 #define STRUCT fnmatch_struct
127 #define BTOWC(C) btowc (C)
128 #define STRLEN(S) strlen (S)
129 #define STRCAT(D, S) strcat (D, S)
130 #define MEMPCPY(D, S, N) mempcpy (D, S, N)
131 #define MEMCHR(S, C, N) memchr (S, C, N)
132 #define WIDE_CHAR_VERSION 0
134 # include <locale/weight.h>
135 # define FINDIDX findidx
137 #include "fnmatch_loop.c"
140 #define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
144 #define FCT internal_fnwmatch
145 #define EXT ext_wmatch
146 #define END end_wpattern
149 #define STRLEN(S) wcslen (S)
150 #define STRCAT(D, S) wcscat (D, S)
151 #define MEMPCPY(D, S, N) wmempcpy (D, S, N)
152 #define MEMCHR(S, C, N) wmemchr (S, C, N)
153 #define WIDE_CHAR_VERSION 1
155 /* Change the name the header defines so it doesn't conflict with
156 the <locale/weight.h> version included above. */
157 # define findidx findidxwc
158 # include <locale/weightwc.h>
160 # define FINDIDX findidxwc
164 /* We have to convert the wide character string in a multibyte string. But
165 we know that the character class names consist of alphanumeric characters
166 from the portable character set, and since the wide character encoding
167 for a member of the portable character set is the same code point as
168 its single-byte encoding, we can use a simplified method to convert the
169 string to a multibyte character string. */
171 is_char_class (const wchar_t *wcs
)
173 char s
[CHAR_CLASS_MAX_LENGTH
+ 1];
178 /* Test for a printable character from the portable character set. */
180 if (*wcs
< 0x20 || *wcs
> 0x7e
181 || *wcs
== 0x24 || *wcs
== 0x40 || *wcs
== 0x60)
186 case L
' ': case L
'!': case L
'"': case L
'#': case L
'%':
187 case L
'&': case L
'\'': case L
'(': case L
')': case L
'*':
188 case L
'+': case L
',': case L
'-': case L
'.': case L
'/':
189 case L
'0': case L
'1': case L
'2': case L
'3': case L
'4':
190 case L
'5': case L
'6': case L
'7': case L
'8': case L
'9':
191 case L
':': case L
';': case L
'<': case L
'=': case L
'>':
193 case L
'A': case L
'B': case L
'C': case L
'D': case L
'E':
194 case L
'F': case L
'G': case L
'H': case L
'I': case L
'J':
195 case L
'K': case L
'L': case L
'M': case L
'N': case L
'O':
196 case L
'P': case L
'Q': case L
'R': case L
'S': case L
'T':
197 case L
'U': case L
'V': case L
'W': case L
'X': case L
'Y':
199 case L
'[': case L
'\\': case L
']': case L
'^': case L
'_':
200 case L
'a': case L
'b': case L
'c': case L
'd': case L
'e':
201 case L
'f': case L
'g': case L
'h': case L
'i': case L
'j':
202 case L
'k': case L
'l': case L
'm': case L
'n': case L
'o':
203 case L
'p': case L
'q': case L
'r': case L
's': case L
't':
204 case L
'u': case L
'v': case L
'w': case L
'x': case L
'y':
205 case L
'z': case L
'{': case L
'|': case L
'}': case L
'~':
212 /* Avoid overrunning the buffer. */
213 if (cp
== s
+ CHAR_CLASS_MAX_LENGTH
)
216 *cp
++ = (char) *wcs
++;
218 while (*wcs
!= L
'\0');
224 #define IS_CHAR_CLASS(string) is_char_class (string)
226 #include "fnmatch_loop.c"
230 fnmatch (const char *pattern
, const char *string
, int flags
)
232 if (__glibc_unlikely (MB_CUR_MAX
!= 1))
237 wchar_t *wpattern_malloc
= NULL
;
239 wchar_t *wstring_malloc
= NULL
;
241 size_t alloca_used
= 0;
243 /* Convert the strings into wide characters. */
244 memset (&ps
, '\0', sizeof (ps
));
246 n
= strnlen (pattern
, 1024);
247 if (__glibc_likely (n
< 1024))
249 wpattern
= (wchar_t *) alloca_account ((n
+ 1) * sizeof (wchar_t),
251 n
= mbsrtowcs (wpattern
, &p
, n
+ 1, &ps
);
252 if (__glibc_unlikely (n
== (size_t) -1))
254 XXX Do we have to set 'errno' to something which mbsrtows hasn't
259 memset (&ps
, '\0', sizeof (ps
));
260 goto prepare_wpattern
;
266 n
= mbsrtowcs (NULL
, &pattern
, 0, &ps
);
267 if (__glibc_unlikely (n
== (size_t) -1))
269 XXX Do we have to set 'errno' to something which mbsrtows hasn't
272 if (__glibc_unlikely (n
>= (size_t) -1 / sizeof (wchar_t)))
274 __set_errno (ENOMEM
);
277 wpattern_malloc
= wpattern
278 = (wchar_t *) malloc ((n
+ 1) * sizeof (wchar_t));
279 assert (mbsinit (&ps
));
280 if (wpattern
== NULL
)
282 (void) mbsrtowcs (wpattern
, &pattern
, n
+ 1, &ps
);
285 assert (mbsinit (&ps
));
286 n
= strnlen (string
, 1024);
288 if (__glibc_likely (n
< 1024))
290 wstring
= (wchar_t *) alloca_account ((n
+ 1) * sizeof (wchar_t),
292 n
= mbsrtowcs (wstring
, &p
, n
+ 1, &ps
);
293 if (__glibc_unlikely (n
== (size_t) -1))
296 XXX Do we have to set 'errno' to something which
297 mbsrtows hasn't already done? */
299 free (wpattern_malloc
);
304 memset (&ps
, '\0', sizeof (ps
));
305 goto prepare_wstring
;
311 n
= mbsrtowcs (NULL
, &string
, 0, &ps
);
312 if (__glibc_unlikely (n
== (size_t) -1))
314 XXX Do we have to set 'errno' to something which mbsrtows hasn't
317 if (__glibc_unlikely (n
>= (size_t) -1 / sizeof (wchar_t)))
319 free (wpattern_malloc
);
320 __set_errno (ENOMEM
);
324 wstring_malloc
= wstring
325 = (wchar_t *) malloc ((n
+ 1) * sizeof (wchar_t));
328 free (wpattern_malloc
);
331 assert (mbsinit (&ps
));
332 (void) mbsrtowcs (wstring
, &string
, n
+ 1, &ps
);
335 int res
= internal_fnwmatch (wpattern
, wstring
, wstring
+ n
,
336 flags
& FNM_PERIOD
, flags
, NULL
,
339 free (wstring_malloc
);
340 free (wpattern_malloc
);
345 return internal_fnmatch (pattern
, string
, string
+ strlen (string
),
346 flags
& FNM_PERIOD
, flags
, NULL
, 0);
350 versioned_symbol (libc
, __fnmatch
, fnmatch
, GLIBC_2_2_3
);
351 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
352 strong_alias (__fnmatch
, __fnmatch_old
)
353 compat_symbol (libc
, __fnmatch_old
, fnmatch
, GLIBC_2_0
);
355 libc_hidden_ver (__fnmatch
, fnmatch
)