OSDN Git Service

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