Annotation of src/lib/libc/gen/fnmatch.c, Revision 1.21.40.1
1.21.40.1! bouyer 1: /* $NetBSD: fnmatch.c,v 1.24 2011/01/31 19:10:18 christos Exp $ */
1.11 cgd 2:
1.1 cgd 3: /*
1.11 cgd 4: * Copyright (c) 1989, 1993, 1994
1.6 cgd 5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * This code is derived from software contributed to Berkeley by
8: * Guido van Rossum.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.20 agc 18: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
1.12 christos 35: #include <sys/cdefs.h>
1.1 cgd 36: #if defined(LIBC_SCCS) && !defined(lint)
1.11 cgd 37: #if 0
38: static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
39: #else
1.21.40.1! bouyer 40: __RCSID("$NetBSD: fnmatch.c,v 1.24 2011/01/31 19:10:18 christos Exp $");
1.11 cgd 41: #endif
1.1 cgd 42: #endif /* LIBC_SCCS and not lint */
43:
44: /*
1.7 jtc 45: * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
1.1 cgd 46: * Compares a filename or pathname to a pattern.
47: */
48:
1.13 jtc 49: #include "namespace.h"
1.15 lukem 50:
51: #include <assert.h>
1.18 thorpej 52: #include <ctype.h>
1.3 jtc 53: #include <fnmatch.h>
1.1 cgd 54: #include <string.h>
1.13 jtc 55:
56: #ifdef __weak_alias
1.17 mycroft 57: __weak_alias(fnmatch,_fnmatch)
1.13 jtc 58: #endif
1.1 cgd 59:
60: #define EOS '\0'
61:
1.21 perry 62: static inline int
1.18 thorpej 63: foldcase(int ch, int flags)
64: {
65:
66: if ((flags & FNM_CASEFOLD) != 0 && isupper(ch))
1.21.40.1! bouyer 67: return tolower(ch);
! 68: return ch;
1.18 thorpej 69: }
70:
71: #define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
72:
1.21.40.1! bouyer 73: static const char *
! 74: rangematch(const char *pattern, int test, int flags)
! 75: {
! 76: int negate, ok;
! 77: char c, c2;
! 78:
! 79: _DIAGASSERT(pattern != NULL);
! 80:
! 81: /*
! 82: * A bracket expression starting with an unquoted circumflex
! 83: * character produces unspecified results (IEEE 1003.2-1992,
! 84: * 3.13.2). This implementation treats it like '!', for
! 85: * consistency with the regular expression syntax.
! 86: * J.T. Conklin (conklin@ngai.kaleida.com)
! 87: */
! 88: if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
! 89: ++pattern;
! 90:
! 91: for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']';) {
! 92: if (c == '\\' && !(flags & FNM_NOESCAPE))
! 93: c = FOLDCASE(*pattern++, flags);
! 94: if (c == EOS)
! 95: return NULL;
! 96: if (*pattern == '-'
! 97: && (c2 = FOLDCASE(*(pattern + 1), flags)) != EOS &&
! 98: c2 != ']') {
! 99: pattern += 2;
! 100: if (c2 == '\\' && !(flags & FNM_NOESCAPE))
! 101: c2 = FOLDCASE(*pattern++, flags);
! 102: if (c2 == EOS)
! 103: return NULL;
! 104: if (c <= test && test <= c2)
! 105: ok = 1;
! 106: } else if (c == test)
! 107: ok = 1;
! 108: }
! 109: return ok == negate ? NULL : pattern;
! 110: }
! 111:
! 112:
! 113: static int
! 114: fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
1.1 cgd 115: {
1.11 cgd 116: const char *stringstart;
117: char c, test;
1.1 cgd 118:
1.15 lukem 119: _DIAGASSERT(pattern != NULL);
120: _DIAGASSERT(string != NULL);
121:
1.21.40.1! bouyer 122: if (recursion-- == 0)
! 123: return FNM_NORES;
! 124:
! 125: for (stringstart = string;;) {
1.18 thorpej 126: switch (c = FOLDCASE(*pattern++, flags)) {
1.1 cgd 127: case EOS:
1.19 provos 128: if ((flags & FNM_LEADING_DIR) && *string == '/')
1.21.40.1! bouyer 129: return 0;
! 130: return *string == EOS ? 0 : FNM_NOMATCH;
1.1 cgd 131: case '?':
1.8 jtc 132: if (*string == EOS)
1.21.40.1! bouyer 133: return FNM_NOMATCH;
1.8 jtc 134: if (*string == '/' && (flags & FNM_PATHNAME))
1.21.40.1! bouyer 135: return FNM_NOMATCH;
1.8 jtc 136: if (*string == '.' && (flags & FNM_PERIOD) &&
1.11 cgd 137: (string == stringstart ||
138: ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.21.40.1! bouyer 139: return FNM_NOMATCH;
1.8 jtc 140: ++string;
1.1 cgd 141: break;
142: case '*':
1.18 thorpej 143: c = FOLDCASE(*pattern, flags);
1.3 jtc 144: /* Collapse multiple stars. */
1.1 cgd 145: while (c == '*')
1.18 thorpej 146: c = FOLDCASE(*++pattern, flags);
1.1 cgd 147:
1.8 jtc 148: if (*string == '.' && (flags & FNM_PERIOD) &&
1.11 cgd 149: (string == stringstart ||
150: ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.21.40.1! bouyer 151: return FNM_NOMATCH;
1.8 jtc 152:
1.3 jtc 153: /* Optimize for pattern with * at end or before /. */
1.14 christos 154: if (c == EOS) {
1.1 cgd 155: if (flags & FNM_PATHNAME)
1.21.40.1! bouyer 156: return (flags & FNM_LEADING_DIR) ||
1.19 provos 157: strchr(string, '/') == NULL ?
1.21.40.1! bouyer 158: 0 : FNM_NOMATCH;
1.1 cgd 159: else
1.21.40.1! bouyer 160: return 0;
1.14 christos 161: } else if (c == '/' && flags & FNM_PATHNAME) {
1.10 jtc 162: if ((string = strchr(string, '/')) == NULL)
1.21.40.1! bouyer 163: return FNM_NOMATCH;
1.1 cgd 164: break;
165: }
166:
1.3 jtc 167: /* General case, use recursion. */
1.18 thorpej 168: while ((test = FOLDCASE(*string, flags)) != EOS) {
1.21.40.1! bouyer 169: int e;
! 170: switch ((e = fnmatchx(pattern, string,
! 171: flags & ~FNM_PERIOD, recursion))) {
! 172: case FNM_NOMATCH:
! 173: break;
! 174: default:
! 175: return e;
! 176: }
1.1 cgd 177: if (test == '/' && flags & FNM_PATHNAME)
178: break;
179: ++string;
180: }
1.21.40.1! bouyer 181: return FNM_NOMATCH;
1.1 cgd 182: case '[':
1.8 jtc 183: if (*string == EOS)
1.21.40.1! bouyer 184: return FNM_NOMATCH;
1.8 jtc 185: if (*string == '/' && flags & FNM_PATHNAME)
1.21.40.1! bouyer 186: return FNM_NOMATCH;
! 187: if ((pattern = rangematch(pattern,
! 188: FOLDCASE(*string, flags), flags)) == NULL)
! 189: return FNM_NOMATCH;
1.8 jtc 190: ++string;
1.1 cgd 191: break;
192: case '\\':
1.3 jtc 193: if (!(flags & FNM_NOESCAPE)) {
1.18 thorpej 194: if ((c = FOLDCASE(*pattern++, flags)) == EOS) {
1.1 cgd 195: c = '\\';
196: --pattern;
197: }
198: }
199: /* FALLTHROUGH */
200: default:
1.18 thorpej 201: if (c != FOLDCASE(*string++, flags))
1.21.40.1! bouyer 202: return FNM_NOMATCH;
1.1 cgd 203: break;
204: }
1.21.40.1! bouyer 205: }
1.3 jtc 206: /* NOTREACHED */
207: }
208:
1.21.40.1! bouyer 209: int
! 210: fnmatch(const char *pattern, const char *string, int flags)
1.3 jtc 211: {
1.21.40.1! bouyer 212: return fnmatchx(pattern, string, flags, 64);
1.1 cgd 213: }
CVSweb <webmaster@jp.NetBSD.org>