kernel: Fix the build of the SOEKRIS kernel config.
[dragonfly.git] / sys / libkern / fnmatch.c
blobbb99e5807bcad81d78e6cb2d632f778e1bed9db6
1 /*
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
6 * Guido van Rossum.
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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * $FreeBSD: src/sys/libkern/fnmatch.c,v 1.17 2003/06/11 05:23:04 obrien Exp $
37 * $DragonFly: src/sys/libkern/fnmatch.c,v 1.3 2006/12/13 21:58:51 dillon Exp $
41 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
42 * Compares a filename or pathname to a pattern.
45 #include <sys/param.h>
46 #include <sys/ctype.h>
47 #include <sys/libkern.h>
49 #define EOS '\0'
51 #define RANGE_MATCH 1
52 #define RANGE_NOMATCH 0
53 #define RANGE_ERROR (-1)
55 #define MAXNEST 20
57 static int rangematch(const char *, char, int, char **);
59 int
60 _kfnmatch(const char *pattern, const char *string, int flags, int nesting)
62 const char *stringstart;
63 char *newp;
64 char c, test;
66 if (nesting == MAXNEST)
67 return (FNM_NOMATCH);
69 for (stringstart = string;;) {
70 switch (c = *pattern++) {
71 case EOS:
72 if ((flags & FNM_LEADING_DIR) && *string == '/')
73 return (0);
74 return (*string == EOS ? 0 : FNM_NOMATCH);
75 case '?':
76 if (*string == EOS)
77 return (FNM_NOMATCH);
78 if (*string == '/' && (flags & FNM_PATHNAME))
79 return (FNM_NOMATCH);
80 if (*string == '.' && (flags & FNM_PERIOD) &&
81 (string == stringstart ||
82 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
83 return (FNM_NOMATCH);
85 ++string;
86 break;
87 case '*':
88 c = *pattern;
89 /* Collapse multiple stars. */
90 while (c == '*')
91 c = *++pattern;
93 if (*string == '.' && (flags & FNM_PERIOD) &&
94 (string == stringstart ||
95 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
96 return (FNM_NOMATCH);
99 /* Optimize for pattern with * at end or before /. */
100 if (c == EOS) {
101 if (flags & FNM_PATHNAME) {
102 return ((flags & FNM_LEADING_DIR) ||
103 index(string, '/') == NULL ?
104 0 : FNM_NOMATCH);
105 } else {
106 return (0);
108 } else if (c == '/' && flags & FNM_PATHNAME) {
109 if ((string = index(string, '/')) == NULL)
110 return (FNM_NOMATCH);
111 break;
114 /* General case, use recursion. */
115 while ((test = *string) != EOS) {
116 if (!_kfnmatch(pattern, string, flags & ~FNM_PERIOD, nesting + 1))
117 return (0);
118 if (test == '/' && flags & FNM_PATHNAME)
119 break;
120 ++string;
122 return (FNM_NOMATCH);
123 case '[':
124 if (*string == EOS)
125 return (FNM_NOMATCH);
126 if (*string == '/' && (flags & FNM_PATHNAME))
127 return (FNM_NOMATCH);
128 if (*string == '.' && (flags & FNM_PERIOD) &&
129 (string == stringstart ||
130 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
131 return (FNM_NOMATCH);
133 switch (rangematch(pattern, *string, flags, &newp)) {
134 case RANGE_ERROR:
135 goto norm;
136 case RANGE_MATCH:
137 pattern = newp;
138 break;
139 case RANGE_NOMATCH:
140 return (FNM_NOMATCH);
142 ++string;
143 break;
144 case '\\':
145 if (!(flags & FNM_NOESCAPE)) {
146 if ((c = *pattern++) == EOS) {
147 c = '\\';
148 --pattern;
151 /* FALLTHROUGH */
152 default:
153 norm:
154 if (c == *string) {
156 } else if ((flags & FNM_CASEFOLD) &&
157 (tolower((unsigned char)c) ==
158 tolower((unsigned char)*string))) {
160 } else {
161 return (FNM_NOMATCH);
163 string++;
164 break;
167 /* NOTREACHED */
170 static int
171 rangematch(const char *pattern, char test, int flags, char **newp)
173 int negate, ok;
174 char c, c2;
177 * A bracket expression starting with an unquoted circumflex
178 * character produces unspecified results (IEEE 1003.2-1992,
179 * 3.13.2). This implementation treats it like '!', for
180 * consistency with the regular expression syntax.
181 * J.T. Conklin (conklin@ngai.kaleida.com)
183 if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
184 ++pattern;
186 if (flags & FNM_CASEFOLD)
187 test = tolower((unsigned char)test);
190 * A right bracket shall lose its special meaning and represent
191 * itself in a bracket expression if it occurs first in the list.
192 * -- POSIX.2 2.8.3.2
194 ok = 0;
195 c = *pattern++;
196 do {
197 if (c == '\\' && !(flags & FNM_NOESCAPE))
198 c = *pattern++;
199 if (c == EOS)
200 return (RANGE_ERROR);
202 if (c == '/' && (flags & FNM_PATHNAME))
203 return (RANGE_NOMATCH);
205 if (flags & FNM_CASEFOLD)
206 c = tolower((unsigned char)c);
208 if (*pattern == '-'
209 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
210 pattern += 2;
211 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
212 c2 = *pattern++;
213 if (c2 == EOS)
214 return (RANGE_ERROR);
216 if (flags & FNM_CASEFOLD)
217 c2 = tolower((unsigned char)c2);
219 if (c <= test && test <= c2)
220 ok = 1;
221 } else if (c == test) {
222 ok = 1;
224 } while ((c = *pattern++) != ']');
226 *newp = (char *)(uintptr_t)pattern;
227 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);