1*0a6a1f1dSLionel Sambuc /* $NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1989, 1993, 1994
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras * Guido van Rossum.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras * without specific prior written permission.
212fe8fb19SBen Gras *
222fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras * SUCH DAMAGE.
332fe8fb19SBen Gras */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
392fe8fb19SBen Gras #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
462fe8fb19SBen Gras * Compares a filename or pathname to a pattern.
472fe8fb19SBen Gras */
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include "namespace.h"
502fe8fb19SBen Gras
512fe8fb19SBen Gras #include <assert.h>
522fe8fb19SBen Gras #include <ctype.h>
532fe8fb19SBen Gras #include <fnmatch.h>
542fe8fb19SBen Gras #include <string.h>
552fe8fb19SBen Gras
562fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(fnmatch,_fnmatch)572fe8fb19SBen Gras __weak_alias(fnmatch,_fnmatch)
582fe8fb19SBen Gras #endif
592fe8fb19SBen Gras
602fe8fb19SBen Gras #define EOS '\0'
612fe8fb19SBen Gras
622fe8fb19SBen Gras static inline int
632fe8fb19SBen Gras foldcase(int ch, int flags)
642fe8fb19SBen Gras {
652fe8fb19SBen Gras
662fe8fb19SBen Gras if ((flags & FNM_CASEFOLD) != 0 && isupper(ch))
67f14fb602SLionel Sambuc return tolower(ch);
68f14fb602SLionel Sambuc return ch;
692fe8fb19SBen Gras }
702fe8fb19SBen Gras
712fe8fb19SBen Gras #define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
722fe8fb19SBen Gras
732fe8fb19SBen Gras static const char *
rangematch(const char * pattern,int test,int flags)74f14fb602SLionel Sambuc rangematch(const char *pattern, int test, int flags)
752fe8fb19SBen Gras {
76*0a6a1f1dSLionel Sambuc int negate, ok, need;
772fe8fb19SBen Gras char c, c2;
782fe8fb19SBen Gras
792fe8fb19SBen Gras _DIAGASSERT(pattern != NULL);
802fe8fb19SBen Gras
812fe8fb19SBen Gras /*
822fe8fb19SBen Gras * A bracket expression starting with an unquoted circumflex
832fe8fb19SBen Gras * character produces unspecified results (IEEE 1003.2-1992,
842fe8fb19SBen Gras * 3.13.2). This implementation treats it like '!', for
852fe8fb19SBen Gras * consistency with the regular expression syntax.
862fe8fb19SBen Gras * J.T. Conklin (conklin@ngai.kaleida.com)
872fe8fb19SBen Gras */
882fe8fb19SBen Gras if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
892fe8fb19SBen Gras ++pattern;
902fe8fb19SBen Gras
91*0a6a1f1dSLionel Sambuc need = 1;
92*0a6a1f1dSLionel Sambuc for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']' || need;) {
93*0a6a1f1dSLionel Sambuc need = 0;
94*0a6a1f1dSLionel Sambuc if (c == '/')
95*0a6a1f1dSLionel Sambuc return (void *)-1;
962fe8fb19SBen Gras if (c == '\\' && !(flags & FNM_NOESCAPE))
972fe8fb19SBen Gras c = FOLDCASE(*pattern++, flags);
982fe8fb19SBen Gras if (c == EOS)
99f14fb602SLionel Sambuc return NULL;
1002fe8fb19SBen Gras if (*pattern == '-'
1012fe8fb19SBen Gras && (c2 = FOLDCASE(*(pattern + 1), flags)) != EOS &&
1022fe8fb19SBen Gras c2 != ']') {
1032fe8fb19SBen Gras pattern += 2;
1042fe8fb19SBen Gras if (c2 == '\\' && !(flags & FNM_NOESCAPE))
1052fe8fb19SBen Gras c2 = FOLDCASE(*pattern++, flags);
1062fe8fb19SBen Gras if (c2 == EOS)
107f14fb602SLionel Sambuc return NULL;
1082fe8fb19SBen Gras if (c <= test && test <= c2)
1092fe8fb19SBen Gras ok = 1;
1102fe8fb19SBen Gras } else if (c == test)
1112fe8fb19SBen Gras ok = 1;
1122fe8fb19SBen Gras }
113f14fb602SLionel Sambuc return ok == negate ? NULL : pattern;
114f14fb602SLionel Sambuc }
115f14fb602SLionel Sambuc
116f14fb602SLionel Sambuc
117f14fb602SLionel Sambuc static int
fnmatchx(const char * pattern,const char * string,int flags,size_t recursion)118f14fb602SLionel Sambuc fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
119f14fb602SLionel Sambuc {
120*0a6a1f1dSLionel Sambuc const char *stringstart, *r;
121f14fb602SLionel Sambuc char c, test;
122f14fb602SLionel Sambuc
123f14fb602SLionel Sambuc _DIAGASSERT(pattern != NULL);
124f14fb602SLionel Sambuc _DIAGASSERT(string != NULL);
125f14fb602SLionel Sambuc
126f14fb602SLionel Sambuc if (recursion-- == 0)
127f14fb602SLionel Sambuc return FNM_NORES;
128f14fb602SLionel Sambuc
129f14fb602SLionel Sambuc for (stringstart = string;;) {
130f14fb602SLionel Sambuc switch (c = FOLDCASE(*pattern++, flags)) {
131f14fb602SLionel Sambuc case EOS:
132f14fb602SLionel Sambuc if ((flags & FNM_LEADING_DIR) && *string == '/')
133f14fb602SLionel Sambuc return 0;
134f14fb602SLionel Sambuc return *string == EOS ? 0 : FNM_NOMATCH;
135f14fb602SLionel Sambuc case '?':
136f14fb602SLionel Sambuc if (*string == EOS)
137f14fb602SLionel Sambuc return FNM_NOMATCH;
138f14fb602SLionel Sambuc if (*string == '/' && (flags & FNM_PATHNAME))
139f14fb602SLionel Sambuc return FNM_NOMATCH;
140f14fb602SLionel Sambuc if (*string == '.' && (flags & FNM_PERIOD) &&
141f14fb602SLionel Sambuc (string == stringstart ||
142f14fb602SLionel Sambuc ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
143f14fb602SLionel Sambuc return FNM_NOMATCH;
144f14fb602SLionel Sambuc ++string;
145f14fb602SLionel Sambuc break;
146f14fb602SLionel Sambuc case '*':
147f14fb602SLionel Sambuc c = FOLDCASE(*pattern, flags);
148f14fb602SLionel Sambuc /* Collapse multiple stars. */
149f14fb602SLionel Sambuc while (c == '*')
150f14fb602SLionel Sambuc c = FOLDCASE(*++pattern, flags);
151f14fb602SLionel Sambuc
152f14fb602SLionel Sambuc if (*string == '.' && (flags & FNM_PERIOD) &&
153f14fb602SLionel Sambuc (string == stringstart ||
154f14fb602SLionel Sambuc ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
155f14fb602SLionel Sambuc return FNM_NOMATCH;
156f14fb602SLionel Sambuc
157f14fb602SLionel Sambuc /* Optimize for pattern with * at end or before /. */
158f14fb602SLionel Sambuc if (c == EOS) {
159f14fb602SLionel Sambuc if (flags & FNM_PATHNAME)
160f14fb602SLionel Sambuc return (flags & FNM_LEADING_DIR) ||
161f14fb602SLionel Sambuc strchr(string, '/') == NULL ?
162f14fb602SLionel Sambuc 0 : FNM_NOMATCH;
163f14fb602SLionel Sambuc else
164f14fb602SLionel Sambuc return 0;
165f14fb602SLionel Sambuc } else if (c == '/' && flags & FNM_PATHNAME) {
166f14fb602SLionel Sambuc if ((string = strchr(string, '/')) == NULL)
167f14fb602SLionel Sambuc return FNM_NOMATCH;
168f14fb602SLionel Sambuc break;
169f14fb602SLionel Sambuc }
170f14fb602SLionel Sambuc
171f14fb602SLionel Sambuc /* General case, use recursion. */
172f14fb602SLionel Sambuc while ((test = FOLDCASE(*string, flags)) != EOS) {
173f14fb602SLionel Sambuc int e;
174f14fb602SLionel Sambuc switch ((e = fnmatchx(pattern, string,
175f14fb602SLionel Sambuc flags & ~FNM_PERIOD, recursion))) {
176f14fb602SLionel Sambuc case FNM_NOMATCH:
177f14fb602SLionel Sambuc break;
178f14fb602SLionel Sambuc default:
179f14fb602SLionel Sambuc return e;
180f14fb602SLionel Sambuc }
181f14fb602SLionel Sambuc if (test == '/' && flags & FNM_PATHNAME)
182f14fb602SLionel Sambuc break;
183f14fb602SLionel Sambuc ++string;
184f14fb602SLionel Sambuc }
185f14fb602SLionel Sambuc return FNM_NOMATCH;
186f14fb602SLionel Sambuc case '[':
187f14fb602SLionel Sambuc if (*string == EOS)
188f14fb602SLionel Sambuc return FNM_NOMATCH;
189f14fb602SLionel Sambuc if (*string == '/' && flags & FNM_PATHNAME)
190f14fb602SLionel Sambuc return FNM_NOMATCH;
191*0a6a1f1dSLionel Sambuc if ((r = rangematch(pattern,
192f14fb602SLionel Sambuc FOLDCASE(*string, flags), flags)) == NULL)
193f14fb602SLionel Sambuc return FNM_NOMATCH;
194*0a6a1f1dSLionel Sambuc if (r == (void *)-1) {
195*0a6a1f1dSLionel Sambuc if (*string != '[')
196*0a6a1f1dSLionel Sambuc return FNM_NOMATCH;
197*0a6a1f1dSLionel Sambuc } else
198*0a6a1f1dSLionel Sambuc pattern = r;
199f14fb602SLionel Sambuc ++string;
200f14fb602SLionel Sambuc break;
201f14fb602SLionel Sambuc case '\\':
202f14fb602SLionel Sambuc if (!(flags & FNM_NOESCAPE)) {
203f14fb602SLionel Sambuc if ((c = FOLDCASE(*pattern++, flags)) == EOS) {
204f14fb602SLionel Sambuc c = '\0';
205f14fb602SLionel Sambuc --pattern;
206f14fb602SLionel Sambuc }
207f14fb602SLionel Sambuc }
208f14fb602SLionel Sambuc /* FALLTHROUGH */
209f14fb602SLionel Sambuc default:
210f14fb602SLionel Sambuc if (c != FOLDCASE(*string++, flags))
211f14fb602SLionel Sambuc return FNM_NOMATCH;
212f14fb602SLionel Sambuc break;
213f14fb602SLionel Sambuc }
214f14fb602SLionel Sambuc }
215f14fb602SLionel Sambuc /* NOTREACHED */
216f14fb602SLionel Sambuc }
217f14fb602SLionel Sambuc
218f14fb602SLionel Sambuc int
fnmatch(const char * pattern,const char * string,int flags)219f14fb602SLionel Sambuc fnmatch(const char *pattern, const char *string, int flags)
220f14fb602SLionel Sambuc {
221f14fb602SLionel Sambuc return fnmatchx(pattern, string, flags, 64);
2222fe8fb19SBen Gras }
223