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