]>
Dogcows Code - chaz/tar/blob - lib/fnmatch.c
1 /* Copyright 1991, 1992, 1993, 1996, 1997, 2000 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program 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
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Enable GNU extensions in fnmatch.h. */
23 # define _GNU_SOURCE 1
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself. This code is part of the GNU C
33 Library, but also included in many other GNU distributions. Compiling
34 and linking in this code is a waste when using the GNU C library
35 (especially if it is a shared library). Rather than having every GNU
36 program understand `configure --with-gnu-libc' and omit the object files,
37 it is simpler to just do this in the source for each such file. */
39 #if defined _LIBC || !defined __GNU_LIBRARY__
42 # if defined STDC_HEADERS || !defined isascii
43 # define IN_CTYPE_DOMAIN(c) 1
45 # define IN_CTYPE_DOMAIN(c) isascii(c)
48 # define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
55 /* Match STRING against the filename pattern PATTERN, returning zero if
56 it matches, nonzero if not. */
58 fnmatch (const char *pattern
, const char *string
, int flags
)
60 register const char *p
= pattern
, *n
= string
;
63 /* Note that this evaluates 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
))
87 /* Trailing \ loses. */
96 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
97 (n
== string
|| ((flags
& FNM_FILE_NAME
) && n
[-1] == '/')))
100 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
104 /* A ? needs to match one character. */
105 if (*n
== '\0' || (*n
== '/' && (flags
& FNM_FILE_NAME
)))
106 /* There isn't another character; no match. */
109 /* One character of the string is consumed in matching
110 this ? wildcard, so *??? won't match if there are
111 less than three characters. */
118 if ((flags
& (FNM_FILE_NAME
| FNM_LEADING_DIR
)) == FNM_FILE_NAME
)
119 for (; *n
!= '\0'; n
++)
126 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
128 for (--p
; *n
!= '\0'; ++n
)
129 if ((c
== '[' || FOLD (*n
) == c1
) &&
130 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
132 else if (*n
== '/' && (flags
& FNM_FILE_NAME
))
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__. */
This page took 0.047313 seconds and 4 git commands to generate.