4 /* ----- THE FOLLOWING UP TO 'END' is glibc-2.1.2 posix/fnmatch.c
5 except for the part with the '#if 0' */
7 /* Copyright (C) 1991, 92, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
8 This file is part of the GNU C Library.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this library; see the file COPYING.LIB. If not,
22 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
29 /* Enable GNU extensions in fnmatch.h. */
31 # define _GNU_SOURCE 1
38 #if HAVE_STRING_H || defined _LIBC
44 #if defined STDC_HEADERS || defined _LIBC
48 /* For platform which support the ISO C amendement 1 functionality we
49 support user defined character classes. */
50 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
51 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
56 /* Comment out all this code if we are using the GNU C Library, and are not
57 actually compiling the library itself. This code is part of the GNU C
58 Library, but also included in many other GNU distributions. Compiling
59 and linking in this code is a waste when using the GNU C library
60 (especially if it is a shared library). Rather than having every GNU
61 program understand `configure --with-gnu-libc' and omit the object files,
62 it is simpler to just do this in the source for each such file. */
64 #if defined _LIBC || !defined __GNU_LIBRARY__
67 # if defined STDC_HEADERS || !defined isascii
70 # define ISASCII(c) isascii(c)
74 # define ISBLANK(c) (ISASCII (c) && isblank (c))
76 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
79 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
81 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
84 #define ISPRINT(c) (ISASCII (c) && isprint (c))
85 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
86 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
87 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
88 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
89 #define ISLOWER(c) (ISASCII (c) && islower (c))
90 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
91 #define ISSPACE(c) (ISASCII (c) && isspace (c))
92 #define ISUPPER(c) (ISASCII (c) && isupper (c))
93 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
95 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
97 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
98 /* The GNU C library provides support for user-defined character classes
99 and the functions from ISO C amendement 1. */
100 # ifdef CHARCLASS_NAME_MAX
101 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
103 /* This shouldn't happen but some implementation might still have this
104 problem. Use a reasonable default value. */
105 # define CHAR_CLASS_MAX_LENGTH 256
109 # define IS_CHAR_CLASS(string) __wctype (string)
111 # define IS_CHAR_CLASS(string) wctype (string)
114 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
116 # define IS_CHAR_CLASS(string) \
117 (STREQ (string, "alpha") || STREQ (string, "upper") \
118 || STREQ (string, "lower") || STREQ (string, "digit") \
119 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
120 || STREQ (string, "space") || STREQ (string, "print") \
121 || STREQ (string, "punct") || STREQ (string, "graph") \
122 || STREQ (string, "cntrl") || STREQ (string, "blank"))
125 /* Avoid depending on library functions or files
126 whose names are inconsistent. */
128 # if !defined _LIBC && !defined getenv
129 extern char *getenv ();
136 /* Match STRING against the filename pattern PATTERN, returning zero if
137 it matches, nonzero if not. */
142 internal_fnmatch (const char *pattern, const char *string,
143 int no_leading_period, int flags)
145 register const char *p = pattern, *n = string;
146 register unsigned char c;
148 /* Note that this evaluates C many times. */
150 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
152 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
155 while ((c = *p++) != '\0')
164 else if (*n == '/' && (flags & FNM_FILE_NAME))
166 else if (*n == '.' && no_leading_period
168 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
173 if (!(flags & FNM_NOESCAPE))
177 /* Trailing \ loses. */
181 if (FOLD ((unsigned char) *n) != c)
186 if (*n == '.' && no_leading_period
188 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
191 for (c = *p++; c == '?' || c == '*'; c = *p++)
193 if (*n == '/' && (flags & FNM_FILE_NAME))
194 /* A slash does not match a wildcard under FNM_FILE_NAME. */
198 /* A ? needs to match one character. */
200 /* There isn't another character; no match. */
203 /* One character of the string is consumed in matching
204 this ? wildcard, so *??? won't match if there are
205 less than three characters. */
211 /* The wildcard(s) is/are the last element of the pattern.
212 If the name is a file name and contains another slash
213 this does mean it cannot match. */
214 return ((flags & FNM_FILE_NAME) && strchr (n, '/') != NULL
221 endp = __strchrnul (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
223 /* replace call to internal glibc function with equivalent */
224 if (!(flags & FNM_FILE_NAME) || ((endp = strchr(n, '/')) == NULL))
225 endp = n + strlen(n);
230 int flags2 = ((flags & FNM_FILE_NAME)
231 ? flags : (flags & ~FNM_PERIOD));
233 for (--p; n < endp; ++n)
234 if (internal_fnmatch (p, n,
244 else if (c == '/' && (flags & FNM_FILE_NAME))
246 while (*n != '\0' && *n != '/')
249 && (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
255 int flags2 = ((flags & FNM_FILE_NAME)
256 ? flags : (flags & ~FNM_PERIOD));
258 if (c == '\\' && !(flags & FNM_NOESCAPE))
261 for (--p; n < endp; ++n)
262 if (FOLD ((unsigned char) *n) == c
263 && (internal_fnmatch (p, n,
274 /* If we come here no match is possible with the wildcard. */
279 /* Nonzero if the sense of the character class is inverted. */
280 static int posixly_correct;
284 if (posixly_correct == 0)
285 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
290 if (*n == '.' && no_leading_period && (n == string
296 if (*n == '/' && (flags & FNM_FILE_NAME))
297 /* `/' cannot be matched. */
300 not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
307 unsigned char fn = FOLD ((unsigned char) *n);
309 if (!(flags & FNM_NOESCAPE) && c == '\\')
313 c = FOLD ((unsigned char) *p);
319 else if (c == '[' && *p == ':')
321 /* Leave room for the null. */
322 char str[CHAR_CLASS_MAX_LENGTH + 1];
324 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
327 const char *startp = p;
331 if (c1 == CHAR_CLASS_MAX_LENGTH)
332 /* The name is too long and therefore the pattern
337 if (c == ':' && p[1] == ']')
342 if (c < 'a' || c >= 'z')
344 /* This cannot possibly be a character class name.
345 Match it as a normal range. */
354 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
355 wt = IS_CHAR_CLASS (str);
357 /* Invalid character class name. */
360 if (__iswctype (__btowc ((unsigned char) *n), wt))
363 if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
364 || (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
365 || (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
366 || (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
367 || (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
368 || (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
369 || (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
370 || (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
371 || (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
372 || (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
373 || (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
374 || (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
379 /* [ (unterminated) loses. */
390 if (c == '-' && *p != ']')
393 unsigned char cend = *p++;
394 if (!(flags & FNM_NOESCAPE) && cend == '\\')
399 if (cold <= fn && fn <= FOLD (cend))
415 /* Skip the rest of the [...] that already matched. */
419 /* [... (unterminated) loses. */
423 if (!(flags & FNM_NOESCAPE) && c == '\\')
427 /* XXX 1003.2d11 is unclear if this is right. */
430 else if (c == '[' && *p == ':')
435 while (*p != ':' || p[1] == ']');
446 if (c != FOLD ((unsigned char) *n))
456 if ((flags & FNM_LEADING_DIR) && *n == '/')
457 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
467 fnmatch (pattern, string, flags)
472 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
475 #endif /* _LIBC or not __GNU_LIBRARY__. */
476 /* ----- END glibc-2.1.2 posix/fnmatch.c */
478 #else /* HAVE_FNMATCH */
479 void fnmatch_dummy(void) {}