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