2dbebfe31ed1fcb0f26cec74e1db6a165d764857
[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 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
110 /* In this case we are implementing the multibyte character handling.  */
111 #   define HANDLE_MULTIBYTE     1
112 #  endif
113
114 # else
115 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
116
117 #  define IS_CHAR_CLASS(string)                                               \
118    (STREQ (string, "alpha") || STREQ (string, "upper")                        \
119     || STREQ (string, "lower") || STREQ (string, "digit")                     \
120     || STREQ (string, "alnum") || STREQ (string, "xdigit")                    \
121     || STREQ (string, "space") || STREQ (string, "print")                     \
122     || STREQ (string, "punct") || STREQ (string, "graph")                     \
123     || STREQ (string, "cntrl") || STREQ (string, "blank"))
124 # endif
125
126 /* Avoid depending on library functions or files
127    whose names are inconsistent.  */
128
129 # if !defined _LIBC && !defined getenv
130 extern char *getenv ();
131 # endif
132
133 # ifndef errno
134 extern int errno;
135 # endif
136
137 /* This function doesn't exist on most systems.  */
138
139 # if !defined HAVE___STRCHRNUL && !defined _LIBC
140 static char *
141 __strchrnul (s, c)
142      const char *s;
143      int c;
144 {
145   char *result = strchr (s, c);
146   if (result == NULL)
147     result = strchr (s, '\0');
148   return result;
149 }
150 # endif
151
152 # ifndef internal_function
153 /* Inside GNU libc we mark some function in a special way.  In other
154    environments simply ignore the marking.  */
155 #  define internal_function
156 # endif
157
158 /* Note that this evaluates C many times.  */
159 # ifdef _LIBC
160 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
161 # else
162 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
163 # endif
164 # define CHAR   char
165 # define UCHAR  unsigned char
166 # define FCT    internal_fnmatch
167 # define L(CS)  CS
168 # define STRCHR(S, C)   strchr (S, C)
169 # define STRCHRNUL(S, C) __strchrnul (S, C)
170 # include "fnmatch_loop.c"
171
172
173 # if HANDLE_MULTIBYTE
174 /* Note that this evaluates C many times.  */
175 #  ifdef _LIBC
176 #   define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
177 #  else
178 #   define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
179 #  endif
180 #  define CHAR  wchar_t
181 #  define UCHAR wint_t
182 #  define FCT   internal_fnwmatch
183 #  define L(CS) L##CS
184 #  define __btowc(wc)   wc
185 #  define STRCHR(S, C)  wcschr (S, C)
186 #  define STRCHRNUL(S, C) __wcschrnul (S, C)
187
188 #  undef IS_CHAR_CLASS
189 #  ifdef _LIBC
190 /* We have to convert the wide character string in a multibyte string.  But
191    we know that the character class names are ASCII strings and since the
192    internal wide character encoding is UCS4 we can use a simplified method
193    to convert the string to a multibyte character string.  */
194 static wctype_t
195 is_char_class (const wchar_t *wcs)
196 {
197   char s[CHAR_CLASS_MAX_LENGTH + 1];
198   char *cp = s;
199
200   do
201     {
202       if (*wcs < 0x20 || *wcs >= 0x7f)
203         return 0;
204
205       *cp++ = (char) *wcs;
206     }
207   while (*wcs++ != L'\0');
208
209   return __wctype (s);
210 }
211 #  else
212 /* Since we cannot assume anything about the internal encoding we have to
213    convert the string back to multibyte representation the hard way.  */
214 static wctype_t
215 is_char_class (const wchar_t *wcs)
216 {
217   mstate_t ps;
218   char *s;
219   size_t n;
220
221   memset (&ps, '\0', sizeof (ps));
222
223   n = wcsrtombs (NULL, wcs, 0, &ps);
224   if (n == (size_t) -1)
225     /* Something went wrong.  */
226     return 0;
227
228   s = alloca (n + 1);
229   assert (mbsinit (&ps));
230   (void) wcsrtombs (s, wcs, n + 1, &ps);
231
232   return __wctype (s);
233 }
234 #  endif
235 #  define IS_CHAR_CLASS(string) is_char_class (string)
236
237 #  include "fnmatch_loop.c"
238 # endif
239
240 int
241 fnmatch (pattern, string, flags)
242      const char *pattern;
243      const char *string;
244      int flags;
245 {
246 # if HANDLE_MULTIBYTE
247   mbstate_t ps;
248   size_t n;
249   wchar_t *wpattern;
250   wchar_t *wstring;
251
252   if (MB_CUR_MAX == 1)
253     /* This is an optimization for 8-bit character set.  */
254     return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
255
256   /* Convert the strings into wide characters.  */
257   memset (&ps, '\0', sizeof (ps));
258   n = mbsrtowcs (NULL, &pattern, 0, &ps);
259   if (n == (size_t) -1)
260     /* Something wrong.
261        XXX Do we have to set `errno' to something which mbsrtows hasn't
262        already done?  */
263     return -1;
264   wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
265   assert (mbsinit (&ps));
266   (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
267
268   assert (mbsinit (&ps));
269   n = mbsrtowcs (NULL, &string, 0, &ps);
270   if (n == (size_t) -1)
271     /* Something wrong.
272        XXX Do we have to set `errno' to something which mbsrtows hasn't
273        already done?  */
274     return -1;
275   wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
276   assert (mbsinit (&ps));
277   (void) mbsrtowcs (wstring, &string, n + 1, &ps);
278
279   return internal_fnwmatch (wpattern, wstring, flags & FNM_PERIOD, flags);
280 # else
281   return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
282 # endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
283 }
284
285 #endif  /* _LIBC or not __GNU_LIBRARY__.  */