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