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