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