4 /* ----- THE FOLLOWING UP TO 'END' is glibc-2.1.2 posix/fnmatch.c
5 except for the parts with '#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. */
25 #if 0 /* header files included better by ../rsync.h */
31 /* Enable GNU extensions in fnmatch.h. */
33 # define _GNU_SOURCE 1
40 #if HAVE_STRING_H || defined _LIBC
46 #if defined STDC_HEADERS || defined _LIBC
52 /* For platform which support the ISO C amendement 1 functionality we
53 support user defined character classes. */
54 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
55 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
60 /* Comment out all this code if we are using the GNU C Library, and are not
61 actually compiling the library itself. This code is part of the GNU C
62 Library, but also included in many other GNU distributions. Compiling
63 and linking in this code is a waste when using the GNU C library
64 (especially if it is a shared library). Rather than having every GNU
65 program understand `configure --with-gnu-libc' and omit the object files,
66 it is simpler to just do this in the source for each such file. */
68 #if defined _LIBC || !defined __GNU_LIBRARY__
71 # if defined STDC_HEADERS || !defined isascii
74 # define ISASCII(c) isascii(c)
78 # define ISBLANK(c) (ISASCII (c) && isblank (c))
80 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
83 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
85 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
88 #define ISPRINT(c) (ISASCII (c) && isprint (c))
89 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
90 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
91 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
92 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
93 #define ISLOWER(c) (ISASCII (c) && islower (c))
94 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
95 #define ISSPACE(c) (ISASCII (c) && isspace (c))
96 #define ISUPPER(c) (ISASCII (c) && isupper (c))
97 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
99 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
101 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
102 /* The GNU C library provides support for user-defined character classes
103 and the functions from ISO C amendement 1. */
104 # ifdef CHARCLASS_NAME_MAX
105 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
107 /* This shouldn't happen but some implementation might still have this
108 problem. Use a reasonable default value. */
109 # define CHAR_CLASS_MAX_LENGTH 256
113 # define IS_CHAR_CLASS(string) __wctype (string)
115 # define IS_CHAR_CLASS(string) wctype (string)
118 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
120 # define IS_CHAR_CLASS(string) \
121 (STREQ (string, "alpha") || STREQ (string, "upper") \
122 || STREQ (string, "lower") || STREQ (string, "digit") \
123 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
124 || STREQ (string, "space") || STREQ (string, "print") \
125 || STREQ (string, "punct") || STREQ (string, "graph") \
126 || STREQ (string, "cntrl") || STREQ (string, "blank"))
129 /* Avoid depending on library functions or files
130 whose names are inconsistent. */
132 # if !defined _LIBC && !defined getenv
133 extern char *getenv ();
140 /* Match STRING against the filename pattern PATTERN, returning zero if
141 it matches, nonzero if not. */
146 internal_fnmatch (const char *pattern, const char *string,
147 int no_leading_period, int flags)
149 register const char *p = pattern, *n = string;
150 register unsigned char c;
152 /* Note that this evaluates C many times. */
154 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
156 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
159 while ((c = *p++) != '\0')
168 else if (*n == '/' && (flags & FNM_FILE_NAME))
170 else if (*n == '.' && no_leading_period
172 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
177 if (!(flags & FNM_NOESCAPE))
181 /* Trailing \ loses. */
185 if (FOLD ((unsigned char) *n) != c)
190 if (*n == '.' && no_leading_period
192 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
195 for (c = *p++; c == '?' || c == '*'; c = *p++)
197 if (*n == '/' && (flags & FNM_FILE_NAME))
198 /* A slash does not match a wildcard under FNM_FILE_NAME. */
202 /* A ? needs to match one character. */
204 /* There isn't another character; no match. */
207 /* One character of the string is consumed in matching
208 this ? wildcard, so *??? won't match if there are
209 less than three characters. */
215 /* The wildcard(s) is/are the last element of the pattern.
216 If the name is a file name and contains another slash
217 this does mean it cannot match. */
218 return ((flags & FNM_FILE_NAME) && strchr (n, '/') != NULL
225 endp = __strchrnul (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
227 /* replace call to internal glibc function with equivalent */
228 if (!(flags & FNM_FILE_NAME) || ((endp = strchr(n, '/')) == NULL))
229 endp = n + strlen(n);
234 int flags2 = ((flags & FNM_FILE_NAME)
235 ? flags : (flags & ~FNM_PERIOD));
237 for (--p; n < endp; ++n)
238 if (internal_fnmatch (p, n,
248 else if (c == '/' && (flags & FNM_FILE_NAME))
250 while (*n != '\0' && *n != '/')
253 && (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
259 int flags2 = ((flags & FNM_FILE_NAME)
260 ? flags : (flags & ~FNM_PERIOD));
262 if (c == '\\' && !(flags & FNM_NOESCAPE))
265 for (--p; n < endp; ++n)
266 if (FOLD ((unsigned char) *n) == c
267 && (internal_fnmatch (p, n,
278 /* If we come here no match is possible with the wildcard. */
283 /* Nonzero if the sense of the character class is inverted. */
284 static int posixly_correct;
288 if (posixly_correct == 0)
289 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
294 if (*n == '.' && no_leading_period && (n == string
300 if (*n == '/' && (flags & FNM_FILE_NAME))
301 /* `/' cannot be matched. */
304 not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
311 unsigned char fn = FOLD ((unsigned char) *n);
313 if (!(flags & FNM_NOESCAPE) && c == '\\')
317 c = FOLD ((unsigned char) *p);
323 else if (c == '[' && *p == ':')
325 /* Leave room for the null. */
326 char str[CHAR_CLASS_MAX_LENGTH + 1];
328 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
331 const char *startp = p;
335 if (c1 == CHAR_CLASS_MAX_LENGTH)
336 /* The name is too long and therefore the pattern
341 if (c == ':' && p[1] == ']')
346 if (c < 'a' || c >= 'z')
348 /* This cannot possibly be a character class name.
349 Match it as a normal range. */
358 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
359 wt = IS_CHAR_CLASS (str);
361 /* Invalid character class name. */
364 if (__iswctype (__btowc ((unsigned char) *n), wt))
367 if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
368 || (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
369 || (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
370 || (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
371 || (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
372 || (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
373 || (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
374 || (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
375 || (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
376 || (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
377 || (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
378 || (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
383 /* [ (unterminated) loses. */
394 if (c == '-' && *p != ']')
397 unsigned char cend = *p++;
398 if (!(flags & FNM_NOESCAPE) && cend == '\\')
403 if (cold <= fn && fn <= FOLD (cend))
419 /* Skip the rest of the [...] that already matched. */
423 /* [... (unterminated) loses. */
427 if (!(flags & FNM_NOESCAPE) && c == '\\')
431 /* XXX 1003.2d11 is unclear if this is right. */
434 else if (c == '[' && *p == ':')
439 while (*p != ':' || p[1] == ']');
450 if (c != FOLD ((unsigned char) *n))
460 if ((flags & FNM_LEADING_DIR) && *n == '/')
461 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
471 fnmatch (pattern, string, flags)
476 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
479 #endif /* _LIBC or not __GNU_LIBRARY__. */
480 /* ----- END glibc-2.1.2 posix/fnmatch.c */
482 #else /* HAVE_FNMATCH */
483 void fnmatch_dummy(void) {}