1b528cefcSMark Murray /* $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $ */
2b528cefcSMark Murray
3b528cefcSMark Murray /*
4b528cefcSMark Murray * Copyright (c) 1989, 1993, 1994
5b528cefcSMark Murray * The Regents of the University of California. All rights reserved.
6b528cefcSMark Murray *
7b528cefcSMark Murray * This code is derived from software contributed to Berkeley by
8b528cefcSMark Murray * Guido van Rossum.
9b528cefcSMark Murray *
10b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
11b528cefcSMark Murray * modification, are permitted provided that the following conditions
12b528cefcSMark Murray * are met:
13b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
15b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
16b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
17b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
18c19800e8SDoug Rabson * 3. Neither the name of the University nor the names of its contributors
19b528cefcSMark Murray * may be used to endorse or promote products derived from this software
20b528cefcSMark Murray * without specific prior written permission.
21b528cefcSMark Murray *
22b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b528cefcSMark Murray * SUCH DAMAGE.
33b528cefcSMark Murray */
34b528cefcSMark Murray
35b528cefcSMark Murray #if defined(LIBC_SCCS) && !defined(lint)
36b528cefcSMark Murray #if 0
37b528cefcSMark Murray static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
38b528cefcSMark Murray #else
39b528cefcSMark Murray static char rcsid[] = "$NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $";
40b528cefcSMark Murray #endif
41b528cefcSMark Murray #endif /* LIBC_SCCS and not lint */
42b528cefcSMark Murray
43b528cefcSMark Murray /*
44b528cefcSMark Murray * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
45b528cefcSMark Murray * Compares a filename or pathname to a pattern.
46b528cefcSMark Murray */
47b528cefcSMark Murray
48*ae771770SStanislav Sedov #ifdef HAVE_CONFIG_H
49*ae771770SStanislav Sedov #include <config.h>
50*ae771770SStanislav Sedov #endif
51*ae771770SStanislav Sedov
52*ae771770SStanislav Sedov #include <roken.h>
53*ae771770SStanislav Sedov
54b528cefcSMark Murray #include <fnmatch.h>
55b528cefcSMark Murray #include <string.h>
56b528cefcSMark Murray
57b528cefcSMark Murray #define EOS '\0'
58b528cefcSMark Murray
59b528cefcSMark Murray static const char *rangematch (const char *, int, int);
60b528cefcSMark Murray
61*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_fnmatch(const char * pattern,const char * string,int flags)62c19800e8SDoug Rabson rk_fnmatch(const char *pattern, const char *string, int flags)
63b528cefcSMark Murray {
64b528cefcSMark Murray const char *stringstart;
65b528cefcSMark Murray char c, test;
66b528cefcSMark Murray
67b528cefcSMark Murray for (stringstart = string;;)
68b528cefcSMark Murray switch (c = *pattern++) {
69b528cefcSMark Murray case EOS:
70b528cefcSMark Murray return (*string == EOS ? 0 : FNM_NOMATCH);
71b528cefcSMark Murray case '?':
72b528cefcSMark Murray if (*string == EOS)
73b528cefcSMark Murray return (FNM_NOMATCH);
74b528cefcSMark Murray if (*string == '/' && (flags & FNM_PATHNAME))
75b528cefcSMark Murray return (FNM_NOMATCH);
76b528cefcSMark Murray if (*string == '.' && (flags & FNM_PERIOD) &&
77b528cefcSMark Murray (string == stringstart ||
78b528cefcSMark Murray ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
79b528cefcSMark Murray return (FNM_NOMATCH);
80b528cefcSMark Murray ++string;
81b528cefcSMark Murray break;
82b528cefcSMark Murray case '*':
83b528cefcSMark Murray c = *pattern;
84b528cefcSMark Murray /* Collapse multiple stars. */
85b528cefcSMark Murray while (c == '*')
86b528cefcSMark Murray c = *++pattern;
87b528cefcSMark Murray
88b528cefcSMark Murray if (*string == '.' && (flags & FNM_PERIOD) &&
89b528cefcSMark Murray (string == stringstart ||
90b528cefcSMark Murray ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
91b528cefcSMark Murray return (FNM_NOMATCH);
92b528cefcSMark Murray
93b528cefcSMark Murray /* Optimize for pattern with * at end or before /. */
94b528cefcSMark Murray if (c == EOS)
95b528cefcSMark Murray if (flags & FNM_PATHNAME)
96b528cefcSMark Murray return (strchr(string, '/') == NULL ?
97b528cefcSMark Murray 0 : FNM_NOMATCH);
98b528cefcSMark Murray else
99b528cefcSMark Murray return (0);
100b528cefcSMark Murray else if (c == '/' && flags & FNM_PATHNAME) {
101b528cefcSMark Murray if ((string = strchr(string, '/')) == NULL)
102b528cefcSMark Murray return (FNM_NOMATCH);
103b528cefcSMark Murray break;
104b528cefcSMark Murray }
105b528cefcSMark Murray
106b528cefcSMark Murray /* General case, use recursion. */
107b528cefcSMark Murray while ((test = *string) != EOS) {
108c19800e8SDoug Rabson if (!rk_fnmatch(pattern, string, flags & ~FNM_PERIOD))
109b528cefcSMark Murray return (0);
110b528cefcSMark Murray if (test == '/' && flags & FNM_PATHNAME)
111b528cefcSMark Murray break;
112b528cefcSMark Murray ++string;
113b528cefcSMark Murray }
114b528cefcSMark Murray return (FNM_NOMATCH);
115b528cefcSMark Murray case '[':
116b528cefcSMark Murray if (*string == EOS)
117b528cefcSMark Murray return (FNM_NOMATCH);
118b528cefcSMark Murray if (*string == '/' && flags & FNM_PATHNAME)
119b528cefcSMark Murray return (FNM_NOMATCH);
120b528cefcSMark Murray if ((pattern =
121b528cefcSMark Murray rangematch(pattern, *string, flags)) == NULL)
122b528cefcSMark Murray return (FNM_NOMATCH);
123b528cefcSMark Murray ++string;
124b528cefcSMark Murray break;
125b528cefcSMark Murray case '\\':
126b528cefcSMark Murray if (!(flags & FNM_NOESCAPE)) {
127b528cefcSMark Murray if ((c = *pattern++) == EOS) {
128b528cefcSMark Murray c = '\\';
129b528cefcSMark Murray --pattern;
130b528cefcSMark Murray }
131b528cefcSMark Murray }
132b528cefcSMark Murray /* FALLTHROUGH */
133b528cefcSMark Murray default:
134b528cefcSMark Murray if (c != *string++)
135b528cefcSMark Murray return (FNM_NOMATCH);
136b528cefcSMark Murray break;
137b528cefcSMark Murray }
138b528cefcSMark Murray /* NOTREACHED */
139b528cefcSMark Murray }
140b528cefcSMark Murray
141b528cefcSMark Murray static const char *
rangematch(const char * pattern,int test,int flags)142b528cefcSMark Murray rangematch(const char *pattern, int test, int flags)
143b528cefcSMark Murray {
144b528cefcSMark Murray int negate, ok;
145b528cefcSMark Murray char c, c2;
146b528cefcSMark Murray
147b528cefcSMark Murray /*
148b528cefcSMark Murray * A bracket expression starting with an unquoted circumflex
149b528cefcSMark Murray * character produces unspecified results (IEEE 1003.2-1992,
150b528cefcSMark Murray * 3.13.2). This implementation treats it like '!', for
151b528cefcSMark Murray * consistency with the regular expression syntax.
152b528cefcSMark Murray * J.T. Conklin (conklin@ngai.kaleida.com)
153b528cefcSMark Murray */
154b528cefcSMark Murray if (negate = (*pattern == '!' || *pattern == '^'))
155b528cefcSMark Murray ++pattern;
156b528cefcSMark Murray
157b528cefcSMark Murray for (ok = 0; (c = *pattern++) != ']';) {
158b528cefcSMark Murray if (c == '\\' && !(flags & FNM_NOESCAPE))
159b528cefcSMark Murray c = *pattern++;
160b528cefcSMark Murray if (c == EOS)
161b528cefcSMark Murray return (NULL);
162b528cefcSMark Murray if (*pattern == '-'
163b528cefcSMark Murray && (c2 = *(pattern+1)) != EOS && c2 != ']') {
164b528cefcSMark Murray pattern += 2;
165b528cefcSMark Murray if (c2 == '\\' && !(flags & FNM_NOESCAPE))
166b528cefcSMark Murray c2 = *pattern++;
167b528cefcSMark Murray if (c2 == EOS)
168b528cefcSMark Murray return (NULL);
169b528cefcSMark Murray if (c <= test && test <= c2)
170b528cefcSMark Murray ok = 1;
171b528cefcSMark Murray } else if (c == test)
172b528cefcSMark Murray ok = 1;
173b528cefcSMark Murray }
174b528cefcSMark Murray return (ok == negate ? NULL : pattern);
175b528cefcSMark Murray }
176