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