1 /* Copyright (C) 1991, 1992, 1993, 1997 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C
4 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #if defined (STDC_HEADERS) || !defined (isascii)
31 # define ISASCII(c) isascii(c)
34 #define ISUPPER(c) (ISASCII (c) && isupper (c))
40 /* Match STRING against the filename pattern PATTERN, returning zero if
41 it matches, nonzero if not. */
43 fnmatch (pattern
, string
, flags
)
48 register const char *p
= pattern
, *n
= string
;
51 /* Note that this evaluates C many times. */
52 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
54 while ((c
= *p
++) != '\0')
63 else if ((flags
& FNM_FILE_NAME
) && *n
== '/')
65 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
66 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
71 if (!(flags
& FNM_NOESCAPE
))
81 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
82 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
85 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++, ++n
)
86 if (((flags
& FNM_FILE_NAME
) && *n
== '/') ||
87 (c
== '?' && *n
== '\0'))
94 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
96 for (--p
; *n
!= '\0'; ++n
)
97 if ((c
== '[' || FOLD (*n
) == c1
) &&
98 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
105 /* Nonzero if the sense of the character class is inverted. */
111 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
112 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
115 not = (*p
== '!' || *p
== '^');
122 register char cstart
= c
, cend
= c
;
124 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
125 cstart
= cend
= *p
++;
127 cstart
= cend
= FOLD (cstart
);
130 /* [ (unterminated) loses. */
136 if ((flags
& FNM_FILE_NAME
) && c
== '/')
137 /* [/] can never match. */
140 if (c
== '-' && *p
!= ']')
143 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
152 if (FOLD (*n
) >= cstart
&& FOLD (*n
) <= cend
)
163 /* Skip the rest of the [...] that already matched. */
167 /* [... (unterminated) loses. */
171 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
172 /* XXX 1003.2d11 is unclear if this is right. */
191 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
192 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */