fnmatch.c revision 1.11.4.1 1 /* $NetBSD: fnmatch.c,v 1.11.4.1 1996/09/19 20:02:33 jtc 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 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
42 #else
43 static char rcsid[] = "$NetBSD: fnmatch.c,v 1.11.4.1 1996/09/19 20:02:33 jtc Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46
47 /*
48 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
49 * Compares a filename or pathname to a pattern.
50 */
51
52 #include "namespace.h"
53 #include <fnmatch.h>
54 #include <string.h>
55
56 #ifdef __weak_alias
57 __weak_alias(fnmatch,_fnmatch);
58 #endif
59
60 #define EOS '\0'
61
62 static const char *rangematch __P((const char *, int, int));
63
64 int
65 fnmatch(pattern, string, flags)
66 const char *pattern, *string;
67 int flags;
68 {
69 const char *stringstart;
70 char c, test;
71
72 for (stringstart = string;;)
73 switch (c = *pattern++) {
74 case EOS:
75 return (*string == EOS ? 0 : FNM_NOMATCH);
76 case '?':
77 if (*string == EOS)
78 return (FNM_NOMATCH);
79 if (*string == '/' && (flags & FNM_PATHNAME))
80 return (FNM_NOMATCH);
81 if (*string == '.' && (flags & FNM_PERIOD) &&
82 (string == stringstart ||
83 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
84 return (FNM_NOMATCH);
85 ++string;
86 break;
87 case '*':
88 c = *pattern;
89 /* Collapse multiple stars. */
90 while (c == '*')
91 c = *++pattern;
92
93 if (*string == '.' && (flags & FNM_PERIOD) &&
94 (string == stringstart ||
95 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
96 return (FNM_NOMATCH);
97
98 /* Optimize for pattern with * at end or before /. */
99 if (c == EOS)
100 if (flags & FNM_PATHNAME)
101 return (strchr(string, '/') == NULL ?
102 0 : FNM_NOMATCH);
103 else
104 return (0);
105 else if (c == '/' && flags & FNM_PATHNAME) {
106 if ((string = strchr(string, '/')) == NULL)
107 return (FNM_NOMATCH);
108 break;
109 }
110
111 /* General case, use recursion. */
112 while ((test = *string) != EOS) {
113 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
114 return (0);
115 if (test == '/' && flags & FNM_PATHNAME)
116 break;
117 ++string;
118 }
119 return (FNM_NOMATCH);
120 case '[':
121 if (*string == EOS)
122 return (FNM_NOMATCH);
123 if (*string == '/' && flags & FNM_PATHNAME)
124 return (FNM_NOMATCH);
125 if ((pattern =
126 rangematch(pattern, *string, flags)) == NULL)
127 return (FNM_NOMATCH);
128 ++string;
129 break;
130 case '\\':
131 if (!(flags & FNM_NOESCAPE)) {
132 if ((c = *pattern++) == EOS) {
133 c = '\\';
134 --pattern;
135 }
136 }
137 /* FALLTHROUGH */
138 default:
139 if (c != *string++)
140 return (FNM_NOMATCH);
141 break;
142 }
143 /* NOTREACHED */
144 }
145
146 static const char *
147 rangematch(pattern, test, flags)
148 const char *pattern;
149 int test, flags;
150 {
151 int negate, ok;
152 char c, c2;
153
154 /*
155 * A bracket expression starting with an unquoted circumflex
156 * character produces unspecified results (IEEE 1003.2-1992,
157 * 3.13.2). This implementation treats it like '!', for
158 * consistency with the regular expression syntax.
159 * J.T. Conklin (conklin (at) ngai.kaleida.com)
160 */
161 if (negate = (*pattern == '!' || *pattern == '^'))
162 ++pattern;
163
164 for (ok = 0; (c = *pattern++) != ']';) {
165 if (c == '\\' && !(flags & FNM_NOESCAPE))
166 c = *pattern++;
167 if (c == EOS)
168 return (NULL);
169 if (*pattern == '-'
170 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
171 pattern += 2;
172 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
173 c2 = *pattern++;
174 if (c2 == EOS)
175 return (NULL);
176 if (c <= test && test <= c2)
177 ok = 1;
178 } else if (c == test)
179 ok = 1;
180 }
181 return (ok == negate ? NULL : pattern);
182 }
183