1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 #ifndef _LIBC
19 # include <libc-config.h>
20 #endif
21
22 /* Enable GNU extensions in fnmatch.h. */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE 1
25 #endif
26
27 #include <fnmatch.h>
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <stdckdint.h>
34 #include <stdlib.h>
35 #if defined _LIBC || HAVE_ALLOCA
36 # include <alloca.h>
37 #endif
38 #include <wchar.h>
39 #include <wctype.h>
40 #include <stddef.h>
41
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. */
45 #ifdef _LIBC
46 # include "../locale/localeinfo.h"
47 # include "../locale/coll-lookup.h"
48 # include <shlib-compat.h>
49
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);
64 #endif
65
66 #ifdef _LIBC
67 # if __glibc_has_attribute (__fallthrough__)
68 # define FALLTHROUGH __attribute__ ((__fallthrough__))
69 # else
70 # define FALLTHROUGH ((void) 0)
71 # endif
72 #else
73 # include "attribute.h"
74 #endif
75
76 #include <flexmember.h>
77
78 #ifdef _LIBC
79 typedef ptrdiff_t idx_t;
80 #else
81 # include "idx.h"
82 #endif
83
84 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
85 #define NO_LEADING_PERIOD(flags) \
86 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
87
88 #ifndef _LIBC
89 # if HAVE_ALLOCA
90 /* The OS usually guarantees only one guard page at the bottom of the stack,
91 and a page size can be as small as 4096 bytes. So we cannot safely
92 allocate anything larger than 4096 bytes. Also care for the possibility
93 of a few compiler-allocated temporary stack slots. */
94 # define __libc_use_alloca(n) ((n) < 4032)
95 # else
96 /* Just use malloc. */
97 # define __libc_use_alloca(n) false
98 # undef alloca
99 # define alloca(n) malloc (n)
100 # endif
101 # define alloca_account(size, avar) ((avar) += (size), alloca (size))
102 #endif
103
104 /* Provide support for user-defined character classes, based on the functions
105 from ISO C 90 amendment 1. */
106 #ifdef CHARCLASS_NAME_MAX
107 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
108 #else
109 /* This shouldn't happen but some implementation might still have this
110 problem. Use a reasonable default value. */
111 # define CHAR_CLASS_MAX_LENGTH 256
112 #endif
113
114 #define IS_CHAR_CLASS(string) wctype (string)
115
116 /* Avoid depending on library functions or files
117 whose names are inconsistent. */
118
119 /* Global variable. */
120 static int posixly_correct;
121
122 /* Note that this evaluates C many times. */
123 #define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
124 #define CHAR char
125 #define UCHAR unsigned char
126 #define INT int
127 #define FCT internal_fnmatch
128 #define EXT ext_match
129 #define END end_pattern
130 #define STRUCT fnmatch_struct
131 #define L_(CS) CS
132 #define BTOWC(C) btowc (C)
133 #define STRLEN(S) strlen (S)
134 #define STRCAT(D, S) strcat (D, S)
135 #define MEMPCPY(D, S, N) mempcpy (D, S, N)
136 #define MEMCHR(S, C, N) memchr (S, C, N)
137 #define WIDE_CHAR_VERSION 0
138 #ifdef _LIBC
139 # include <locale/weight.h>
140 # define FINDIDX findidx
141 #endif
142 #include "fnmatch_loop.c"
143
144
145 #define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
146 #define CHAR wchar_t
147 #define UCHAR wint_t
148 #define INT wint_t
149 #define FCT internal_fnwmatch
150 #define EXT ext_wmatch
151 #define END end_wpattern
152 #define L_(CS) L##CS
153 #define BTOWC(C) (C)
154 #define STRLEN(S) wcslen (S)
155 #define STRCAT(D, S) wcscat (D, S)
156 #define MEMPCPY(D, S, N) wmempcpy (D, S, N)
157 #define MEMCHR(S, C, N) wmemchr (S, C, N)
158 #define WIDE_CHAR_VERSION 1
159 #ifdef _LIBC
160 /* Change the name the header defines so it doesn't conflict with
161 the <locale/weight.h> version included above. */
162 # define findidx findidxwc
163 # include <locale/weightwc.h>
164 # undef findidx
165 # define FINDIDX findidxwc
166 #endif
167
168 #undef IS_CHAR_CLASS
169 /* We have to convert the wide character string in a multibyte string. But
170 we know that the character class names consist of alphanumeric characters
171 from the portable character set, and since the wide character encoding
172 for a member of the portable character set is the same code point as
173 its single-byte encoding, we can use a simplified method to convert the
174 string to a multibyte character string. */
175 static wctype_t
176 is_char_class (const wchar_t *wcs)
177 {
178 char s[CHAR_CLASS_MAX_LENGTH + 1];
179 char *cp = s;
180
181 do
182 {
183 /* Test for a printable character from the portable character set. */
184 #ifdef _LIBC
185 if (*wcs < 0x20 || *wcs > 0x7e
186 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
187 return (wctype_t) 0;
188 #else
189 switch (*wcs)
190 {
191 case L' ': case L'!': case L'"': case L'#': case L'%':
192 case L'&': case L'\'': case L'(': case L')': case L'*':
193 case L'+': case L',': case L'-': case L'.': case L'/':
194 case L'0': case L'1': case L'2': case L'3': case L'4':
195 case L'5': case L'6': case L'7': case L'8': case L'9':
196 case L':': case L';': case L'<': case L'=': case L'>':
197 case L'?':
198 case L'A': case L'B': case L'C': case L'D': case L'E':
199 case L'F': case L'G': case L'H': case L'I': case L'J':
200 case L'K': case L'L': case L'M': case L'N': case L'O':
201 case L'P': case L'Q': case L'R': case L'S': case L'T':
202 case L'U': case L'V': case L'W': case L'X': case L'Y':
203 case L'Z':
204 case L'[': case L'\\': case L']': case L'^': case L'_':
205 case L'a': case L'b': case L'c': case L'd': case L'e':
206 case L'f': case L'g': case L'h': case L'i': case L'j':
207 case L'k': case L'l': case L'm': case L'n': case L'o':
208 case L'p': case L'q': case L'r': case L's': case L't':
209 case L'u': case L'v': case L'w': case L'x': case L'y':
210 case L'z': case L'{': case L'|': case L'}': case L'~':
211 break;
212 default:
213 return (wctype_t) 0;
214 }
215 #endif
216
217 /* Avoid overrunning the buffer. */
218 if (cp == s + CHAR_CLASS_MAX_LENGTH)
219 return (wctype_t) 0;
220
221 *cp++ = (char) *wcs++;
222 }
223 while (*wcs != L'\0');
224
225 *cp = '\0';
226
227 return wctype (s);
228 }
229 #define IS_CHAR_CLASS(string) is_char_class (string)
230
231 #include "fnmatch_loop.c"
232
233
234 int
235 fnmatch (const char *pattern, const char *string, int flags)
236 {
237 if (__glibc_unlikely (MB_CUR_MAX != 1))
238 {
239 mbstate_t ps;
240 size_t n;
241 const char *p;
242 wchar_t *wpattern_malloc = NULL;
243 wchar_t *wpattern;
244 wchar_t *wstring_malloc = NULL;
245 wchar_t *wstring;
246 size_t alloca_used = 0;
247
248 /* Convert the strings into wide characters. */
249 memset (&ps, '\0', sizeof (ps));
250 p = pattern;
251 n = strnlen (pattern, 1024);
252 if (__glibc_likely (n < 1024))
253 {
254 wpattern = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
255 alloca_used);
256 n = mbsrtowcs (wpattern, &p, n + 1, &ps);
257 if (__glibc_unlikely (n == (size_t) -1))
258 /* Something wrong.
259 XXX Do we have to set 'errno' to something which mbsrtows hasn't
260 already done? */
261 return -1;
262 if (p)
263 {
264 memset (&ps, '\0', sizeof (ps));
265 goto prepare_wpattern;
266 }
267 }
268 else
269 {
270 prepare_wpattern:
271 n = mbsrtowcs (NULL, &pattern, 0, &ps);
272 if (__glibc_unlikely (n == (size_t) -1))
273 /* Something wrong.
274 XXX Do we have to set 'errno' to something which mbsrtows hasn't
275 already done? */
276 return -1;
277 if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
278 {
279 __set_errno (ENOMEM);
280 return -2;
281 }
282 wpattern_malloc = wpattern
283 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
284 assert (mbsinit (&ps));
285 if (wpattern == NULL)
286 return -2;
287 (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
288 }
289
290 assert (mbsinit (&ps));
291 n = strnlen (string, 1024);
292 p = string;
293 if (__glibc_likely (n < 1024))
294 {
295 wstring = (wchar_t *) alloca_account ((n + 1) * sizeof (wchar_t),
296 alloca_used);
297 n = mbsrtowcs (wstring, &p, n + 1, &ps);
298 if (__glibc_unlikely (n == (size_t) -1))
299 {
300 /* Something wrong.
301 XXX Do we have to set 'errno' to something which
302 mbsrtows hasn't already done? */
303 free_return:
304 free (wpattern_malloc);
305 return -1;
306 }
307 if (p)
308 {
309 memset (&ps, '\0', sizeof (ps));
310 goto prepare_wstring;
311 }
312 }
313 else
314 {
315 prepare_wstring:
316 n = mbsrtowcs (NULL, &string, 0, &ps);
317 if (__glibc_unlikely (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 goto free_return;
322 if (__glibc_unlikely (n >= (size_t) -1 / sizeof (wchar_t)))
323 {
324 free (wpattern_malloc);
325 __set_errno (ENOMEM);
326 return -2;
327 }
328
329 wstring_malloc = wstring
330 = (wchar_t *) malloc ((n + 1) * sizeof (wchar_t));
331 if (wstring == NULL)
332 {
333 free (wpattern_malloc);
334 return -2;
335 }
336 assert (mbsinit (&ps));
337 (void) mbsrtowcs (wstring, &string, n + 1, &ps);
338 }
339
340 int res = internal_fnwmatch (wpattern, wstring, wstring + n,
341 flags & FNM_PERIOD, flags, NULL,
342 alloca_used);
343
344 free (wstring_malloc);
345 free (wpattern_malloc);
346
347 return res;
348 }
349
350 return internal_fnmatch (pattern, string, string + strlen (string),
351 flags & FNM_PERIOD, flags, NULL, 0);
352 }
353
354 #undef fnmatch
355 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
356 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
357 strong_alias (__fnmatch, __fnmatch_old)
358 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
359 #endif
360 libc_hidden_ver (__fnmatch, fnmatch)