]>
Dogcows Code - chaz/tar/blob - lib/fnmatch.c
1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2000, 2001,
2 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Enable GNU extensions in fnmatch.h. */
24 # define _GNU_SOURCE 1
28 # define alloca __builtin_alloca
29 # define HAVE_ALLOCA 1
31 # if defined HAVE_ALLOCA_H || defined _LIBC
44 #if ! defined __builtin_expect && __GNUC__ < 3
45 # define __builtin_expect(expr, expected) (expr)
53 #if HAVE_STRING_H || defined _LIBC
61 #if defined STDC_HEADERS || defined _LIBC
66 #define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
68 /* For platform which support the ISO C amendement 1 functionality we
69 support user defined character classes. */
70 #if defined _LIBC || WIDE_CHAR_SUPPORT
71 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
76 /* We need some of the locale data (the collation sequence information)
77 but there is no interface to get this information in general. Therefore
78 we support a correct implementation only in glibc. */
80 # include "../locale/localeinfo.h"
81 # include "../locale/elem-hash.h"
82 # include "../locale/coll-lookup.h"
83 # include <shlib-compat.h>
85 # define CONCAT(a,b) __CONCAT(a,b)
86 # define mbsinit __mbsinit
87 # define mbsrtowcs __mbsrtowcs
88 # define fnmatch __fnmatch
89 extern int fnmatch (const char *pattern
, const char *string
, int flags
);
92 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
93 #define NO_LEADING_PERIOD(flags) \
94 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
96 /* Comment out all this code if we are using the GNU C Library, are not
97 actually compiling the library itself, and have not detected a bug
98 in the library. This code is part of the GNU C
99 Library, but also included in many other GNU distributions. Compiling
100 and linking in this code is a waste when using the GNU C library
101 (especially if it is a shared library). Rather than having every GNU
102 program understand `configure --with-gnu-libc' and omit the object files,
103 it is simpler to just do this in the source for each such file. */
105 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
108 # if defined STDC_HEADERS || !defined isascii
109 # define ISASCII(c) 1
111 # define ISASCII(c) isascii(c)
115 # define ISBLANK(c) (ISASCII (c) && isblank (c))
117 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
120 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
122 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
125 # define ISPRINT(c) (ISASCII (c) && isprint (c))
126 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
127 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
128 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
129 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
130 # define ISLOWER(c) (ISASCII (c) && islower (c))
131 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
132 # define ISSPACE(c) (ISASCII (c) && isspace (c))
133 # define ISUPPER(c) (ISASCII (c) && isupper (c))
134 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
136 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
138 # if defined _LIBC || WIDE_CHAR_SUPPORT
139 /* The GNU C library provides support for user-defined character classes
140 and the functions from ISO C amendement 1. */
141 # ifdef CHARCLASS_NAME_MAX
142 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
144 /* This shouldn't happen but some implementation might still have this
145 problem. Use a reasonable default value. */
146 # define CHAR_CLASS_MAX_LENGTH 256
150 # define IS_CHAR_CLASS(string) __wctype (string)
152 # define IS_CHAR_CLASS(string) wctype (string)
156 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
158 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
161 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
162 /* In this case we are implementing the multibyte character handling. */
163 # define HANDLE_MULTIBYTE 1
167 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
169 # define IS_CHAR_CLASS(string) \
170 (STREQ (string, "alpha") || STREQ (string, "upper") \
171 || STREQ (string, "lower") || STREQ (string, "digit") \
172 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
173 || STREQ (string, "space") || STREQ (string, "print") \
174 || STREQ (string, "punct") || STREQ (string, "graph") \
175 || STREQ (string, "cntrl") || STREQ (string, "blank"))
178 /* Avoid depending on library functions or files
179 whose names are inconsistent. */
181 # if !defined _LIBC && !defined getenv && !HAVE_DECL_GETENV
182 extern char *getenv ();
189 /* Global variable. */
190 static int posixly_correct
;
192 # ifndef internal_function
193 /* Inside GNU libc we mark some function in a special way. In other
194 environments simply ignore the marking. */
195 # define internal_function
198 /* Note that this evaluates C many times. */
200 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
202 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
205 # define UCHAR unsigned char
207 # define FCT internal_fnmatch
208 # define EXT ext_match
209 # define END end_pattern
212 # define BTOWC(C) __btowc (C)
214 # define BTOWC(C) btowc (C)
216 # define STRLEN(S) strlen (S)
217 # define STRCAT(D, S) strcat (D, S)
219 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
222 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
224 # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
227 # define MEMCHR(S, C, N) memchr (S, C, N)
228 # define STRCOLL(S1, S2) strcoll (S1, S2)
229 # include "fnmatch_loop.c"
232 # if HANDLE_MULTIBYTE
233 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
234 # define CHAR wchar_t
235 # define UCHAR wint_t
237 # define FCT internal_fnwmatch
238 # define EXT ext_wmatch
239 # define END end_wpattern
241 # define BTOWC(C) (C)
243 # define STRLEN(S) __wcslen (S)
244 # define STRCAT(D, S) __wcscat (D, S)
245 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
247 # define STRLEN(S) wcslen (S)
248 # define STRCAT(D, S) wcscat (D, S)
250 # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
252 # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (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
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. */
267 is_char_class (const wchar_t *wcs
)
269 char s
[CHAR_CLASS_MAX_LENGTH
+ 1];
274 /* Test for a printable character from the portable character set. */
276 if (*wcs
< 0x20 || *wcs
> 0x7e
277 || *wcs
== 0x24 || *wcs
== 0x40 || *wcs
== 0x60)
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
'>':
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':
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
'~':
308 /* Avoid overrunning the buffer. */
309 if (cp
== s
+ CHAR_CLASS_MAX_LENGTH
)
312 *cp
++ = (char) *wcs
++;
314 while (*wcs
!= L
'\0');
324 # define IS_CHAR_CLASS(string) is_char_class (string)
326 # include "fnmatch_loop.c"
331 fnmatch (pattern
, string
, flags
)
336 # if HANDLE_MULTIBYTE
337 if (__builtin_expect (MB_CUR_MAX
, 1) != 1)
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)
349 XXX Do we have to set `errno' to something which mbsrtows hasn't
352 wpattern
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
353 assert (mbsinit (&ps
));
354 (void) mbsrtowcs (wpattern
, &pattern
, n
+ 1, &ps
);
356 assert (mbsinit (&ps
));
357 n
= mbsrtowcs (NULL
, &string
, 0, &ps
);
358 if (__builtin_expect (n
, 0) == (size_t) -1)
360 XXX Do we have to set `errno' to something which mbsrtows hasn't
363 wstring
= (wchar_t *) alloca ((n
+ 1) * sizeof (wchar_t));
364 assert (mbsinit (&ps
));
365 (void) mbsrtowcs (wstring
, &string
, n
+ 1, &ps
);
367 return internal_fnwmatch (wpattern
, wstring
, wstring
+ n
,
368 flags
& FNM_PERIOD
, flags
);
370 # endif /* mbstate_t and mbsrtowcs or _LIBC. */
372 return internal_fnmatch (pattern
, string
, string
+ strlen (string
),
373 flags
& FNM_PERIOD
, flags
);
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
);
385 #endif /* _LIBC or not __GNU_LIBRARY__. */
This page took 0.058449 seconds and 4 git commands to generate.