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 /* Comment out all this code if we are using the GNU C Library, and are not
52    actually compiling the library itself.  This code is part of the GNU C
53    Library, but also included in many other GNU distributions.  Compiling
54    and linking in this code is a waste when using the GNU C library
55    (especially if it is a shared library).  Rather than having every GNU
56    program understand `configure --with-gnu-libc' and omit the object files,
57    it is simpler to just do this in the source for each such file.  */
58
59 #if defined _LIBC || !defined __GNU_LIBRARY__
60
61
62 # if defined STDC_HEADERS || !defined isascii
63 #  define ISASCII(c) 1
64 # else
65 #  define ISASCII(c) isascii(c)
66 # endif
67
68 # ifdef isblank
69 #  define ISBLANK(c) (ISASCII (c) && isblank (c))
70 # else
71 #  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
72 # endif
73 # ifdef isgraph
74 #  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
75 # else
76 #  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
77 # endif
78
79 # define ISPRINT(c) (ISASCII (c) && isprint (c))
80 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
81 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
82 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
83 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
84 # define ISLOWER(c) (ISASCII (c) && islower (c))
85 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
86 # define ISSPACE(c) (ISASCII (c) && isspace (c))
87 # define ISUPPER(c) (ISASCII (c) && isupper (c))
88 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
89
90 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
91
92 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
93 /* The GNU C library provides support for user-defined character classes
94    and the functions from ISO C amendement 1.  */
95 #  ifdef CHARCLASS_NAME_MAX
96 #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
97 #  else
98 /* This shouldn't happen but some implementation might still have this
99    problem.  Use a reasonable default value.  */
100 #   define CHAR_CLASS_MAX_LENGTH 256
101 #  endif
102
103 #  ifdef _LIBC
104 #   define IS_CHAR_CLASS(string) __wctype (string)
105 #  else
106 #   define IS_CHAR_CLASS(string) wctype (string)
107 #  endif
108
109 #  ifdef _LIBC
110 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
111 #  else
112 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
113 #  endif
114
115 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
116 /* In this case we are implementing the multibyte character handling.  */
117 #   define HANDLE_MULTIBYTE     1
118 #  endif
119
120 # else
121 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
122
123 #  define IS_CHAR_CLASS(string)                                               \
124    (STREQ (string, "alpha") || STREQ (string, "upper")                        \
125     || STREQ (string, "lower") || STREQ (string, "digit")                     \
126     || STREQ (string, "alnum") || STREQ (string, "xdigit")                    \
127     || STREQ (string, "space") || STREQ (string, "print")                     \
128     || STREQ (string, "punct") || STREQ (string, "graph")                     \
129     || STREQ (string, "cntrl") || STREQ (string, "blank"))
130 # endif
131
132 /* Avoid depending on library functions or files
133    whose names are inconsistent.  */
134
135 # if !defined _LIBC && !defined getenv
136 extern char *getenv ();
137 # endif
138
139 # ifndef errno
140 extern int errno;
141 # endif
142
143 /* This function doesn't exist on most systems.  */
144
145 # if !defined HAVE___STRCHRNUL && !defined _LIBC
146 static char *
147 __strchrnul (s, c)
148      const char *s;
149      int c;
150 {
151   char *result = strchr (s, c);
152   if (result == NULL)
153     result = strchr (s, '\0');
154   return result;
155 }
156 # endif
157
158 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
159 static wchar_t *
160 __wcschrnul (s, c)
161      const wchar_t *s;
162      wint_t c;
163 {
164   wchar_t *result = wcschr (s, c);
165   if (result == NULL)
166     result = wcschr (s, '\0');
167   return result;
168 }
169 # endif
170
171 # ifndef internal_function
172 /* Inside GNU libc we mark some function in a special way.  In other
173    environments simply ignore the marking.  */
174 #  define internal_function
175 # endif
176
177 /* Note that this evaluates C many times.  */
178 # ifdef _LIBC
179 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
180 # else
181 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
182 # endif
183 # define CHAR   char
184 # define UCHAR  unsigned char
185 # define FCT    internal_fnmatch
186 # define L(CS)  CS
187 # ifdef _LIBC
188 #  define BTOWC(C)      __btowc (C)
189 # else
190 #  define BTOWC(C)      btowc (C)
191 # endif
192 # define STRCHR(S, C)   strchr (S, C)
193 # define STRCHRNUL(S, C) __strchrnul (S, C)
194 # define STRCOLL(S1, S2) strcoll (S1, S2)
195 # include "fnmatch_loop.c"
196
197
198 # if HANDLE_MULTIBYTE
199 /* Note that this evaluates C many times.  */
200 #  ifdef _LIBC
201 #   define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
202 #  else
203 #   define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
204 #  endif
205 #  define CHAR  wchar_t
206 #  define UCHAR wint_t
207 #  define FCT   internal_fnwmatch
208 #  define L(CS) L##CS
209 #  define BTOWC(C)      (C)
210 #  define STRCHR(S, C)  wcschr (S, C)
211 #  define STRCHRNUL(S, C) __wcschrnul (S, C)
212 # define STRCOLL(S1, S2) wcscoll (S1, S2)
213
214 #  undef IS_CHAR_CLASS
215 #  ifdef _LIBC
216 /* We have to convert the wide character string in a multibyte string.  But
217    we know that the character class names are ASCII strings and since the
218    internal wide character encoding is UCS4 we can use a simplified method
219    to convert the string to a multibyte character string.  */
220 static wctype_t
221 is_char_class (const wchar_t *wcs)
222 {
223   char s[CHAR_CLASS_MAX_LENGTH + 1];
224   char *cp = s;
225
226   do
227     {
228       if (*wcs < 0x20 || *wcs >= 0x7f)
229         return 0;
230
231       *cp++ = (char) *wcs;
232     }
233   while (*wcs++ != L'\0');
234
235   return __wctype (s);
236 }
237 #  else
238 /* Since we cannot assume anything about the internal encoding we have to
239    convert the string back to multibyte representation the hard way.  */
240 static wctype_t
241 is_char_class (const wchar_t *wcs)
242 {
243   mbstate_t ps;
244   const wchar_t *pwc;
245   char *s;
246   size_t n;
247
248   memset (&ps, '\0', sizeof (ps));
249
250   pwc = wcs;
251   n = wcsrtombs (NULL, &pwc, 0, &ps);
252   if (n == (size_t) -1)
253     /* Something went wrong.  */
254     return 0;
255
256   s = alloca (n + 1);
257   assert (mbsinit (&ps));
258   pwc = wcs;
259   (void) wcsrtombs (s, &pwc, n + 1, &ps);
260
261   return wctype (s);
262 }
263 #  endif
264 #  define IS_CHAR_CLASS(string) is_char_class (string)
265
266 #  include "fnmatch_loop.c"
267 # endif
268
269 int
270 fnmatch (pattern, string, flags)
271      const char *pattern;
272      const char *string;
273      int flags;
274 {
275 # if HANDLE_MULTIBYTE
276   mbstate_t ps;
277   size_t n;
278   wchar_t *wpattern;
279   wchar_t *wstring;
280
281   if (MB_CUR_MAX == 1)
282     /* This is an optimization for 8-bit character set.  */
283     return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
284
285   /* Convert the strings into wide characters.  */
286   memset (&ps, '\0', sizeof (ps));
287   n = mbsrtowcs (NULL, &pattern, 0, &ps);
288   if (n == (size_t) -1)
289     /* Something wrong.
290        XXX Do we have to set `errno' to something which mbsrtows hasn't
291        already done?  */
292     return -1;
293   wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
294   assert (mbsinit (&ps));
295   (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
296
297   assert (mbsinit (&ps));
298   n = mbsrtowcs (NULL, &string, 0, &ps);
299   if (n == (size_t) -1)
300     /* Something wrong.
301        XXX Do we have to set `errno' to something which mbsrtows hasn't
302        already done?  */
303     return -1;
304   wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
305   assert (mbsinit (&ps));
306   (void) mbsrtowcs (wstring, &string, n + 1, &ps);
307
308   return internal_fnwmatch (wpattern, wstring, flags & FNM_PERIOD, flags);
309 # else
310   return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
311 # endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
312 }
313
314 #endif  /* _LIBC or not __GNU_LIBRARY__.  */