xref: /netbsd-src/external/gpl2/xcvs/dist/lib/fnmatch.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1a7c91847Schristos /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005
2a7c91847Schristos 	Free Software Foundation, Inc.
3a7c91847Schristos 
4a7c91847Schristos    This program is free software; you can redistribute it and/or modify
5a7c91847Schristos    it under the terms of the GNU General Public License as published by
6a7c91847Schristos    the Free Software Foundation; either version 2, or (at your option)
7a7c91847Schristos    any later version.
8a7c91847Schristos 
9a7c91847Schristos    This program is distributed in the hope that it will be useful,
10a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12a7c91847Schristos    GNU General Public License for more details.
13a7c91847Schristos 
14a7c91847Schristos    You should have received a copy of the GNU General Public License
15a7c91847Schristos    along with this program; if not, write to the Free Software Foundation,
16a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*5a6c14c8Schristos #include <sys/cdefs.h>
18*5a6c14c8Schristos __RCSID("$NetBSD: fnmatch.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
19*5a6c14c8Schristos 
20a7c91847Schristos 
21a7c91847Schristos #ifdef HAVE_CONFIG_H
22a7c91847Schristos # include <config.h>
23a7c91847Schristos #endif
24a7c91847Schristos 
25a7c91847Schristos /* Enable GNU extensions in fnmatch.h.  */
26a7c91847Schristos #ifndef _GNU_SOURCE
27a7c91847Schristos # define _GNU_SOURCE	1
28a7c91847Schristos #endif
29a7c91847Schristos 
30a7c91847Schristos #if ! defined __builtin_expect && __GNUC__ < 3
31a7c91847Schristos # define __builtin_expect(expr, expected) (expr)
32a7c91847Schristos #endif
33a7c91847Schristos 
34a7c91847Schristos #include <fnmatch.h>
35a7c91847Schristos 
36a7c91847Schristos #include <alloca.h>
37a7c91847Schristos #include <assert.h>
38a7c91847Schristos #include <ctype.h>
39a7c91847Schristos #include <errno.h>
40a7c91847Schristos #include <stddef.h>
41a7c91847Schristos #include <stdbool.h>
42a7c91847Schristos #include <stdlib.h>
43a7c91847Schristos #include <string.h>
44a7c91847Schristos 
45a7c91847Schristos #define WIDE_CHAR_SUPPORT \
46a7c91847Schristos   (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC \
47a7c91847Schristos    && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
48a7c91847Schristos 
49a7c91847Schristos /* For platform which support the ISO C amendement 1 functionality we
50a7c91847Schristos    support user defined character classes.  */
51a7c91847Schristos #if defined _LIBC || WIDE_CHAR_SUPPORT
52a7c91847Schristos /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
53a7c91847Schristos # include <wchar.h>
54a7c91847Schristos # include <wctype.h>
55a7c91847Schristos #endif
56a7c91847Schristos 
57a7c91847Schristos /* We need some of the locale data (the collation sequence information)
58a7c91847Schristos    but there is no interface to get this information in general.  Therefore
59a7c91847Schristos    we support a correct implementation only in glibc.  */
60a7c91847Schristos #ifdef _LIBC
61a7c91847Schristos # include "../locale/localeinfo.h"
62a7c91847Schristos # include "../locale/elem-hash.h"
63a7c91847Schristos # include "../locale/coll-lookup.h"
64a7c91847Schristos # include <shlib-compat.h>
65a7c91847Schristos 
66a7c91847Schristos # define CONCAT(a,b) __CONCAT(a,b)
67a7c91847Schristos # define mbsrtowcs __mbsrtowcs
68a7c91847Schristos # define fnmatch __fnmatch
69a7c91847Schristos extern int fnmatch (const char *pattern, const char *string, int flags);
70a7c91847Schristos #endif
71a7c91847Schristos 
72a7c91847Schristos #ifndef SIZE_MAX
73a7c91847Schristos # define SIZE_MAX ((size_t) -1)
74a7c91847Schristos #endif
75a7c91847Schristos 
76a7c91847Schristos /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
77a7c91847Schristos #define NO_LEADING_PERIOD(flags) \
78a7c91847Schristos   ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
79a7c91847Schristos 
80a7c91847Schristos /* Comment out all this code if we are using the GNU C Library, and are not
81a7c91847Schristos    actually compiling the library itself, and have not detected a bug
82a7c91847Schristos    in the library.  This code is part of the GNU C
83a7c91847Schristos    Library, but also included in many other GNU distributions.  Compiling
84a7c91847Schristos    and linking in this code is a waste when using the GNU C library
85a7c91847Schristos    (especially if it is a shared library).  Rather than having every GNU
86a7c91847Schristos    program understand `configure --with-gnu-libc' and omit the object files,
87a7c91847Schristos    it is simpler to just do this in the source for each such file.  */
88a7c91847Schristos 
89a7c91847Schristos #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
90a7c91847Schristos 
91a7c91847Schristos 
92a7c91847Schristos # if defined STDC_HEADERS || !defined isascii
93a7c91847Schristos #  define ISASCII(c) 1
94a7c91847Schristos # else
95a7c91847Schristos #  define ISASCII(c) isascii(c)
96a7c91847Schristos # endif
97a7c91847Schristos 
98a7c91847Schristos # ifdef isblank
99a7c91847Schristos #  define ISBLANK(c) (ISASCII (c) && isblank (c))
100a7c91847Schristos # else
101a7c91847Schristos #  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
102a7c91847Schristos # endif
103a7c91847Schristos # ifdef isgraph
104a7c91847Schristos #  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
105a7c91847Schristos # else
106a7c91847Schristos #  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
107a7c91847Schristos # endif
108a7c91847Schristos 
109a7c91847Schristos # define ISPRINT(c) (ISASCII (c) && isprint (c))
110a7c91847Schristos # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
111a7c91847Schristos # define ISALNUM(c) (ISASCII (c) && isalnum (c))
112a7c91847Schristos # define ISALPHA(c) (ISASCII (c) && isalpha (c))
113a7c91847Schristos # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
114a7c91847Schristos # define ISLOWER(c) (ISASCII (c) && islower (c))
115a7c91847Schristos # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
116a7c91847Schristos # define ISSPACE(c) (ISASCII (c) && isspace (c))
117a7c91847Schristos # define ISUPPER(c) (ISASCII (c) && isupper (c))
118a7c91847Schristos # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
119a7c91847Schristos 
120a7c91847Schristos # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
121a7c91847Schristos 
122a7c91847Schristos # if defined _LIBC || WIDE_CHAR_SUPPORT
123a7c91847Schristos /* The GNU C library provides support for user-defined character classes
124a7c91847Schristos    and the functions from ISO C amendement 1.  */
125a7c91847Schristos #  ifdef CHARCLASS_NAME_MAX
126a7c91847Schristos #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
127a7c91847Schristos #  else
128a7c91847Schristos /* This shouldn't happen but some implementation might still have this
129a7c91847Schristos    problem.  Use a reasonable default value.  */
130a7c91847Schristos #   define CHAR_CLASS_MAX_LENGTH 256
131a7c91847Schristos #  endif
132a7c91847Schristos 
133a7c91847Schristos #  ifdef _LIBC
134a7c91847Schristos #   define IS_CHAR_CLASS(string) __wctype (string)
135a7c91847Schristos #  else
136a7c91847Schristos #   define IS_CHAR_CLASS(string) wctype (string)
137a7c91847Schristos #  endif
138a7c91847Schristos 
139a7c91847Schristos #  ifdef _LIBC
140a7c91847Schristos #   define ISWCTYPE(WC, WT)	__iswctype (WC, WT)
141a7c91847Schristos #  else
142a7c91847Schristos #   define ISWCTYPE(WC, WT)	iswctype (WC, WT)
143a7c91847Schristos #  endif
144a7c91847Schristos 
145a7c91847Schristos #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
146a7c91847Schristos /* In this case we are implementing the multibyte character handling.  */
147a7c91847Schristos #   define HANDLE_MULTIBYTE	1
148a7c91847Schristos #  endif
149a7c91847Schristos 
150a7c91847Schristos # else
151a7c91847Schristos #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
152a7c91847Schristos 
153a7c91847Schristos #  define IS_CHAR_CLASS(string)						      \
154a7c91847Schristos    (STREQ (string, "alpha") || STREQ (string, "upper")			      \
155a7c91847Schristos     || STREQ (string, "lower") || STREQ (string, "digit")		      \
156a7c91847Schristos     || STREQ (string, "alnum") || STREQ (string, "xdigit")		      \
157a7c91847Schristos     || STREQ (string, "space") || STREQ (string, "print")		      \
158a7c91847Schristos     || STREQ (string, "punct") || STREQ (string, "graph")		      \
159a7c91847Schristos     || STREQ (string, "cntrl") || STREQ (string, "blank"))
160a7c91847Schristos # endif
161a7c91847Schristos 
162a7c91847Schristos /* Avoid depending on library functions or files
163a7c91847Schristos    whose names are inconsistent.  */
164a7c91847Schristos 
165a7c91847Schristos /* Global variable.  */
166a7c91847Schristos static int posixly_correct;
167a7c91847Schristos 
168a7c91847Schristos # ifndef internal_function
169a7c91847Schristos /* Inside GNU libc we mark some function in a special way.  In other
170a7c91847Schristos    environments simply ignore the marking.  */
171a7c91847Schristos #  define internal_function
172a7c91847Schristos # endif
173a7c91847Schristos 
174a7c91847Schristos /* Note that this evaluates C many times.  */
175a7c91847Schristos # ifdef _LIBC
176a7c91847Schristos #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
177a7c91847Schristos # else
178a7c91847Schristos #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
179a7c91847Schristos # endif
180a7c91847Schristos # define CHAR	char
181a7c91847Schristos # define UCHAR	unsigned char
182a7c91847Schristos # define INT	int
183a7c91847Schristos # define FCT	internal_fnmatch
184a7c91847Schristos # define EXT	ext_match
185a7c91847Schristos # define END	end_pattern
186a7c91847Schristos # define L(CS)	CS
187a7c91847Schristos # ifdef _LIBC
188a7c91847Schristos #  define BTOWC(C)	__btowc (C)
189a7c91847Schristos # else
190a7c91847Schristos #  define BTOWC(C)	btowc (C)
191a7c91847Schristos # endif
192a7c91847Schristos # define STRLEN(S) strlen (S)
193a7c91847Schristos # define STRCAT(D, S) strcat (D, S)
194a7c91847Schristos # ifdef _LIBC
195a7c91847Schristos #  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
196a7c91847Schristos # else
197a7c91847Schristos #  if HAVE_MEMPCPY
198a7c91847Schristos #   define MEMPCPY(D, S, N) mempcpy (D, S, N)
199a7c91847Schristos #  else
200a7c91847Schristos #   define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
201a7c91847Schristos #  endif
202a7c91847Schristos # endif
203a7c91847Schristos # define MEMCHR(S, C, N) memchr (S, C, N)
204a7c91847Schristos # define STRCOLL(S1, S2) strcoll (S1, S2)
205a7c91847Schristos # include "fnmatch_loop.c"
206a7c91847Schristos 
207a7c91847Schristos 
208a7c91847Schristos # if HANDLE_MULTIBYTE
209a7c91847Schristos #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
210a7c91847Schristos #  define CHAR	wchar_t
211a7c91847Schristos #  define UCHAR	wint_t
212a7c91847Schristos #  define INT	wint_t
213a7c91847Schristos #  define FCT	internal_fnwmatch
214a7c91847Schristos #  define EXT	ext_wmatch
215a7c91847Schristos #  define END	end_wpattern
216a7c91847Schristos #  define L(CS)	L##CS
217a7c91847Schristos #  define BTOWC(C)	(C)
218a7c91847Schristos #  ifdef _LIBC
219a7c91847Schristos #   define STRLEN(S) __wcslen (S)
220a7c91847Schristos #   define STRCAT(D, S) __wcscat (D, S)
221a7c91847Schristos #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
222a7c91847Schristos #  else
223a7c91847Schristos #   define STRLEN(S) wcslen (S)
224a7c91847Schristos #   define STRCAT(D, S) wcscat (D, S)
225a7c91847Schristos #   if HAVE_WMEMPCPY
226a7c91847Schristos #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
227a7c91847Schristos #   else
228a7c91847Schristos #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
229a7c91847Schristos #   endif
230a7c91847Schristos #  endif
231a7c91847Schristos #  define MEMCHR(S, C, N) wmemchr (S, C, N)
232a7c91847Schristos #  define STRCOLL(S1, S2) wcscoll (S1, S2)
233a7c91847Schristos #  define WIDE_CHAR_VERSION 1
234a7c91847Schristos 
235a7c91847Schristos #  undef IS_CHAR_CLASS
236a7c91847Schristos /* We have to convert the wide character string in a multibyte string.  But
237a7c91847Schristos    we know that the character class names consist of alphanumeric characters
238a7c91847Schristos    from the portable character set, and since the wide character encoding
239a7c91847Schristos    for a member of the portable character set is the same code point as
240a7c91847Schristos    its single-byte encoding, we can use a simplified method to convert the
241a7c91847Schristos    string to a multibyte character string.  */
242a7c91847Schristos static wctype_t
is_char_class(const wchar_t * wcs)243a7c91847Schristos is_char_class (const wchar_t *wcs)
244a7c91847Schristos {
245a7c91847Schristos   char s[CHAR_CLASS_MAX_LENGTH + 1];
246a7c91847Schristos   char *cp = s;
247a7c91847Schristos 
248a7c91847Schristos   do
249a7c91847Schristos     {
250a7c91847Schristos       /* Test for a printable character from the portable character set.  */
251a7c91847Schristos #  ifdef _LIBC
252a7c91847Schristos       if (*wcs < 0x20 || *wcs > 0x7e
253a7c91847Schristos 	  || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
254a7c91847Schristos 	return (wctype_t) 0;
255a7c91847Schristos #  else
256a7c91847Schristos       switch (*wcs)
257a7c91847Schristos 	{
258a7c91847Schristos 	case L' ': case L'!': case L'"': case L'#': case L'%':
259a7c91847Schristos 	case L'&': case L'\'': case L'(': case L')': case L'*':
260a7c91847Schristos 	case L'+': case L',': case L'-': case L'.': case L'/':
261a7c91847Schristos 	case L'0': case L'1': case L'2': case L'3': case L'4':
262a7c91847Schristos 	case L'5': case L'6': case L'7': case L'8': case L'9':
263a7c91847Schristos 	case L':': case L';': case L'<': case L'=': case L'>':
264a7c91847Schristos 	case L'?':
265a7c91847Schristos 	case L'A': case L'B': case L'C': case L'D': case L'E':
266a7c91847Schristos 	case L'F': case L'G': case L'H': case L'I': case L'J':
267a7c91847Schristos 	case L'K': case L'L': case L'M': case L'N': case L'O':
268a7c91847Schristos 	case L'P': case L'Q': case L'R': case L'S': case L'T':
269a7c91847Schristos 	case L'U': case L'V': case L'W': case L'X': case L'Y':
270a7c91847Schristos 	case L'Z':
271a7c91847Schristos 	case L'[': case L'\\': case L']': case L'^': case L'_':
272a7c91847Schristos 	case L'a': case L'b': case L'c': case L'd': case L'e':
273a7c91847Schristos 	case L'f': case L'g': case L'h': case L'i': case L'j':
274a7c91847Schristos 	case L'k': case L'l': case L'm': case L'n': case L'o':
275a7c91847Schristos 	case L'p': case L'q': case L'r': case L's': case L't':
276a7c91847Schristos 	case L'u': case L'v': case L'w': case L'x': case L'y':
277a7c91847Schristos 	case L'z': case L'{': case L'|': case L'}': case L'~':
278a7c91847Schristos 	  break;
279a7c91847Schristos 	default:
280a7c91847Schristos 	  return (wctype_t) 0;
281a7c91847Schristos 	}
282a7c91847Schristos #  endif
283a7c91847Schristos 
284a7c91847Schristos       /* Avoid overrunning the buffer.  */
285a7c91847Schristos       if (cp == s + CHAR_CLASS_MAX_LENGTH)
286a7c91847Schristos 	return (wctype_t) 0;
287a7c91847Schristos 
288a7c91847Schristos       *cp++ = (char) *wcs++;
289a7c91847Schristos     }
290a7c91847Schristos   while (*wcs != L'\0');
291a7c91847Schristos 
292a7c91847Schristos   *cp = '\0';
293a7c91847Schristos 
294a7c91847Schristos #  ifdef _LIBC
295a7c91847Schristos   return __wctype (s);
296a7c91847Schristos #  else
297a7c91847Schristos   return wctype (s);
298a7c91847Schristos #  endif
299a7c91847Schristos }
300a7c91847Schristos #  define IS_CHAR_CLASS(string) is_char_class (string)
301a7c91847Schristos 
302a7c91847Schristos #  include "fnmatch_loop.c"
303a7c91847Schristos # endif
304a7c91847Schristos 
305a7c91847Schristos 
306a7c91847Schristos int
fnmatch(const char * pattern,const char * string,int flags)307a7c91847Schristos fnmatch (const char *pattern, const char *string, int flags)
308a7c91847Schristos {
309a7c91847Schristos # if HANDLE_MULTIBYTE
310a7c91847Schristos #  define ALLOCA_LIMIT 2000
311a7c91847Schristos   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
312a7c91847Schristos     {
313a7c91847Schristos       mbstate_t ps;
314a7c91847Schristos       size_t patsize;
315a7c91847Schristos       size_t strsize;
316a7c91847Schristos       size_t totsize;
317a7c91847Schristos       wchar_t *wpattern;
318a7c91847Schristos       wchar_t *wstring;
319a7c91847Schristos       int res;
320a7c91847Schristos 
321a7c91847Schristos       /* Calculate the size needed to convert the strings to
322a7c91847Schristos 	 wide characters.  */
323a7c91847Schristos       memset (&ps, '\0', sizeof (ps));
324a7c91847Schristos       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
325a7c91847Schristos       if (__builtin_expect (patsize != 0, 1))
326a7c91847Schristos 	{
327a7c91847Schristos 	  assert (mbsinit (&ps));
328a7c91847Schristos 	  strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
329a7c91847Schristos 	  if (__builtin_expect (strsize != 0, 1))
330a7c91847Schristos 	    {
331a7c91847Schristos 	      assert (mbsinit (&ps));
332a7c91847Schristos 	      totsize = patsize + strsize;
333a7c91847Schristos 	      if (__builtin_expect (! (patsize <= totsize
334a7c91847Schristos 				       && totsize <= SIZE_MAX / sizeof (wchar_t)),
335a7c91847Schristos 				    0))
336a7c91847Schristos 		{
337a7c91847Schristos 		  errno = ENOMEM;
338a7c91847Schristos 		  return -1;
339a7c91847Schristos 		}
340a7c91847Schristos 
341a7c91847Schristos 	      /* Allocate room for the wide characters.  */
342a7c91847Schristos 	      if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
343a7c91847Schristos 		wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
344a7c91847Schristos 	      else
345a7c91847Schristos 		{
346a7c91847Schristos 		  wpattern = malloc (totsize * sizeof (wchar_t));
347a7c91847Schristos 		  if (__builtin_expect (! wpattern, 0))
348a7c91847Schristos 		    {
349a7c91847Schristos 		      errno = ENOMEM;
350a7c91847Schristos 		      return -1;
351a7c91847Schristos 		    }
352a7c91847Schristos 		}
353a7c91847Schristos 	      wstring = wpattern + patsize;
354a7c91847Schristos 
355a7c91847Schristos 	      /* Convert the strings into wide characters.  */
356a7c91847Schristos 	      mbsrtowcs (wpattern, &pattern, patsize, &ps);
357a7c91847Schristos 	      assert (mbsinit (&ps));
358a7c91847Schristos 	      mbsrtowcs (wstring, &string, strsize, &ps);
359a7c91847Schristos 
360a7c91847Schristos 	      res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
361a7c91847Schristos 				       flags & FNM_PERIOD, flags);
362a7c91847Schristos 
363a7c91847Schristos 	      if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
364a7c91847Schristos 		free (wpattern);
365a7c91847Schristos 	      return res;
366a7c91847Schristos 	    }
367a7c91847Schristos 	}
368a7c91847Schristos     }
369a7c91847Schristos 
370a7c91847Schristos # endif /* HANDLE_MULTIBYTE */
371a7c91847Schristos 
372a7c91847Schristos   return internal_fnmatch (pattern, string, string + strlen (string),
373a7c91847Schristos 			   flags & FNM_PERIOD, flags);
374a7c91847Schristos }
375a7c91847Schristos 
376a7c91847Schristos # ifdef _LIBC
377a7c91847Schristos #  undef fnmatch
378a7c91847Schristos versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
379a7c91847Schristos #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
380a7c91847Schristos strong_alias (__fnmatch, __fnmatch_old)
381a7c91847Schristos compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
382a7c91847Schristos #  endif
383a7c91847Schristos libc_hidden_ver (__fnmatch, fnmatch)
384a7c91847Schristos # endif
385a7c91847Schristos 
386a7c91847Schristos #endif	/* _LIBC or not __GNU_LIBRARY__.  */
387