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