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