]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/fnmatch.c
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/gen/fnmatch.c,v 1.19 2010/04/16 22:29:24 jilles Exp $");
40 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
41 * Compares a filename or pathname to a pattern.
45 * Some notes on multibyte character support:
46 * 1. Patterns with illegal byte sequences match nothing.
47 * 2. Illegal byte sequences in the "string" argument are handled by treating
48 * them as single-byte characters with a value of the first byte of the
49 * sequence cast to wchar_t.
50 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
51 * used for most, but not all, conversions. Further work will be required
52 * to support state-dependent encodings.
66 #define RANGE_NOMATCH 0
67 #define RANGE_ERROR (-1)
69 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
70 static int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
74 fnmatch(pattern
, string
, flags
)
75 const char *pattern
, *string
;
78 static const mbstate_t initial
;
80 return (fnmatch1(pattern
, string
, string
, flags
, initial
, initial
));
84 fnmatch1(pattern
, string
, stringstart
, flags
, patmbs
, strmbs
)
85 const char *pattern
, *string
, *stringstart
;
87 mbstate_t patmbs
, strmbs
;
95 pclen
= mbrtowc(&pc
, pattern
, MB_LEN_MAX
, &patmbs
);
96 if (pclen
== (size_t)-1 || pclen
== (size_t)-2)
99 sclen
= mbrtowc(&sc
, string
, MB_LEN_MAX
, &strmbs
);
100 if (sclen
== (size_t)-1 || sclen
== (size_t)-2) {
101 sc
= (unsigned char)*string
;
103 memset(&strmbs
, 0, sizeof(strmbs
));
107 if ((flags
& FNM_LEADING_DIR
) && sc
== '/')
109 return (sc
== EOS
? 0 : FNM_NOMATCH
);
112 return (FNM_NOMATCH
);
113 if (sc
== '/' && (flags
& FNM_PATHNAME
))
114 return (FNM_NOMATCH
);
115 if (sc
== '.' && (flags
& FNM_PERIOD
) &&
116 (string
== stringstart
||
117 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
118 return (FNM_NOMATCH
);
123 /* Collapse multiple stars. */
127 if (sc
== '.' && (flags
& FNM_PERIOD
) &&
128 (string
== stringstart
||
129 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
130 return (FNM_NOMATCH
);
132 /* Optimize for pattern with * at end or before /. */
134 if (flags
& FNM_PATHNAME
)
135 return ((flags
& FNM_LEADING_DIR
) ||
136 strchr(string
, '/') == NULL
?
140 else if (c
== '/' && flags
& FNM_PATHNAME
) {
141 if ((string
= strchr(string
, '/')) == NULL
)
142 return (FNM_NOMATCH
);
146 /* General case, use recursion. */
148 if (!fnmatch1(pattern
, string
, stringstart
,
149 flags
, patmbs
, strmbs
))
151 sclen
= mbrtowc(&sc
, string
, MB_LEN_MAX
,
153 if (sclen
== (size_t)-1 ||
154 sclen
== (size_t)-2) {
155 sc
= (unsigned char)*string
;
157 memset(&strmbs
, 0, sizeof(strmbs
));
159 if (sc
== '/' && flags
& FNM_PATHNAME
)
163 return (FNM_NOMATCH
);
166 return (FNM_NOMATCH
);
167 if (sc
== '/' && (flags
& FNM_PATHNAME
))
168 return (FNM_NOMATCH
);
169 if (sc
== '.' && (flags
& FNM_PERIOD
) &&
170 (string
== stringstart
||
171 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
172 return (FNM_NOMATCH
);
174 switch (rangematch(pattern
, sc
, flags
, &newp
,
182 return (FNM_NOMATCH
);
187 if (!(flags
& FNM_NOESCAPE
)) {
188 pclen
= mbrtowc(&pc
, pattern
, MB_LEN_MAX
,
190 if (pclen
== (size_t)-1 || pclen
== (size_t)-2)
191 return (FNM_NOMATCH
);
201 else if ((flags
& FNM_CASEFOLD
) &&
202 (towlower(pc
) == towlower(sc
)))
205 return (FNM_NOMATCH
);
214 rangematch(pattern
, test
, flags
, newp
, patmbs
)
227 * A bracket expression starting with an unquoted circumflex
228 * character produces unspecified results (IEEE 1003.2-1992,
229 * 3.13.2). This implementation treats it like '!', for
230 * consistency with the regular expression syntax.
231 * J.T. Conklin (conklin@ngai.kaleida.com)
233 if ( (negate
= (*pattern
== '!' || *pattern
== '^')) )
236 if (flags
& FNM_CASEFOLD
)
237 test
= towlower(test
);
240 * A right bracket shall lose its special meaning and represent
241 * itself in a bracket expression if it occurs first in the list.
247 if (*pattern
== ']' && pattern
> origpat
) {
250 } else if (*pattern
== '\0') {
251 return (RANGE_ERROR
);
252 } else if (*pattern
== '/' && (flags
& FNM_PATHNAME
)) {
253 return (RANGE_NOMATCH
);
254 } else if (*pattern
== '\\' && !(flags
& FNM_NOESCAPE
))
256 pclen
= mbrtowc(&c
, pattern
, MB_LEN_MAX
, patmbs
);
257 if (pclen
== (size_t)-1 || pclen
== (size_t)-2)
258 return (RANGE_NOMATCH
);
261 if (flags
& FNM_CASEFOLD
)
264 if (*pattern
== '-' && *(pattern
+ 1) != EOS
&&
265 *(pattern
+ 1) != ']') {
266 if (*++pattern
== '\\' && !(flags
& FNM_NOESCAPE
))
269 pclen
= mbrtowc(&c2
, pattern
, MB_LEN_MAX
, patmbs
);
270 if (pclen
== (size_t)-1 || pclen
== (size_t)-2)
271 return (RANGE_NOMATCH
);
274 return (RANGE_ERROR
);
276 if (flags
& FNM_CASEFOLD
)
279 if (__collate_load_error
?
280 c
<= test
&& test
<= c2
:
281 __collate_range_cmp(c
, test
) <= 0
282 && __collate_range_cmp(test
, c2
) <= 0
285 } else if (c
== test
)
289 *newp
= (char *)pattern
;
290 return (ok
== negate
? RANGE_NOMATCH
: RANGE_MATCH
);