1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
22 /* Enable GNU extensions in fnmatch.h. */
32 /* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */
40 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
47 /* Match STRING against the filename pattern PATTERN, returning zero if
48 it matches, nonzero if not. */
50 fnmatch (pattern
, string
, flags
)
55 register const char *p
= pattern
, *n
= string
;
58 /* Note that this evalutes C many times. */
59 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
61 while ((c
= *p
++) != '\0')
70 else if ((flags
& FNM_FILE_NAME
) && *n
== '/')
72 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
73 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
78 if (!(flags
& FNM_NOESCAPE
))
88 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
89 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
92 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++, ++n
)
93 if (((flags
& FNM_FILE_NAME
) && *n
== '/') ||
94 (c
== '?' && *n
== '\0'))
101 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
103 for (--p
; *n
!= '\0'; ++n
)
104 if ((c
== '[' || FOLD (*n
) == c1
) &&
105 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
112 /* Nonzero if the sense of the character class is inverted. */
118 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
119 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
122 not = (*p
== '!' || *p
== '^');
129 register char cstart
= c
, cend
= c
;
131 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
132 cstart
= cend
= *p
++;
134 cstart
= cend
= FOLD (cstart
);
137 /* [ (unterminated) loses. */
143 if ((flags
& FNM_FILE_NAME
) && c
== '/')
144 /* [/] can never match. */
147 if (c
== '-' && *p
!= ']')
150 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
159 if (FOLD (*n
) >= cstart
&& FOLD (*n
) <= cend
)
170 /* Skip the rest of the [...] that already matched. */
174 /* [... (unterminated) loses. */
178 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
179 /* XXX 1003.2d11 is unclear if this is right. */
198 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
199 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
205 #endif /* _LIBC or not __GNU_LIBRARY__. */