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