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