1 /* Copyright (C) 1991, 92, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
23 /* Enable GNU extensions in fnmatch.h. */
25 # define _GNU_SOURCE 1
33 #if HAVE_STRING_H || defined _LIBC
39 #if defined STDC_HEADERS || defined _LIBC
43 /* For platforms which support the ISO C amendment 1 functionality we
44 support user defined character classes. */
45 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
46 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
51 /* Comment out all this code if we are using the GNU C Library, and are not
52 actually compiling the library itself. This code is part of the GNU C
53 Library, but also included in many other GNU distributions. Compiling
54 and linking in this code is a waste when using the GNU C library
55 (especially if it is a shared library). Rather than having every GNU
56 program understand `configure --with-gnu-libc' and omit the object files,
57 it is simpler to just do this in the source for each such file. */
59 #if defined NO_FNMATCH || defined NO_FNMATCH_CASEFOLD || \
60 defined _LIBC || !defined __GNU_LIBRARY__
63 # if defined STDC_HEADERS || !defined isascii
66 # define ISASCII(c) isascii(c)
70 # define ISBLANK(c) (ISASCII (c) && isblank (c))
72 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
75 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
77 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
80 # define ISPRINT(c) (ISASCII (c) && isprint (c))
81 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
82 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
83 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
84 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
85 # define ISLOWER(c) (ISASCII (c) && islower (c))
86 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
87 # define ISSPACE(c) (ISASCII (c) && isspace (c))
88 # define ISUPPER(c) (ISASCII (c) && isupper (c))
89 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
91 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
93 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
94 /* The GNU C library provides support for user-defined character classes
95 and the functions from ISO C amendment 1. */
96 # ifdef CHARCLASS_NAME_MAX
97 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
99 /* This shouldn't happen but some implementation might still have this
100 problem. Use a reasonable default value. */
101 # define CHAR_CLASS_MAX_LENGTH 256
105 # define IS_CHAR_CLASS(string) __wctype (string)
107 # define IS_CHAR_CLASS(string) wctype (string)
110 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
112 # define IS_CHAR_CLASS(string) \
113 (STREQ (string, "alpha") || STREQ (string, "upper") \
114 || STREQ (string, "lower") || STREQ (string, "digit") \
115 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
116 || STREQ (string, "space") || STREQ (string, "print") \
117 || STREQ (string, "punct") || STREQ (string, "graph") \
118 || STREQ (string, "cntrl") || STREQ (string, "blank"))
121 /* Avoid depending on library functions or files
122 whose names are inconsistent. */
124 # if !defined _LIBC && !defined getenv
125 extern char *getenv (const char *name
);
136 /* This function doesn't exist on most systems. */
138 # if !defined HAVE___STRCHRNUL && !defined _LIBC
140 __strchrnul (const char *s
, int c
)
144 char *result
= strchr (s
, c
);
146 result
= strchr (s
, '\0');
151 # ifndef internal_function
152 /* Inside GNU libc we mark some function in a special way. In other
153 environments simply ignore the marking. */
154 # define internal_function
157 /* Match STRING against the filename pattern PATTERN, returning zero if
158 it matches, nonzero if not. */
159 static int internal_fnmatch
__P ((const char *pattern
, const char *string
,
160 int no_leading_period
, int flags
))
164 internal_fnmatch (const char *pattern
, const char *string
, int no_leading_period
, int flags
)
170 register const char *p
= pattern
, *n
= string
;
171 register unsigned char c
;
173 /* Note that this evaluates C many times. */
175 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
177 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
180 while ((c
= *p
++) != '\0')
189 else if (*n
== '/' && (flags
& FNM_FILE_NAME
))
191 else if (*n
== '.' && no_leading_period
193 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
198 if (!(flags
& FNM_NOESCAPE
))
202 /* Trailing \ loses. */
206 if (FOLD ((unsigned char) *n
) != c
)
211 if (*n
== '.' && no_leading_period
213 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
216 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
218 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
219 /* A slash does not match a wildcard under FNM_FILE_NAME. */
223 /* A ? needs to match one character. */
225 /* There isn't another character; no match. */
228 /* One character of the string is consumed in matching
229 this ? wildcard, so *??? won't match if there are
230 less than three characters. */
236 /* The wildcard(s) is/are the last element of the pattern.
237 If the name is a file name and contains another slash
238 this does mean it cannot match. */
239 return ((flags
& FNM_FILE_NAME
) && strchr (n
, '/') != NULL
245 endp
= __strchrnul (n
, (flags
& FNM_FILE_NAME
) ? '/' : '\0');
249 int flags2
= ((flags
& FNM_FILE_NAME
)
250 ? flags
: (flags
& ~FNM_PERIOD
));
252 for (--p
; n
< endp
; ++n
)
253 if (internal_fnmatch (p
, n
,
263 else if (c
== '/' && (flags
& FNM_FILE_NAME
))
265 while (*n
!= '\0' && *n
!= '/')
268 && (internal_fnmatch (p
, n
+ 1, flags
& FNM_PERIOD
,
274 int flags2
= ((flags
& FNM_FILE_NAME
)
275 ? flags
: (flags
& ~FNM_PERIOD
));
277 if (c
== '\\' && !(flags
& FNM_NOESCAPE
))
280 for (--p
; n
< endp
; ++n
)
281 if (FOLD ((unsigned char) *n
) == c
282 && (internal_fnmatch (p
, n
,
293 /* If we come here no match is possible with the wildcard. */
298 /* Nonzero if the sense of the character class is inverted. */
299 static int posixly_correct
;
303 if (posixly_correct
== 0)
304 posixly_correct
= getenv ("POSIXLY_CORRECT") != NULL
? 1 : -1;
309 if (*n
== '.' && no_leading_period
&& (n
== string
315 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
316 /* `/' cannot be matched. */
319 not = (*p
== '!' || (posixly_correct
< 0 && *p
== '^'));
326 unsigned char fn
= FOLD ((unsigned char) *n
);
328 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
332 c
= FOLD ((unsigned char) *p
);
338 else if (c
== '[' && *p
== ':')
340 /* Leave room for the null. */
341 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
343 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
346 const char *startp
= p
;
350 if (c1
> CHAR_CLASS_MAX_LENGTH
)
351 /* The name is too long and therefore the pattern
356 if (c
== ':' && p
[1] == ']')
361 if (c
< 'a' || c
>= 'z')
363 /* This cannot possibly be a character class name.
364 Match it as a normal range. */
373 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
374 wt
= IS_CHAR_CLASS (str
);
376 /* Invalid character class name. */
379 if (__iswctype (__btowc ((unsigned char) *n
), wt
))
382 if ((STREQ (str
, "alnum") && ISALNUM ((unsigned char) *n
))
383 || (STREQ (str
, "alpha") && ISALPHA ((unsigned char) *n
))
384 || (STREQ (str
, "blank") && ISBLANK ((unsigned char) *n
))
385 || (STREQ (str
, "cntrl") && ISCNTRL ((unsigned char) *n
))
386 || (STREQ (str
, "digit") && ISDIGIT ((unsigned char) *n
))
387 || (STREQ (str
, "graph") && ISGRAPH ((unsigned char) *n
))
388 || (STREQ (str
, "lower") && ISLOWER ((unsigned char) *n
))
389 || (STREQ (str
, "print") && ISPRINT ((unsigned char) *n
))
390 || (STREQ (str
, "punct") && ISPUNCT ((unsigned char) *n
))
391 || (STREQ (str
, "space") && ISSPACE ((unsigned char) *n
))
392 || (STREQ (str
, "upper") && ISUPPER ((unsigned char) *n
))
393 || (STREQ (str
, "xdigit") && ISXDIGIT ((unsigned char) *n
)))
398 /* [ (unterminated) loses. */
409 if (c
== '-' && *p
!= ']')
412 unsigned char cend
= *p
++;
413 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
418 if (cold
<= fn
&& fn
<= FOLD (cend
))
434 /* Skip the rest of the [...] that already matched. */
438 /* [... (unterminated) loses. */
442 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
446 /* XXX 1003.2d11 is unclear if this is right. */
449 else if (c
== '[' && *p
== ':')
454 while (*p
!= ':' || p
[1] == ']');
465 if (c
!= FOLD ((unsigned char) *n
))
475 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
476 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
486 fnmatch (const char *pattern
, const char *string
, int flags
)
491 return internal_fnmatch (pattern
, string
, flags
& FNM_PERIOD
, flags
);
494 #endif /* _LIBC or not __GNU_LIBRARY__. */