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