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
32 #if HAVE_STRING_H || defined _LIBC
38 #if defined STDC_HEADERS || defined _LIBC
42 /* For platform which support the ISO C amendement 1 functionality we
43 support user defined character classes. */
44 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
45 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
50 /* Comment out all this code if we are using the GNU C Library, and are not
51 actually compiling the library itself. This code is part of the GNU C
52 Library, but also included in many other GNU distributions. Compiling
53 and linking in this code is a waste when using the GNU C library
54 (especially if it is a shared library). Rather than having every GNU
55 program understand `configure --with-gnu-libc' and omit the object files,
56 it is simpler to just do this in the source for each such file. */
58 #if defined _LIBC || !defined __GNU_LIBRARY__
61 # if defined STDC_HEADERS || !defined isascii
64 # define ISASCII(c) isascii(c)
68 # define ISBLANK(c) (ISASCII (c) && isblank (c))
70 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
73 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
75 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
78 #define ISPRINT(c) (ISASCII (c) && isprint (c))
79 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
80 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
81 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
82 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
83 #define ISLOWER(c) (ISASCII (c) && islower (c))
84 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
85 #define ISSPACE(c) (ISASCII (c) && isspace (c))
86 #define ISUPPER(c) (ISASCII (c) && isupper (c))
87 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
89 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
91 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
92 /* The GNU C library provides support for user-defined character classes
93 and the functions from ISO C amendement 1. */
94 # ifdef CHARCLASS_NAME_MAX
95 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
97 /* This shouldn't happen but some implementation might still have this
98 problem. Use a reasonable default value. */
99 # define CHAR_CLASS_MAX_LENGTH 256
103 # define IS_CHAR_CLASS(string) __wctype (string)
105 # define IS_CHAR_CLASS(string) wctype (string)
108 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
110 # define IS_CHAR_CLASS(string) \
111 (STREQ (string, "alpha") || STREQ (string, "upper") \
112 || STREQ (string, "lower") || STREQ (string, "digit") \
113 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
114 || STREQ (string, "space") || STREQ (string, "print") \
115 || STREQ (string, "punct") || STREQ (string, "graph") \
116 || STREQ (string, "cntrl") || STREQ (string, "blank"))
119 /* Avoid depending on library functions or files
120 whose names are inconsistent. */
122 # if !defined _LIBC && !defined getenv
123 extern char *getenv ();
130 /* Match STRING against the filename pattern PATTERN, returning zero if
131 it matches, nonzero if not. */
136 internal_fnmatch (const char *pattern
, const char *string
,
137 int no_leading_period
, int flags
)
139 register const char *p
= pattern
, *n
= string
;
140 register unsigned char c
;
142 /* Note that this evaluates C many times. */
144 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
146 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
149 while ((c
= *p
++) != '\0')
158 else if (*n
== '/' && (flags
& FNM_FILE_NAME
))
160 else if (*n
== '.' && no_leading_period
162 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
167 if (!(flags
& FNM_NOESCAPE
))
171 /* Trailing \ loses. */
175 if (FOLD ((unsigned char) *n
) != c
)
180 if (*n
== '.' && no_leading_period
182 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
185 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
187 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
188 /* A slash does not match a wildcard under FNM_FILE_NAME. */
192 /* A ? needs to match one character. */
194 /* There isn't another character; no match. */
197 /* One character of the string is consumed in matching
198 this ? wildcard, so *??? won't match if there are
199 less than three characters. */
205 /* The wildcard(s) is/are the last element of the pattern.
206 If the name is a file name and contains another slash
207 this does mean it cannot match. */
208 return ((flags
& FNM_FILE_NAME
) && strchr (n
, '/') != NULL
214 endp
= __strchrnul (n
, (flags
& FNM_FILE_NAME
) ? '/' : '\0');
218 int flags2
= ((flags
& FNM_FILE_NAME
)
219 ? flags
: (flags
& ~FNM_PERIOD
));
221 for (--p
; n
< endp
; ++n
)
222 if (internal_fnmatch (p
, n
,
232 else if (c
== '/' && (flags
& FNM_FILE_NAME
))
234 while (*n
!= '\0' && *n
!= '/')
237 && (internal_fnmatch (p
, n
+ 1, flags
& FNM_PERIOD
,
243 int flags2
= ((flags
& FNM_FILE_NAME
)
244 ? flags
: (flags
& ~FNM_PERIOD
));
246 if (c
== '\\' && !(flags
& FNM_NOESCAPE
))
249 for (--p
; n
< endp
; ++n
)
250 if (FOLD ((unsigned char) *n
) == c
251 && (internal_fnmatch (p
, n
,
262 /* If we come here no match is possible with the wildcard. */
267 /* Nonzero if the sense of the character class is inverted. */
268 static int posixly_correct
;
272 if (posixly_correct
== 0)
273 posixly_correct
= getenv ("POSIXLY_CORRECT") != NULL
? 1 : -1;
278 if (*n
== '.' && no_leading_period
&& (n
== string
284 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
285 /* `/' cannot be matched. */
288 not = (*p
== '!' || (posixly_correct
< 0 && *p
== '^'));
295 unsigned char fn
= FOLD ((unsigned char) *n
);
297 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
301 c
= FOLD ((unsigned char) *p
);
307 else if (c
== '[' && *p
== ':')
309 /* Leave room for the null. */
310 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
312 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
315 const char *startp
= p
;
319 if (c1
== CHAR_CLASS_MAX_LENGTH
)
320 /* The name is too long and therefore the pattern
325 if (c
== ':' && p
[1] == ']')
330 if (c
< 'a' || c
>= 'z')
332 /* This cannot possibly be a character class name.
333 Match it as a normal range. */
342 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
343 wt
= IS_CHAR_CLASS (str
);
345 /* Invalid character class name. */
348 if (__iswctype (__btowc ((unsigned char) *n
), wt
))
351 if ((STREQ (str
, "alnum") && ISALNUM ((unsigned char) *n
))
352 || (STREQ (str
, "alpha") && ISALPHA ((unsigned char) *n
))
353 || (STREQ (str
, "blank") && ISBLANK ((unsigned char) *n
))
354 || (STREQ (str
, "cntrl") && ISCNTRL ((unsigned char) *n
))
355 || (STREQ (str
, "digit") && ISDIGIT ((unsigned char) *n
))
356 || (STREQ (str
, "graph") && ISGRAPH ((unsigned char) *n
))
357 || (STREQ (str
, "lower") && ISLOWER ((unsigned char) *n
))
358 || (STREQ (str
, "print") && ISPRINT ((unsigned char) *n
))
359 || (STREQ (str
, "punct") && ISPUNCT ((unsigned char) *n
))
360 || (STREQ (str
, "space") && ISSPACE ((unsigned char) *n
))
361 || (STREQ (str
, "upper") && ISUPPER ((unsigned char) *n
))
362 || (STREQ (str
, "xdigit") && ISXDIGIT ((unsigned char) *n
)))
367 /* [ (unterminated) loses. */
378 if (c
== '-' && *p
!= ']')
381 unsigned char cend
= *p
++;
382 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
387 if (cold
<= fn
&& fn
<= FOLD (cend
))
403 /* Skip the rest of the [...] that already matched. */
407 /* [... (unterminated) loses. */
411 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
415 /* XXX 1003.2d11 is unclear if this is right. */
418 else if (c
== '[' && *p
== ':')
423 while (*p
!= ':' || p
[1] == ']');
434 if (c
!= FOLD ((unsigned char) *n
))
444 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
445 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
455 fnmatch (pattern
, string
, flags
)
460 return internal_fnmatch (pattern
, string
, flags
& FNM_PERIOD
, flags
);
463 #endif /* _LIBC or not __GNU_LIBRARY__. */