Merge commit 'upstream/1.7.6p1'
[debian/sudo] / fnmatch.c
1 /*
2  * Copyright (c) 2008, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  * Copyright (c) 1989, 1993, 1994
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Guido van Rossum.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
36  * Compares a filename or pathname to a pattern.
37  */
38
39 #include <config.h>
40
41 #include <sys/types.h>
42
43 #include <stdio.h>
44 #include <ctype.h>
45 #ifdef HAVE_STRING_H
46 # include <string.h>
47 #endif /* HAVE_STRING_H */
48 #ifdef HAVE_STRINGS_H
49 # include <strings.h>
50 #endif /* HAVE_STRINGS_H */
51
52 #include "missing.h"
53 #include "emul/fnmatch.h"
54 #include "emul/charclass.h"
55
56 #undef  EOS
57 #define EOS     '\0'
58
59 #define RANGE_MATCH     1
60 #define RANGE_NOMATCH   0
61 #define RANGE_ERROR     (-1)
62
63 #if defined(LIBC_SCCS) && !defined(lint)
64 __unused static const char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
65 #endif /* LIBC_SCCS and not lint */
66
67 static int rangematch __P((const char *, int, int, char **));
68 static int classmatch __P((const char *, int, int, const char **));
69
70 int
71 fnmatch(pattern, string, flags)
72         const char *pattern, *string;
73         int flags;
74 {
75         const char *stringstart;
76         char *newp;
77         char c, test;
78
79         for (stringstart = string;;)
80                 switch (c = *pattern++) {
81                 case EOS:
82                         if (ISSET(flags, FNM_LEADING_DIR) && *string == '/')
83                                 return 0;
84                         return *string == EOS ? 0 : FNM_NOMATCH;
85                 case '?':
86                         if (*string == EOS)
87                                 return FNM_NOMATCH;
88                         if (*string == '/' && ISSET(flags, FNM_PATHNAME))
89                                 return FNM_NOMATCH;
90                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
91                             (string == stringstart ||
92                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
93                                 return FNM_NOMATCH;
94                         ++string;
95                         break;
96                 case '*':
97                         c = *pattern;
98                         /* Collapse multiple stars. */
99                         while (c == '*')
100                                 c = *++pattern;
101
102                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
103                             (string == stringstart ||
104                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
105                                 return FNM_NOMATCH;
106
107                         /* Optimize for pattern with * at end or before /. */
108                         if (c == EOS) {
109                                 if (ISSET(flags, FNM_PATHNAME))
110                                         return ISSET(flags, FNM_LEADING_DIR) ||
111                                             strchr(string, '/') == NULL ?
112                                             0 : FNM_NOMATCH;
113                                 else
114                                         return 0;
115                         } else if (c == '/' && ISSET(flags, FNM_PATHNAME)) {
116                                 if ((string = strchr(string, '/')) == NULL)
117                                         return FNM_NOMATCH;
118                                 break;
119                         }
120
121                         /* General case, use recursion. */
122                         while ((test = *string) != EOS) {
123                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
124                                         return 0;
125                                 if (test == '/' && ISSET(flags, FNM_PATHNAME))
126                                         break;
127                                 ++string;
128                         }
129                         return FNM_NOMATCH;
130                 case '[':
131                         if (*string == EOS)
132                                 return FNM_NOMATCH;
133                         if (*string == '/' && ISSET(flags, FNM_PATHNAME))
134                                 return FNM_NOMATCH;
135                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
136                             (string == stringstart ||
137                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
138                                 return FNM_NOMATCH;
139
140                         switch (rangematch(pattern, *string, flags, &newp)) {
141                         case RANGE_ERROR:
142                                 /* not a good range, treat as normal text */
143                                 goto normal;
144                         case RANGE_MATCH:
145                                 pattern = newp;
146                                 break;
147                         case RANGE_NOMATCH:
148                                 return FNM_NOMATCH;
149                         }
150                         ++string;
151                         break;
152                 case '\\':
153                         if (!ISSET(flags, FNM_NOESCAPE)) {
154                                 if ((c = *pattern++) == EOS) {
155                                         c = '\\';
156                                         --pattern;
157                                 }
158                         }
159                         /* FALLTHROUGH */
160                 default:
161                 normal:
162                         if (c != *string && !(ISSET(flags, FNM_CASEFOLD) &&
163                                  (tolower((unsigned char)c) ==
164                                  tolower((unsigned char)*string))))
165                                 return FNM_NOMATCH;
166                         ++string;
167                         break;
168                 }
169         /* NOTREACHED */
170 }
171
172 static int
173 #ifdef __STDC__
174 rangematch(const char *pattern, int test, int flags, char **newp)
175 #else
176 rangematch(pattern, test, flags, newp)
177         const char *pattern;
178         int test;
179         int flags;
180         char **newp;
181 #endif
182 {
183         int negate, ok, rv;
184         char c, c2;
185
186         /*
187          * A bracket expression starting with an unquoted circumflex
188          * character produces unspecified results (IEEE 1003.2-1992,
189          * 3.13.2).  This implementation treats it like '!', for
190          * consistency with the regular expression syntax.
191          * J.T. Conklin (conklin@ngai.kaleida.com)
192          */
193         if ((negate = (*pattern == '!' || *pattern == '^')))
194                 ++pattern;
195
196         if (ISSET(flags, FNM_CASEFOLD))
197                 test = tolower(test);
198
199         /*
200          * A right bracket shall lose its special meaning and represent
201          * itself in a bracket expression if it occurs first in the list.
202          * -- POSIX.2 2.8.3.2
203          */
204         ok = 0;
205         c = *pattern++;
206         do {
207                 if (c == '[' && *pattern == ':') {
208                         do {
209                                 rv = classmatch(pattern + 1, test,
210                                     (flags & FNM_CASEFOLD), &pattern);
211                                 if (rv == RANGE_MATCH)
212                                         ok = 1;
213                                 c = *pattern++;
214                         } while (rv != RANGE_ERROR && c == '[' && *pattern == ':');
215                         if (c == ']')
216                         break;
217                 }
218                 if (c == '\\' && !ISSET(flags, FNM_NOESCAPE))
219                         c = *pattern++;
220                 if (c == EOS)
221                         return RANGE_ERROR;
222                 if (c == '/' && ISSET(flags, FNM_PATHNAME))
223                         return RANGE_NOMATCH;
224                 if (ISSET(flags, FNM_CASEFOLD))
225                         c = tolower((unsigned char)c);
226                 if (*pattern == '-'
227                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
228                         pattern += 2;
229                         if (c2 == '\\' && !ISSET(flags, FNM_NOESCAPE))
230                                 c2 = *pattern++;
231                         if (c2 == EOS)
232                                 return RANGE_ERROR;
233                         if (ISSET(flags, FNM_CASEFOLD))
234                                 c2 = tolower((unsigned char)c2);
235                         if (c <= test && test <= c2)
236                                 ok = 1;
237                 } else if (c == test)
238                         ok = 1;
239         } while ((c = *pattern++) != ']');
240
241         *newp = (char *)pattern;
242         return ok == negate ? RANGE_NOMATCH : RANGE_MATCH;
243 }
244
245 static int
246 #ifdef __STDC__
247 classmatch(const char *pattern, int test, int foldcase, const char **ep)
248 #else
249 classmatch(pattern, test, foldcase, ep)
250         const char *pattern;
251         int test;
252         int foldcase;
253         const char **ep;
254 #endif
255 {
256         struct cclass *cc;
257         const char *colon;
258         size_t len;
259         int rval = RANGE_NOMATCH;
260
261         if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
262                 *ep = pattern - 2;
263                 return RANGE_ERROR;
264         }
265         *ep = colon + 2;
266         len = (size_t)(colon - pattern);
267
268         if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
269                 pattern = "lower:]";
270         for (cc = cclasses; cc->name != NULL; cc++) {
271                 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
272                         if (cc->isctype(test))
273                                 rval = RANGE_MATCH;
274                         break;
275                 }
276         }
277         if (cc->name == NULL) {
278                 /* invalid character class, return EOS */
279                 *ep = colon + strlen(colon);
280                 rval = RANGE_ERROR;
281         }
282         return rval;
283 }