1/* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2004 2 Free Software Foundation, Inc. 3 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) 7 any later version. 8 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. 13 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. */ 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#if ! defined __builtin_expect && __GNUC__ < 3 28# define __builtin_expect(expr, expected) (expr) 29#endif 30 31#include <fnmatch.h> 32 33#include <alloca.h> 34#include <assert.h> 35#include <ctype.h> 36#include <errno.h> 37#include <stddef.h> 38#include <stdbool.h> 39#include <stdlib.h> 40#include <string.h> 41 42#define WIDE_CHAR_SUPPORT \ 43 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC \ 44 && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY)) 45 46/* For platform which support the ISO C amendement 1 functionality we 47 support user defined character classes. */ 48#if defined _LIBC || WIDE_CHAR_SUPPORT 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/* We need some of the locale data (the collation sequence information) 55 but there is no interface to get this information in general. Therefore 56 we support a correct implementation only in glibc. */ 57#ifdef _LIBC 58# include "../locale/localeinfo.h" 59# include "../locale/elem-hash.h" 60# include "../locale/coll-lookup.h" 61# include <shlib-compat.h> 62 63# define CONCAT(a,b) __CONCAT(a,b) 64# define mbsrtowcs __mbsrtowcs 65# define fnmatch __fnmatch 66extern int fnmatch (const char *pattern, const char *string, int flags); 67#endif 68 69#ifndef SIZE_MAX 70# define SIZE_MAX ((size_t) -1) 71#endif 72 73/* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */ 74#define NO_LEADING_PERIOD(flags) \ 75 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD)) 76 77/* Comment out all this code if we are using the GNU C Library, and are not 78 actually compiling the library itself, and have not detected a bug 79 in the library. 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__ || !HAVE_FNMATCH_GNU 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 || WIDE_CHAR_SUPPORT 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/* Global variable. */ 163static int posixly_correct; 164 165# ifndef internal_function 166/* Inside GNU libc we mark some function in a special way. In other 167 environments simply ignore the marking. */ 168# define internal_function 169# endif 170 171/* Note that this evaluates C many times. */ 172# ifdef _LIBC 173# define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c)) 174# else 175# define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c)) 176# endif 177# define CHAR char 178# define UCHAR unsigned char 179# define INT int 180# define FCT internal_fnmatch 181# define EXT ext_match 182# define END end_pattern 183# define L(CS) CS 184# ifdef _LIBC 185# define BTOWC(C) __btowc (C) 186# else 187# define BTOWC(C) btowc (C) 188# endif 189# define STRLEN(S) strlen (S) 190# define STRCAT(D, S) strcat (D, S) 191# ifdef _LIBC 192# define MEMPCPY(D, S, N) __mempcpy (D, S, N) 193# else 194# if HAVE_MEMPCPY 195# define MEMPCPY(D, S, N) mempcpy (D, S, N) 196# else 197# define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) 198# endif 199# endif 200# define MEMCHR(S, C, N) memchr (S, C, N) 201# define STRCOLL(S1, S2) strcoll (S1, S2) 202# include "fnmatch_loop.c" 203 204 205# if HANDLE_MULTIBYTE 206# define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c)) 207# define CHAR wchar_t 208# define UCHAR wint_t 209# define INT wint_t 210# define FCT internal_fnwmatch 211# define EXT ext_wmatch 212# define END end_wpattern 213# define L(CS) L##CS 214# define BTOWC(C) (C) 215# ifdef _LIBC 216# define STRLEN(S) __wcslen (S) 217# define STRCAT(D, S) __wcscat (D, S) 218# define MEMPCPY(D, S, N) __wmempcpy (D, S, N) 219# else 220# define STRLEN(S) wcslen (S) 221# define STRCAT(D, S) wcscat (D, S) 222# if HAVE_WMEMPCPY 223# define MEMPCPY(D, S, N) wmempcpy (D, S, N) 224# else 225# define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N)) 226# endif 227# endif 228# define MEMCHR(S, C, N) wmemchr (S, C, N) 229# define STRCOLL(S1, S2) wcscoll (S1, S2) 230# define WIDE_CHAR_VERSION 1 231 232# undef IS_CHAR_CLASS 233/* We have to convert the wide character string in a multibyte string. But 234 we know that the character class names consist of alphanumeric characters 235 from the portable character set, and since the wide character encoding 236 for a member of the portable character set is the same code point as 237 its single-byte encoding, we can use a simplified method to convert the 238 string to a multibyte character string. */ 239static wctype_t 240is_char_class (const wchar_t *wcs) 241{ 242 char s[CHAR_CLASS_MAX_LENGTH + 1]; 243 char *cp = s; 244 245 do 246 { 247 /* Test for a printable character from the portable character set. */ 248# ifdef _LIBC 249 if (*wcs < 0x20 || *wcs > 0x7e 250 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60) 251 return (wctype_t) 0; 252# else 253 switch (*wcs) 254 { 255 case L' ': case L'!': case L'"': case L'#': case L'%': 256 case L'&': case L'\'': case L'(': case L')': case L'*': 257 case L'+': case L',': case L'-': case L'.': case L'/': 258 case L'0': case L'1': case L'2': case L'3': case L'4': 259 case L'5': case L'6': case L'7': case L'8': case L'9': 260 case L':': case L';': case L'<': case L'=': case L'>': 261 case L'?': 262 case L'A': case L'B': case L'C': case L'D': case L'E': 263 case L'F': case L'G': case L'H': case L'I': case L'J': 264 case L'K': case L'L': case L'M': case L'N': case L'O': 265 case L'P': case L'Q': case L'R': case L'S': case L'T': 266 case L'U': case L'V': case L'W': case L'X': case L'Y': 267 case L'Z': 268 case L'[': case L'\\': case L']': case L'^': case L'_': 269 case L'a': case L'b': case L'c': case L'd': case L'e': 270 case L'f': case L'g': case L'h': case L'i': case L'j': 271 case L'k': case L'l': case L'm': case L'n': case L'o': 272 case L'p': case L'q': case L'r': case L's': case L't': 273 case L'u': case L'v': case L'w': case L'x': case L'y': 274 case L'z': case L'{': case L'|': case L'}': case L'~': 275 break; 276 default: 277 return (wctype_t) 0; 278 } 279# endif 280 281 /* Avoid overrunning the buffer. */ 282 if (cp == s + CHAR_CLASS_MAX_LENGTH) 283 return (wctype_t) 0; 284 285 *cp++ = (char) *wcs++; 286 } 287 while (*wcs != L'\0'); 288 289 *cp = '\0'; 290 291# ifdef _LIBC 292 return __wctype (s); 293# else 294 return wctype (s); 295# endif 296} 297# define IS_CHAR_CLASS(string) is_char_class (string) 298 299# include "fnmatch_loop.c" 300# endif 301 302 303int 304fnmatch (const char *pattern, const char *string, int flags) 305{ 306# if HANDLE_MULTIBYTE 307# define ALLOCA_LIMIT 2000 308 if (__builtin_expect (MB_CUR_MAX, 1) != 1) 309 { 310 mbstate_t ps; 311 size_t patsize; 312 size_t strsize; 313 size_t totsize; 314 wchar_t *wpattern; 315 wchar_t *wstring; 316 int res; 317 318 /* Calculate the size needed to convert the strings to 319 wide characters. */ 320 memset (&ps, '\0', sizeof (ps)); 321 patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1; 322 if (__builtin_expect (patsize == 0, 0)) 323 /* Something wrong. 324 XXX Do we have to set `errno' to something which mbsrtows hasn't 325 already done? */ 326 return -1; 327 assert (mbsinit (&ps)); 328 strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1; 329 if (__builtin_expect (strsize == 0, 0)) 330 /* Something wrong. 331 XXX Do we have to set `errno' to something which mbsrtows hasn't 332 already done? */ 333 return -1; 334 assert (mbsinit (&ps)); 335 totsize = patsize + strsize; 336 if (__builtin_expect (! (patsize <= totsize 337 && totsize <= SIZE_MAX / sizeof (wchar_t)), 338 0)) 339 { 340 errno = ENOMEM; 341 return -1; 342 } 343 344 /* Allocate room for the wide characters. */ 345 if (__builtin_expect (totsize < ALLOCA_LIMIT, 1)) 346 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t)); 347 else 348 { 349 wpattern = malloc (totsize * sizeof (wchar_t)); 350 if (__builtin_expect (! wpattern, 0)) 351 { 352 errno = ENOMEM; 353 return -1; 354 } 355 } 356 wstring = wpattern + patsize; 357 358 /* Convert the strings into wide characters. */ 359 mbsrtowcs (wpattern, &pattern, patsize, &ps); 360 assert (mbsinit (&ps)); 361 mbsrtowcs (wstring, &string, strsize, &ps); 362 363 res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1, 364 flags & FNM_PERIOD, flags); 365 366 if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0)) 367 free (wpattern); 368 return res; 369 } 370# endif /* HANDLE_MULTIBYTE */ 371 372 return internal_fnmatch (pattern, string, string + strlen (string), 373 flags & FNM_PERIOD, flags); 374} 375 376# ifdef _LIBC 377# undef fnmatch 378versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3); 379# if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3) 380strong_alias (__fnmatch, __fnmatch_old) 381compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0); 382# endif 383libc_hidden_ver (__fnmatch, fnmatch) 384# endif 385 386#endif /* _LIBC or not __GNU_LIBRARY__. */ 387