update init.d to clean new state location /var/lib/sudo, prepare to upload
[debian/sudo] / fnmatch.c
1 /*
2  * Copyright (c) 2008 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 <stdio.h>
42 #include <ctype.h>
43 #ifdef HAVE_STRING_H
44 # include <string.h>
45 #else
46 # ifdef HAVE_STRINGS_H
47 #  include <strings.h>
48 # endif
49 #endif /* HAVE_STRING_H */
50
51 #include <compat.h>
52 #include "emul/fnmatch.h"
53 #include "emul/charclass.h"
54
55 #undef  EOS
56 #define EOS     '\0'
57
58 #define RANGE_MATCH     1
59 #define RANGE_NOMATCH   0
60 #define RANGE_ERROR     (-1)
61
62 #if defined(LIBC_SCCS) && !defined(lint)
63 __unused static const char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
64 #endif /* LIBC_SCCS and not lint */
65
66 static int rangematch __P((const char *, int, int, char **));
67 static int classmatch __P((const char *, int, int, const char **));
68
69 int
70 fnmatch(pattern, string, flags)
71         const char *pattern, *string;
72         int flags;
73 {
74         const char *stringstart;
75         char *newp;
76         char c, test;
77
78         for (stringstart = string;;)
79                 switch (c = *pattern++) {
80                 case EOS:
81                         if (ISSET(flags, FNM_LEADING_DIR) && *string == '/')
82                                 return (0);
83                         return (*string == EOS ? 0 : FNM_NOMATCH);
84                 case '?':
85                         if (*string == EOS)
86                                 return (FNM_NOMATCH);
87                         if (*string == '/' && ISSET(flags, FNM_PATHNAME))
88                                 return (FNM_NOMATCH);
89                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
90                             (string == stringstart ||
91                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
92                                 return (FNM_NOMATCH);
93                         ++string;
94                         break;
95                 case '*':
96                         c = *pattern;
97                         /* Collapse multiple stars. */
98                         while (c == '*')
99                                 c = *++pattern;
100
101                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
102                             (string == stringstart ||
103                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
104                                 return (FNM_NOMATCH);
105
106                         /* Optimize for pattern with * at end or before /. */
107                         if (c == EOS) {
108                                 if (ISSET(flags, FNM_PATHNAME))
109                                         return (ISSET(flags, FNM_LEADING_DIR) ||
110                                             strchr(string, '/') == NULL ?
111                                             0 : FNM_NOMATCH);
112                                 else
113                                         return (0);
114                         } else if (c == '/' && ISSET(flags, FNM_PATHNAME)) {
115                                 if ((string = strchr(string, '/')) == NULL)
116                                         return (FNM_NOMATCH);
117                                 break;
118                         }
119
120                         /* General case, use recursion. */
121                         while ((test = *string) != EOS) {
122                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
123                                         return (0);
124                                 if (test == '/' && ISSET(flags, FNM_PATHNAME))
125                                         break;
126                                 ++string;
127                         }
128                         return (FNM_NOMATCH);
129                 case '[':
130                         if (*string == EOS)
131                                 return (FNM_NOMATCH);
132                         if (*string == '/' && ISSET(flags, FNM_PATHNAME))
133                                 return (FNM_NOMATCH);
134                         if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
135                             (string == stringstart ||
136                             (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
137                                 return (FNM_NOMATCH);
138
139                         switch (rangematch(pattern, *string, flags, &newp)) {
140                         case RANGE_ERROR:
141                                 /* not a good range, treat as normal text */
142                                 goto normal;
143                         case RANGE_MATCH:
144                                 pattern = newp;
145                                 break;
146                         case RANGE_NOMATCH:
147                                 return (FNM_NOMATCH);
148                         }
149                         ++string;
150                         break;
151                 case '\\':
152                         if (!ISSET(flags, FNM_NOESCAPE)) {
153                                 if ((c = *pattern++) == EOS) {
154                                         c = '\\';
155                                         --pattern;
156                                 }
157                         }
158                         /* FALLTHROUGH */
159                 default:
160                 normal:
161                         if (c != *string && !(ISSET(flags, FNM_CASEFOLD) &&
162                                  (tolower((unsigned char)c) ==
163                                  tolower((unsigned char)*string))))
164                                 return (FNM_NOMATCH);
165                         ++string;
166                         break;
167                 }
168         /* NOTREACHED */
169 }
170
171 static int
172 #ifdef __STDC__
173 rangematch(const char *pattern, int test, int flags, char **newp)
174 #else
175 rangematch(pattern, test, flags, newp)
176         const char *pattern;
177         int test;
178         int flags;
179         char **newp;
180 #endif
181 {
182         int negate, ok, rv;
183         char c, c2;
184
185         /*
186          * A bracket expression starting with an unquoted circumflex
187          * character produces unspecified results (IEEE 1003.2-1992,
188          * 3.13.2).  This implementation treats it like '!', for
189          * consistency with the regular expression syntax.
190          * J.T. Conklin (conklin@ngai.kaleida.com)
191          */
192         if ((negate = (*pattern == '!' || *pattern == '^')))
193                 ++pattern;
194
195         if (ISSET(flags, FNM_CASEFOLD))
196                 test = tolower(test);
197
198         /*
199          * A right bracket shall lose its special meaning and represent
200          * itself in a bracket expression if it occurs first in the list.
201          * -- POSIX.2 2.8.3.2
202          */
203         ok = 0;
204         c = *pattern++;
205         do {
206                 if (c == '[' && *pattern == ':') {
207                         do {
208                                 rv = classmatch(pattern + 1, test,
209                                     (flags & FNM_CASEFOLD), &pattern);
210                                 if (rv == RANGE_MATCH)
211                                         ok = 1;
212                                 c = *pattern++;
213                         } while (rv != RANGE_ERROR && c == '[' && *pattern == ':');
214                         if (c == ']')
215                         break;
216                 }
217                 if (c == '\\' && !ISSET(flags, FNM_NOESCAPE))
218                         c = *pattern++;
219                 if (c == EOS)
220                         return (RANGE_ERROR);
221                 if (c == '/' && ISSET(flags, FNM_PATHNAME))
222                         return (RANGE_NOMATCH);
223                 if (ISSET(flags, FNM_CASEFOLD))
224                         c = tolower((unsigned char)c);
225                 if (*pattern == '-'
226                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
227                         pattern += 2;
228                         if (c2 == '\\' && !ISSET(flags, FNM_NOESCAPE))
229                                 c2 = *pattern++;
230                         if (c2 == EOS)
231                                 return (RANGE_ERROR);
232                         if (ISSET(flags, FNM_CASEFOLD))
233                                 c2 = tolower((unsigned char)c2);
234                         if (c <= test && test <= c2)
235                                 ok = 1;
236                 } else if (c == test)
237                         ok = 1;
238         } while ((c = *pattern++) != ']');
239
240         *newp = (char *)pattern;
241         return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
242 }
243
244 static int
245 #ifdef __STDC__
246 classmatch(const char *pattern, int test, int foldcase, const char **ep)
247 #else
248 classmatch(pattern, test, foldcase, ep)
249         const char *pattern;
250         int test;
251         int foldcase;
252         const char **ep;
253 #endif
254 {
255         struct cclass *cc;
256         const char *colon;
257         size_t len;
258         int rval = RANGE_NOMATCH;
259
260         if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
261                 *ep = pattern - 2;
262                 return(RANGE_ERROR);
263         }
264         *ep = colon + 2;
265         len = (size_t)(colon - pattern);
266
267         if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
268                 pattern = "lower:]";
269         for (cc = cclasses; cc->name != NULL; cc++) {
270                 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
271                         if (cc->isctype(test))
272                                 rval = RANGE_MATCH;
273                         break;
274                 }
275         }
276         if (cc->name == NULL) {
277                 /* invalid character class, return EOS */
278                 *ep = colon + strlen(colon);
279                 rval = RANGE_ERROR;
280         }
281         return(rval);
282 }