Back to index

glibc  2.9
fnmatch.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2007
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #if HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 /* Enable GNU extensions in fnmatch.h.  */
00025 #ifndef _GNU_SOURCE
00026 # define _GNU_SOURCE 1
00027 #endif
00028 
00029 #include <assert.h>
00030 #include <errno.h>
00031 #include <fnmatch.h>
00032 #include <ctype.h>
00033 
00034 #if HAVE_STRING_H || defined _LIBC
00035 # include <string.h>
00036 #else
00037 # include <strings.h>
00038 #endif
00039 
00040 #if defined STDC_HEADERS || defined _LIBC
00041 # include <stdlib.h>
00042 #endif
00043 
00044 /* For platform which support the ISO C amendement 1 functionality we
00045    support user defined character classes.  */
00046 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
00047 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
00048 # include <wchar.h>
00049 # include <wctype.h>
00050 #endif
00051 
00052 /* We need some of the locale data (the collation sequence information)
00053    but there is no interface to get this information in general.  Therefore
00054    we support a correct implementation only in glibc.  */
00055 #ifdef _LIBC
00056 # include "../locale/localeinfo.h"
00057 # include "../locale/elem-hash.h"
00058 # include "../locale/coll-lookup.h"
00059 # include <shlib-compat.h>
00060 
00061 # define CONCAT(a,b) __CONCAT(a,b)
00062 # define mbsrtowcs __mbsrtowcs
00063 # define fnmatch __fnmatch
00064 extern int fnmatch (const char *pattern, const char *string, int flags);
00065 #endif
00066 
00067 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
00068 #define NO_LEADING_PERIOD(flags) \
00069   ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
00070 
00071 /* Comment out all this code if we are using the GNU C Library, and are not
00072    actually compiling the library itself.  This code is part of the GNU C
00073    Library, but also included in many other GNU distributions.  Compiling
00074    and linking in this code is a waste when using the GNU C library
00075    (especially if it is a shared library).  Rather than having every GNU
00076    program understand `configure --with-gnu-libc' and omit the object files,
00077    it is simpler to just do this in the source for each such file.  */
00078 
00079 #if defined _LIBC || !defined __GNU_LIBRARY__
00080 
00081 
00082 # if defined STDC_HEADERS || !defined isascii
00083 #  define ISASCII(c) 1
00084 # else
00085 #  define ISASCII(c) isascii(c)
00086 # endif
00087 
00088 # ifdef isblank
00089 #  define ISBLANK(c) (ISASCII (c) && isblank (c))
00090 # else
00091 #  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
00092 # endif
00093 # ifdef isgraph
00094 #  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
00095 # else
00096 #  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
00097 # endif
00098 
00099 # define ISPRINT(c) (ISASCII (c) && isprint (c))
00100 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
00101 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
00102 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
00103 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
00104 # define ISLOWER(c) (ISASCII (c) && islower (c))
00105 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
00106 # define ISSPACE(c) (ISASCII (c) && isspace (c))
00107 # define ISUPPER(c) (ISASCII (c) && isupper (c))
00108 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
00109 
00110 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
00111 
00112 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
00113 /* The GNU C library provides support for user-defined character classes
00114    and the functions from ISO C amendement 1.  */
00115 #  ifdef CHARCLASS_NAME_MAX
00116 #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
00117 #  else
00118 /* This shouldn't happen but some implementation might still have this
00119    problem.  Use a reasonable default value.  */
00120 #   define CHAR_CLASS_MAX_LENGTH 256
00121 #  endif
00122 
00123 #  ifdef _LIBC
00124 #   define IS_CHAR_CLASS(string) __wctype (string)
00125 #  else
00126 #   define IS_CHAR_CLASS(string) wctype (string)
00127 #  endif
00128 
00129 #  ifdef _LIBC
00130 #   define ISWCTYPE(WC, WT) __iswctype (WC, WT)
00131 #  else
00132 #   define ISWCTYPE(WC, WT) iswctype (WC, WT)
00133 #  endif
00134 
00135 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
00136 /* In this case we are implementing the multibyte character handling.  */
00137 #   define HANDLE_MULTIBYTE 1
00138 #  endif
00139 
00140 # else
00141 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
00142 
00143 #  define IS_CHAR_CLASS(string)                                             \
00144    (STREQ (string, "alpha") || STREQ (string, "upper")                      \
00145     || STREQ (string, "lower") || STREQ (string, "digit")                   \
00146     || STREQ (string, "alnum") || STREQ (string, "xdigit")                  \
00147     || STREQ (string, "space") || STREQ (string, "print")                   \
00148     || STREQ (string, "punct") || STREQ (string, "graph")                   \
00149     || STREQ (string, "cntrl") || STREQ (string, "blank"))
00150 # endif
00151 
00152 /* Avoid depending on library functions or files
00153    whose names are inconsistent.  */
00154 
00155 # if !defined _LIBC && !defined getenv
00156 extern char *getenv ();
00157 # endif
00158 
00159 # ifndef errno
00160 extern int errno;
00161 # endif
00162 
00163 /* Global variable.  */
00164 static int posixly_correct;
00165 
00166 /* This function doesn't exist on most systems.  */
00167 
00168 # if !defined HAVE___STRCHRNUL && !defined _LIBC
00169 static char *
00170 __strchrnul (s, c)
00171      const char *s;
00172      int c;
00173 {
00174   char *result = strchr (s, c);
00175   if (result == NULL)
00176     result = strchr (s, '\0');
00177   return result;
00178 }
00179 # endif
00180 
00181 # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
00182 static wchar_t *
00183 __wcschrnul (s, c)
00184      const wchar_t *s;
00185      wint_t c;
00186 {
00187   wchar_t *result = wcschr (s, c);
00188   if (result == NULL)
00189     result = wcschr (s, '\0');
00190   return result;
00191 }
00192 # endif
00193 
00194 # ifndef internal_function
00195 /* Inside GNU libc we mark some function in a special way.  In other
00196    environments simply ignore the marking.  */
00197 #  define internal_function
00198 # endif
00199 
00200 /* Note that this evaluates C many times.  */
00201 # ifdef _LIBC
00202 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
00203 # else
00204 #  define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
00205 # endif
00206 # define CHAR char
00207 # define UCHAR       unsigned char
00208 # define INT  int
00209 # define FCT  internal_fnmatch
00210 # define EXT  ext_match
00211 # define END  end_pattern
00212 # define STRUCT      fnmatch_struct
00213 # define L(CS)       CS
00214 # ifdef _LIBC
00215 #  define BTOWC(C)   __btowc (C)
00216 # else
00217 #  define BTOWC(C)   btowc (C)
00218 # endif
00219 # define STRLEN(S) strlen (S)
00220 # define STRCAT(D, S) strcat (D, S)
00221 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
00222 # define MEMCHR(S, C, N) memchr (S, C, N)
00223 # define STRCOLL(S1, S2) strcoll (S1, S2)
00224 # include "fnmatch_loop.c"
00225 
00226 
00227 # if HANDLE_MULTIBYTE
00228 /* Note that this evaluates C many times.  */
00229 #  ifdef _LIBC
00230 #   define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
00231 #  else
00232 #   define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
00233 #  endif
00234 #  define CHAR       wchar_t
00235 #  define UCHAR      wint_t
00236 #  define INT wint_t
00237 #  define FCT internal_fnwmatch
00238 #  define EXT ext_wmatch
00239 #  define END end_wpattern
00240 #  define STRUCT fnwmatch_struct
00241 #  define L(CS)      L##CS
00242 #  define BTOWC(C)   (C)
00243 #  define STRLEN(S) __wcslen (S)
00244 #  define STRCAT(D, S) __wcscat (D, S)
00245 #  define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
00246 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
00247 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
00248 #  define WIDE_CHAR_VERSION 1
00249 
00250 #  undef IS_CHAR_CLASS
00251 /* We have to convert the wide character string in a multibyte string.  But
00252    we know that the character class names consist of alphanumeric characters
00253    from the portable character set, and since the wide character encoding
00254    for a member of the portable character set is the same code point as
00255    its single-byte encoding, we can use a simplified method to convert the
00256    string to a multibyte character string.  */
00257 static wctype_t
00258 is_char_class (const wchar_t *wcs)
00259 {
00260   char s[CHAR_CLASS_MAX_LENGTH + 1];
00261   char *cp = s;
00262 
00263   do
00264     {
00265       /* Test for a printable character from the portable character set.  */
00266 #  ifdef _LIBC
00267       if (*wcs < 0x20 || *wcs > 0x7e
00268          || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
00269        return (wctype_t) 0;
00270 #  else
00271       switch (*wcs)
00272        {
00273        case L' ': case L'!': case L'"': case L'#': case L'%':
00274        case L'&': case L'\'': case L'(': case L')': case L'*':
00275        case L'+': case L',': case L'-': case L'.': case L'/':
00276        case L'0': case L'1': case L'2': case L'3': case L'4':
00277        case L'5': case L'6': case L'7': case L'8': case L'9':
00278        case L':': case L';': case L'<': case L'=': case L'>':
00279        case L'?':
00280        case L'A': case L'B': case L'C': case L'D': case L'E':
00281        case L'F': case L'G': case L'H': case L'I': case L'J':
00282        case L'K': case L'L': case L'M': case L'N': case L'O':
00283        case L'P': case L'Q': case L'R': case L'S': case L'T':
00284        case L'U': case L'V': case L'W': case L'X': case L'Y':
00285        case L'Z':
00286        case L'[': case L'\\': case L']': case L'^': case L'_':
00287        case L'a': case L'b': case L'c': case L'd': case L'e':
00288        case L'f': case L'g': case L'h': case L'i': case L'j':
00289        case L'k': case L'l': case L'm': case L'n': case L'o':
00290        case L'p': case L'q': case L'r': case L's': case L't':
00291        case L'u': case L'v': case L'w': case L'x': case L'y':
00292        case L'z': case L'{': case L'|': case L'}': case L'~':
00293          break;
00294        default:
00295          return (wctype_t) 0;
00296        }
00297 #  endif
00298 
00299       /* Avoid overrunning the buffer.  */
00300       if (cp == s + CHAR_CLASS_MAX_LENGTH)
00301        return (wctype_t) 0;
00302 
00303       *cp++ = (char) *wcs++;
00304     }
00305   while (*wcs != L'\0');
00306 
00307   *cp = '\0';
00308 
00309 #  ifdef _LIBC
00310   return __wctype (s);
00311 #  else
00312   return wctype (s);
00313 #  endif
00314 }
00315 #  define IS_CHAR_CLASS(string) is_char_class (string)
00316 
00317 #  include "fnmatch_loop.c"
00318 # endif
00319 
00320 
00321 int
00322 fnmatch (pattern, string, flags)
00323      const char *pattern;
00324      const char *string;
00325      int flags;
00326 {
00327 # if HANDLE_MULTIBYTE
00328   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
00329     {
00330       mbstate_t ps;
00331       size_t n;
00332       const char *p;
00333       wchar_t *wpattern;
00334       wchar_t *wstring;
00335 
00336       /* Convert the strings into wide characters.  */
00337       memset (&ps, '\0', sizeof (ps));
00338       p = pattern;
00339 #ifdef _LIBC
00340       n = strnlen (pattern, 1024);
00341 #else
00342       n = strlen (pattern);
00343 #endif
00344       if (__builtin_expect (n < 1024, 1))
00345        {
00346          wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
00347          n = mbsrtowcs (wpattern, &p, n + 1, &ps);
00348          if (__builtin_expect (n == (size_t) -1, 0))
00349            /* Something wrong.
00350               XXX Do we have to set `errno' to something which mbsrtows hasn't
00351               already done?  */
00352            return -1;
00353          if (p)
00354            {
00355              memset (&ps, '\0', sizeof (ps));
00356              goto prepare_wpattern;
00357            }
00358        }
00359       else
00360        {
00361        prepare_wpattern:
00362          n = mbsrtowcs (NULL, &pattern, 0, &ps);
00363          if (__builtin_expect (n == (size_t) -1, 0))
00364            /* Something wrong.
00365               XXX Do we have to set `errno' to something which mbsrtows hasn't
00366               already done?  */
00367            return -1;
00368          wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
00369          assert (mbsinit (&ps));
00370          (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
00371        }
00372 
00373       assert (mbsinit (&ps));
00374 #ifdef _LIBC
00375       n = strnlen (string, 1024);
00376 #else
00377       n = strlen (string);
00378 #endif
00379       p = string;
00380       if (__builtin_expect (n < 1024, 1))
00381        {
00382          wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
00383          n = mbsrtowcs (wstring, &p, n + 1, &ps);
00384          if (__builtin_expect (n == (size_t) -1, 0))
00385            /* Something wrong.
00386               XXX Do we have to set `errno' to something which mbsrtows hasn't
00387               already done?  */
00388            return -1;
00389          if (p)
00390            {
00391              memset (&ps, '\0', sizeof (ps));
00392              goto prepare_wstring;
00393            }
00394        }
00395       else
00396        {
00397        prepare_wstring:
00398          n = mbsrtowcs (NULL, &string, 0, &ps);
00399          if (__builtin_expect (n == (size_t) -1, 0))
00400            /* Something wrong.
00401               XXX Do we have to set `errno' to something which mbsrtows hasn't
00402               already done?  */
00403            return -1;
00404          wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
00405          assert (mbsinit (&ps));
00406          (void) mbsrtowcs (wstring, &string, n + 1, &ps);
00407        }
00408 
00409       return internal_fnwmatch (wpattern, wstring, wstring + n,
00410                             flags & FNM_PERIOD, flags, NULL);
00411     }
00412 # endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
00413 
00414   return internal_fnmatch (pattern, string, string + strlen (string),
00415                         flags & FNM_PERIOD, flags, NULL);
00416 }
00417 
00418 # ifdef _LIBC
00419 #  undef fnmatch
00420 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
00421 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
00422 strong_alias (__fnmatch, __fnmatch_old)
00423 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
00424 #  endif
00425 libc_hidden_ver (__fnmatch, fnmatch)
00426 # endif
00427 
00428 #endif /* _LIBC or not __GNU_LIBRARY__.  */