1 /* Copyright (C) 1991, 1992, 1993 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))
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
45 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
52 /* Match STRING against the filename pattern PATTERN, returning zero if
53 it matches, nonzero if not. */
55 fnmatch (pattern
, string
, flags
)
60 register const char *p
= pattern
, *n
= string
;
63 /* Note that this evalutes C many times. */
64 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
66 while ((c
= *p
++) != '\0')
75 else if ((flags
& FNM_FILE_NAME
) && *n
== '/')
77 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
78 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
83 if (!(flags
& FNM_NOESCAPE
))
93 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
94 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
97 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++, ++n
)
98 if (((flags
& FNM_FILE_NAME
) && *n
== '/') ||
99 (c
== '?' && *n
== '\0'))
106 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
108 for (--p
; *n
!= '\0'; ++n
)
109 if ((c
== '[' || FOLD (*n
) == c1
) &&
110 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
117 /* Nonzero if the sense of the character class is inverted. */
123 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
124 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
127 not = (*p
== '!' || *p
== '^');
134 register char cstart
= c
, cend
= c
;
136 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
137 cstart
= cend
= *p
++;
139 cstart
= cend
= FOLD (cstart
);
142 /* [ (unterminated) loses. */
148 if ((flags
& FNM_FILE_NAME
) && c
== '/')
149 /* [/] can never match. */
152 if (c
== '-' && *p
!= ']')
155 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
164 if (FOLD (*n
) >= cstart
&& FOLD (*n
) <= cend
)
175 /* Skip the rest of the [...] that already matched. */
179 /* [... (unterminated) loses. */
183 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
184 /* XXX 1003.2d11 is unclear if this is right. */
203 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
204 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
212 #endif /* _LIBC or not __GNU_LIBRARY__. */