3 /* Copyright 1991, 1992, 1993, 1996, 1997, 2000 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Enable GNU extensions in fnmatch.h. */
25 # define _GNU_SOURCE 1
32 #if defined STDC_HEADERS || !defined isascii
33 # define IN_CTYPE_DOMAIN(c) 1
35 # define IN_CTYPE_DOMAIN(c) isascii (c)
38 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
45 /* Match STRING against the filename pattern PATTERN, returning zero if
46 it matches, nonzero if not. */
48 fnmatch (const char *pattern
, const char *string
, int flags
)
50 register const char *p
= pattern
, *n
= string
;
53 /* Note that this evaluates C many times. */
54 #define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER ((unsigned char) (c)) \
55 ? tolower ((unsigned char) (c)) \
58 while ((c
= *p
++) != '\0')
67 else if ((flags
& FNM_FILE_NAME
) && *n
== '/')
69 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
70 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
75 if (!(flags
& FNM_NOESCAPE
))
79 /* Trailing \ loses. */
88 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
89 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
92 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
96 /* A ? needs to match one character. */
97 if (*n
== '\0' || (*n
== '/' && (flags
& FNM_FILE_NAME
)))
98 /* There isn't another character; no match. */
101 /* One character of the string is consumed in matching
102 this ? wildcard, so *??? won't match if there are
103 less than three characters. */
110 if ((flags
& (FNM_FILE_NAME
| FNM_LEADING_DIR
)) == FNM_FILE_NAME
)
111 for (; *n
!= '\0'; n
++)
118 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
120 for (--p
; *n
!= '\0'; ++n
)
121 if ((c
== '[' || FOLD (*n
) == c1
) &&
122 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
124 else if (*n
== '/' && (flags
& FNM_FILE_NAME
))
131 /* Nonzero if the sense of the character class is inverted. */
137 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
138 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
141 not = (*p
== '!' || *p
== '^');
148 register char cstart
= c
, cend
= c
;
150 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
154 cstart
= cend
= *p
++;
157 cstart
= cend
= FOLD (cstart
);
160 /* [ (unterminated) loses. */
166 if ((flags
& FNM_FILE_NAME
) && c
== '/')
167 /* [/] can never match. */
170 if (c
== '-' && *p
!= ']')
173 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
182 if (FOLD (*n
) >= cstart
&& FOLD (*n
) <= cend
)
193 /* Skip the rest of the [...] that already matched. */
197 /* [... (unterminated) loses. */
201 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
205 /* XXX 1003.2d11 is unclear if this is right. */
225 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
226 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */