1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997 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 /* Comment out all this code if we are using the GNU C Library, and are not
34 actually compiling the library itself. This code is part of the GNU C
35 Library, but also included in many other GNU distributions. Compiling
36 and linking in this code is a waste when using the GNU C library
37 (especially if it is a shared library). Rather than having every GNU
38 program understand `configure --with-gnu-libc' and omit the object files,
39 it is simpler to just do this in the source for each such file. */
41 #if defined _LIBC || !defined __GNU_LIBRARY__
44 # if defined STDC_HEADERS || !defined isascii
47 # define ISASCII(c) isascii(c)
50 # define ISUPPER(c) (ISASCII (c) && isupper (c))
57 /* Match STRING against the filename pattern PATTERN, returning zero if
58 it matches, nonzero if not. */
60 fnmatch (pattern
, string
, flags
)
65 register const char *p
= pattern
, *n
= string
;
68 /* Note that this evaluates C many times. */
69 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
71 while ((c
= *p
++) != '\0')
80 else if ((flags
& FNM_FILE_NAME
) && *n
== '/')
82 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
83 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
88 if (!(flags
& FNM_NOESCAPE
))
92 /* Trailing \ loses. */
101 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
102 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
105 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
107 if ((flags
& FNM_FILE_NAME
) && *n
== '/')
108 /* A slash does not match a wildcard under FNM_FILE_NAME. */
112 /* A ? needs to match one character. */
114 /* There isn't another character; no match. */
117 /* One character of the string is consumed in matching
118 this ? wildcard, so *??? won't match if there are
119 less than three characters. */
128 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
130 for (--p
; *n
!= '\0'; ++n
)
131 if ((c
== '[' || FOLD (*n
) == c1
) &&
132 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
139 /* Nonzero if the sense of the character class is inverted. */
145 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
146 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
149 not = (*p
== '!' || *p
== '^');
156 register char cstart
= c
, cend
= c
;
158 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
162 cstart
= cend
= *p
++;
165 cstart
= cend
= FOLD (cstart
);
168 /* [ (unterminated) loses. */
174 if ((flags
& FNM_FILE_NAME
) && c
== '/')
175 /* [/] can never match. */
178 if (c
== '-' && *p
!= ']')
181 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
190 if (FOLD (*n
) >= cstart
&& FOLD (*n
) <= cend
)
201 /* Skip the rest of the [...] that already matched. */
205 /* [... (unterminated) loses. */
209 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
213 /* XXX 1003.2d11 is unclear if this is right. */
233 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
234 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
242 #endif /* _LIBC or not __GNU_LIBRARY__. */