]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/fnmatch.c
Libc-1082.50.1.tar.gz
[apple/libc.git] / gen / FreeBSD / fnmatch.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Guido van Rossum.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
e9ce8d39
A
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
5b2abdfb
A
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
35#endif /* LIBC_SCCS and not lint */
9385eb3d 36#include <sys/cdefs.h>
1f2f436a 37__FBSDID("$FreeBSD: src/lib/libc/gen/fnmatch.c,v 1.19 2010/04/16 22:29:24 jilles Exp $");
e9ce8d39 38
ad3c9f2a
A
39#include "xlocale_private.h"
40
e9ce8d39
A
41/*
42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
43 * Compares a filename or pathname to a pattern.
44 */
45
3d9156a7
A
46/*
47 * Some notes on multibyte character support:
48 * 1. Patterns with illegal byte sequences match nothing.
49 * 2. Illegal byte sequences in the "string" argument are handled by treating
50 * them as single-byte characters with a value of the first byte of the
51 * sequence cast to wchar_t.
52 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
53 * used for most, but not all, conversions. Further work will be required
54 * to support state-dependent encodings.
55 */
56
e9ce8d39 57#include <fnmatch.h>
3d9156a7 58#include <limits.h>
e9ce8d39 59#include <string.h>
3d9156a7
A
60#include <wchar.h>
61#include <wctype.h>
5b2abdfb
A
62
63#include "collate.h"
e9ce8d39
A
64
65#define EOS '\0'
66
ad3c9f2a 67#define RETURN_ERROR 2 /* neither 0 or FNM_NOMATCH */
5b2abdfb
A
68#define RANGE_MATCH 1
69#define RANGE_NOMATCH 0
70#define RANGE_ERROR (-1)
71
ad3c9f2a
A
72#define RECURSION_MAX 64
73
74__private_extern__ int rangematch(const char *, wchar_t, const char *, int, char **, char **, mbstate_t *, mbstate_t *, locale_t);
1f2f436a 75static int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
ad3c9f2a 76 mbstate_t, locale_t, int);
e9ce8d39
A
77
78int
79fnmatch(pattern, string, flags)
80 const char *pattern, *string;
81 int flags;
3d9156a7
A
82{
83 static const mbstate_t initial;
ad3c9f2a
A
84#if __DARWIN_UNIX03
85 return (fnmatch1(pattern, string, string, flags, initial, initial, __current_locale(), RECURSION_MAX));
86#else /* !__DARWIN_UNIX03 */
87 return (fnmatch1(pattern, string, string, flags, initial, initial, __current_locale(), RECURSION_MAX) != 0 ? FNM_NOMATCH : 0);
88#endif /* __DARWIN_UNIX03 */
3d9156a7
A
89}
90
91static int
ad3c9f2a 92fnmatch1(pattern, string, stringstart, flags, patmbs, strmbs, loc, recursion)
1f2f436a 93 const char *pattern, *string, *stringstart;
3d9156a7
A
94 int flags;
95 mbstate_t patmbs, strmbs;
ad3c9f2a
A
96 locale_t loc;
97 int recursion;
e9ce8d39 98{
ad3c9f2a 99 char *newp, *news;
3d9156a7
A
100 char c;
101 wchar_t pc, sc;
102 size_t pclen, sclen;
e9ce8d39 103
ad3c9f2a
A
104 if (recursion-- <= 0)
105 return RETURN_ERROR;
1f2f436a 106 for (;;) {
ad3c9f2a 107 pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, &patmbs, loc);
3d9156a7 108 if (pclen == (size_t)-1 || pclen == (size_t)-2)
ad3c9f2a
A
109#if __DARWIN_UNIX03
110 return (RETURN_ERROR);
111#else /* !__DARWIN_UNIX03 */
3d9156a7 112 return (FNM_NOMATCH);
ad3c9f2a 113#endif /* __DARWIN_UNIX03 */
3d9156a7 114 pattern += pclen;
ad3c9f2a 115 sclen = mbrtowc_l(&sc, string, MB_LEN_MAX, &strmbs, loc);
3d9156a7
A
116 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
117 sc = (unsigned char)*string;
118 sclen = 1;
119 memset(&strmbs, 0, sizeof(strmbs));
120 }
121 switch (pc) {
e9ce8d39 122 case EOS:
3d9156a7 123 if ((flags & FNM_LEADING_DIR) && sc == '/')
5b2abdfb 124 return (0);
3d9156a7 125 return (sc == EOS ? 0 : FNM_NOMATCH);
e9ce8d39 126 case '?':
3d9156a7 127 if (sc == EOS)
e9ce8d39 128 return (FNM_NOMATCH);
3d9156a7 129 if (sc == '/' && (flags & FNM_PATHNAME))
e9ce8d39 130 return (FNM_NOMATCH);
3d9156a7 131 if (sc == '.' && (flags & FNM_PERIOD) &&
e9ce8d39
A
132 (string == stringstart ||
133 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
134 return (FNM_NOMATCH);
3d9156a7 135 string += sclen;
e9ce8d39
A
136 break;
137 case '*':
138 c = *pattern;
139 /* Collapse multiple stars. */
140 while (c == '*')
141 c = *++pattern;
142
3d9156a7 143 if (sc == '.' && (flags & FNM_PERIOD) &&
e9ce8d39
A
144 (string == stringstart ||
145 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
146 return (FNM_NOMATCH);
147
148 /* Optimize for pattern with * at end or before /. */
149 if (c == EOS)
150 if (flags & FNM_PATHNAME)
5b2abdfb
A
151 return ((flags & FNM_LEADING_DIR) ||
152 strchr(string, '/') == NULL ?
e9ce8d39
A
153 0 : FNM_NOMATCH);
154 else
155 return (0);
156 else if (c == '/' && flags & FNM_PATHNAME) {
157 if ((string = strchr(string, '/')) == NULL)
158 return (FNM_NOMATCH);
159 break;
160 }
161
162 /* General case, use recursion. */
ad3c9f2a 163 int ret;
3d9156a7 164 while (sc != EOS) {
ad3c9f2a
A
165 if ((ret = fnmatch1(pattern, string, stringstart,
166 flags, patmbs, strmbs, loc, recursion)) != FNM_NOMATCH)
167 return (ret);
168 sclen = mbrtowc_l(&sc, string, MB_LEN_MAX,
169 &strmbs, loc);
3d9156a7
A
170 if (sclen == (size_t)-1 ||
171 sclen == (size_t)-2) {
172 sc = (unsigned char)*string;
173 sclen = 1;
174 memset(&strmbs, 0, sizeof(strmbs));
175 }
176 if (sc == '/' && flags & FNM_PATHNAME)
e9ce8d39 177 break;
3d9156a7 178 string += sclen;
e9ce8d39
A
179 }
180 return (FNM_NOMATCH);
181 case '[':
3d9156a7 182 if (sc == EOS)
e9ce8d39 183 return (FNM_NOMATCH);
3d9156a7 184 if (sc == '/' && (flags & FNM_PATHNAME))
5b2abdfb 185 return (FNM_NOMATCH);
3d9156a7 186 if (sc == '.' && (flags & FNM_PERIOD) &&
5b2abdfb
A
187 (string == stringstart ||
188 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
e9ce8d39 189 return (FNM_NOMATCH);
5b2abdfb 190
ad3c9f2a
A
191 switch (rangematch(pattern, sc, string + sclen, flags,
192 &newp, &news, &patmbs, &strmbs, loc)) {
5b2abdfb 193 case RANGE_ERROR:
ad3c9f2a
A
194#if __DARWIN_UNIX03
195 return (RETURN_ERROR);
196#else /* !__DARWIN_UNIX03 */
5b2abdfb 197 goto norm;
ad3c9f2a 198#endif /* __DARWIN_UNIX03 */
5b2abdfb
A
199 case RANGE_MATCH:
200 pattern = newp;
ad3c9f2a 201 string = news;
5b2abdfb
A
202 break;
203 case RANGE_NOMATCH:
e9ce8d39 204 return (FNM_NOMATCH);
5b2abdfb 205 }
e9ce8d39
A
206 break;
207 case '\\':
208 if (!(flags & FNM_NOESCAPE)) {
ad3c9f2a
A
209 pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX,
210 &patmbs, loc);
3d9156a7 211 if (pclen == (size_t)-1 || pclen == (size_t)-2)
ad3c9f2a
A
212#if __DARWIN_UNIX03
213 return (RETURN_ERROR);
214#else /* !__DARWIN_UNIX03 */
3d9156a7 215 return (FNM_NOMATCH);
ad3c9f2a 216#endif /* __DARWIN_UNIX03 */
3d9156a7
A
217 if (pclen == 0)
218 pc = '\\';
219 pattern += pclen;
e9ce8d39
A
220 }
221 /* FALLTHROUGH */
222 default:
ad3c9f2a 223#if !__DARWIN_UNIX03
5b2abdfb 224 norm:
ad3c9f2a 225#endif /* !__DARWIN_UNIX03 */
3d9156a7 226 if (pc == sc)
5b2abdfb
A
227 ;
228 else if ((flags & FNM_CASEFOLD) &&
ad3c9f2a 229 (towlower_l(pc, loc) == towlower_l(sc, loc)))
5b2abdfb
A
230 ;
231 else
e9ce8d39 232 return (FNM_NOMATCH);
3d9156a7 233 string += sclen;
e9ce8d39
A
234 break;
235 }
3d9156a7 236 }
e9ce8d39
A
237 /* NOTREACHED */
238}
239
ad3c9f2a
A
240#ifndef BUILDING_VARIANT
241__private_extern__ int
242rangematch(pattern, test, string, flags, newp, news, patmbs, strmbs, loc)
243 const char *pattern, *string;
3d9156a7 244 wchar_t test;
5b2abdfb 245 int flags;
ad3c9f2a
A
246 char **newp, **news;
247 mbstate_t *patmbs, *strmbs;
248 locale_t loc;
e9ce8d39 249{
ad3c9f2a 250 int negate, ok, special;
3d9156a7 251 wchar_t c, c2;
ad3c9f2a
A
252 wchar_t buf[STR_LEN]; /* STR_LEN defined in collate.h */
253 size_t pclen, sclen, len;
254 const char *origpat, *cp, *savestring;
255 mbstate_t save;
e9ce8d39
A
256
257 /*
258 * A bracket expression starting with an unquoted circumflex
259 * character produces unspecified results (IEEE 1003.2-1992,
260 * 3.13.2). This implementation treats it like '!', for
261 * consistency with the regular expression syntax.
262 * J.T. Conklin (conklin@ngai.kaleida.com)
263 */
5b2abdfb 264 if ( (negate = (*pattern == '!' || *pattern == '^')) )
e9ce8d39 265 ++pattern;
5b2abdfb
A
266
267 if (flags & FNM_CASEFOLD)
ad3c9f2a 268 test = towlower_l(test, loc);
5b2abdfb
A
269
270 /*
271 * A right bracket shall lose its special meaning and represent
272 * itself in a bracket expression if it occurs first in the list.
273 * -- POSIX.2 2.8.3.2
274 */
275 ok = 0;
3d9156a7
A
276 origpat = pattern;
277 for (;;) {
ad3c9f2a 278 c = 0;
3d9156a7 279 if (*pattern == ']' && pattern > origpat) {
3d9156a7
A
280 break;
281 } else if (*pattern == '\0') {
5b2abdfb 282 return (RANGE_ERROR);
3d9156a7 283 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
3d9156a7
A
284 return (RANGE_NOMATCH);
285 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
286 pattern++;
ad3c9f2a
A
287 else if (*pattern == '[' && ((special = *(pattern + 1)) == '.' || special == '=' || special == ':')) {
288 cp = (pattern += 2);
6465356a 289 while((cp = strchr(cp, special))) {
ad3c9f2a
A
290 if (*(cp + 1) == ']')
291 break;
292 cp++;
293 }
294 if (!cp)
295 return (RANGE_ERROR);
296 if (special == '.') {
297treat_like_collating_symbol:
298 len = __collate_collating_symbol(buf, STR_LEN, pattern, cp - pattern, patmbs, loc);
299 if (len == (size_t)-1 || len == 0)
300 return (RANGE_ERROR);
301 pattern = cp + 2;
302 if (len > 1) {
303 wchar_t *wp, sc;
304 /* no multi-character collation symbols as start of range */
305 if (*(cp + 2) == '-' && *(cp + 3) != EOS
306 && *(cp + 3) != ']')
307 return (RANGE_ERROR);
308 wp = buf;
309 if (test != *wp++)
310 continue;
311 if (len == 1) {
312 ok = 1;
313 break;
314 }
315 memcpy(&save, strmbs, sizeof(save));
316 savestring = string;
317 while (--len > 0) {
318 sclen = mbrtowc_l(&sc, string, MB_LEN_MAX, strmbs, loc);
319 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
320 sc = (unsigned char)*string;
321 sclen = 1;
322 memset(&strmbs, 0, sizeof(strmbs));
323 }
324 if (sc != *wp++) {
325 memcpy(strmbs, &save, sizeof(save));
326 string = savestring;
327 break;
328 }
329 string += sclen;
330 }
331 if (len == 0) {
332 ok = 1;
333 break;
334 }
335 continue; /* no match */
336 }
337 c = *buf;
338 } else if (special == '=') {
339 int ec;
340 memcpy(&save, patmbs, sizeof(save));
341 ec = __collate_equiv_class(pattern, cp - pattern, patmbs, loc);
342 if (ec < 0)
343 return (RANGE_ERROR);
344 if (ec == 0) {
345 memcpy(patmbs, &save, sizeof(save));
346 goto treat_like_collating_symbol;
347 }
348 pattern = cp + 2;
349 /* no equivalence classes as start of range */
350 if (*(cp + 2) == '-' && *(cp + 3) != EOS &&
351 *(cp + 3) != ']')
352 return (RANGE_ERROR);
353 len = __collate_equiv_match(ec, NULL, 0, test, string, strlen(string), strmbs, &sclen, loc);
6465356a 354 if (len == (size_t)-1) {
ad3c9f2a 355 return (RANGE_ERROR);
6465356a 356 }
ad3c9f2a
A
357 if (len > 0) {
358 ok = 1;
359 string += sclen;
360 break;
361 }
362 continue;
363 } else { /* special == ':' */
364 wctype_t charclass;
365 char name[CHARCLASS_NAME_MAX + 1];
366 /* no character classes as start of range */
367 if (*(cp + 2) == '-' && *(cp + 3) != EOS &&
368 *(cp + 3) != ']')
369 return (RANGE_ERROR);
370 /* assume character class names are ascii */
371 if (cp - pattern > CHARCLASS_NAME_MAX)
372 return (RANGE_ERROR);
373 strlcpy(name, pattern, cp - pattern + 1);
374 pattern = cp + 2;
375 if ((charclass = wctype(name)) == 0)
376 return (RANGE_ERROR);
377 if (iswctype_l(test, charclass, loc)) {
378 ok = 1;
379 break;
380 }
381 continue;
382 }
383 }
384 if (!c) {
385 pclen = mbrtowc_l(&c, pattern, MB_LEN_MAX, patmbs, loc);
386 if (pclen == (size_t)-1 || pclen == (size_t)-2)
387 return (RANGE_ERROR);
388 pattern += pclen;
389 }
5b2abdfb
A
390
391 if (flags & FNM_CASEFOLD)
ad3c9f2a 392 c = towlower_l(c, loc);
5b2abdfb 393
3d9156a7
A
394 if (*pattern == '-' && *(pattern + 1) != EOS &&
395 *(pattern + 1) != ']') {
396 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
397 if (*pattern != EOS)
398 pattern++;
ad3c9f2a 399 pclen = mbrtowc_l(&c2, pattern, MB_LEN_MAX, patmbs, loc);
3d9156a7 400 if (pclen == (size_t)-1 || pclen == (size_t)-2)
ad3c9f2a 401 return (RANGE_ERROR);
3d9156a7 402 pattern += pclen;
e9ce8d39 403 if (c2 == EOS)
5b2abdfb
A
404 return (RANGE_ERROR);
405
6465356a 406 if ((c2 == '[' && (special = *pattern) == '.') || special == '=' || special == ':') {
ad3c9f2a
A
407 /* no equivalence classes or character classes as end of range */
408 if (special == '=' || special == ':')
409 return (RANGE_ERROR);
410 cp = ++pattern;
6465356a 411 while((cp = strchr(cp, special))) {
ad3c9f2a
A
412 if (*(cp + 1) == ']')
413 break;
414 cp++;
415 }
416 if (!cp)
417 return (RANGE_ERROR);
418 len = __collate_collating_symbol(buf, STR_LEN, pattern, cp - pattern, patmbs, loc);
419 /* no multi-character collation symbols as end of range */
420 if (len != 1)
421 return (RANGE_ERROR);
422 pattern = cp + 2;
423 c2 = *buf;
424 }
425
5b2abdfb 426 if (flags & FNM_CASEFOLD)
ad3c9f2a 427 c2 = towlower_l(c2, loc);
5b2abdfb 428
ad3c9f2a 429 if (loc->__collate_load_error ?
5b2abdfb 430 c <= test && test <= c2 :
ad3c9f2a
A
431 __collate_range_cmp(c, test, loc) <= 0
432 && __collate_range_cmp(test, c2, loc) <= 0
433 ) {
e9ce8d39 434 ok = 1;
ad3c9f2a
A
435 break;
436 }
437 } else if (c == test) {
e9ce8d39 438 ok = 1;
ad3c9f2a
A
439 break;
440 }
3d9156a7 441 }
ad3c9f2a
A
442 /* go to end of bracket expression */
443 special = 0;
444 while(*pattern != ']') {
445 if (*pattern == 0)
446 return (RANGE_ERROR);
447 if (*pattern == special) {
448 if (*++pattern == ']') {
449 special = 0;
450 pattern++;
451 }
452 continue;
453 }
454 if (!special && *pattern == '[') {
455 special = *++pattern;
456 if (special != '.' && special != '=' && special != ':')
457 special = 0;
458 else
459 pattern++;
460 continue;
461 }
462 pclen = mbrtowc_l(&c, pattern, MB_LEN_MAX, patmbs, loc);
463 if (pclen == (size_t)-1 || pclen == (size_t)-2)
464 return (RANGE_ERROR);
465 pattern += pclen;
466 }
5b2abdfb 467
ad3c9f2a
A
468 *newp = (char *)++pattern;
469 *news = (char *)string;
5b2abdfb 470 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
e9ce8d39 471}
ad3c9f2a 472#endif /* BUILDING_VARIANT */