]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/libkern/fnmatch.c
Mark more code #ifdef NODEVFS
[FreeBSD/FreeBSD.git] / sys / libkern / fnmatch.c
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Guido van Rossum.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38
39 /*
40  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
41  * Compares a filename or pathname to a pattern.
42  */
43
44 #include <sys/param.h>
45 #include <sys/ctype.h>
46 #include <sys/libkern.h>
47
48 #define EOS     '\0'
49
50 #define RANGE_MATCH     1
51 #define RANGE_NOMATCH   0
52 #define RANGE_ERROR     (-1)
53
54 static int rangematch(const char *, char, int, char **);
55
56 int
57 fnmatch(pattern, string, flags)
58         const char *pattern, *string;
59         int flags;
60 {
61         const char *stringstart;
62         char *newp;
63         char c, test;
64
65         for (stringstart = string;;)
66                 switch (c = *pattern++) {
67                 case EOS:
68                         if ((flags & FNM_LEADING_DIR) && *string == '/')
69                                 return (0);
70                         return (*string == EOS ? 0 : FNM_NOMATCH);
71                 case '?':
72                         if (*string == EOS)
73                                 return (FNM_NOMATCH);
74                         if (*string == '/' && (flags & FNM_PATHNAME))
75                                 return (FNM_NOMATCH);
76                         if (*string == '.' && (flags & FNM_PERIOD) &&
77                             (string == stringstart ||
78                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
79                                 return (FNM_NOMATCH);
80                         ++string;
81                         break;
82                 case '*':
83                         c = *pattern;
84                         /* Collapse multiple stars. */
85                         while (c == '*')
86                                 c = *++pattern;
87
88                         if (*string == '.' && (flags & FNM_PERIOD) &&
89                             (string == stringstart ||
90                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
91                                 return (FNM_NOMATCH);
92
93                         /* Optimize for pattern with * at end or before /. */
94                         if (c == EOS)
95                                 if (flags & FNM_PATHNAME)
96                                         return ((flags & FNM_LEADING_DIR) ||
97                                             index(string, '/') == NULL ?
98                                             0 : FNM_NOMATCH);
99                                 else
100                                         return (0);
101                         else if (c == '/' && flags & FNM_PATHNAME) {
102                                 if ((string = index(string, '/')) == NULL)
103                                         return (FNM_NOMATCH);
104                                 break;
105                         }
106
107                         /* General case, use recursion. */
108                         while ((test = *string) != EOS) {
109                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
110                                         return (0);
111                                 if (test == '/' && flags & FNM_PATHNAME)
112                                         break;
113                                 ++string;
114                         }
115                         return (FNM_NOMATCH);
116                 case '[':
117                         if (*string == EOS)
118                                 return (FNM_NOMATCH);
119                         if (*string == '/' && (flags & FNM_PATHNAME))
120                                 return (FNM_NOMATCH);
121                         if (*string == '.' && (flags & FNM_PERIOD) &&
122                             (string == stringstart ||
123                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
124                                 return (FNM_NOMATCH);
125
126                         switch (rangematch(pattern, *string, flags, &newp)) {
127                         case RANGE_ERROR:
128                                 goto norm;
129                         case RANGE_MATCH:
130                                 pattern = newp;
131                                 break;
132                         case RANGE_NOMATCH:
133                                 return (FNM_NOMATCH);
134                         }
135                         ++string;
136                         break;
137                 case '\\':
138                         if (!(flags & FNM_NOESCAPE)) {
139                                 if ((c = *pattern++) == EOS) {
140                                         c = '\\';
141                                         --pattern;
142                                 }
143                         }
144                         /* FALLTHROUGH */
145                 default:
146                 norm:
147                         if (c == *string)
148                                 ;
149                         else if ((flags & FNM_CASEFOLD) &&
150                                  (tolower((unsigned char)c) ==
151                                   tolower((unsigned char)*string)))
152                                 ;
153                         else
154                                 return (FNM_NOMATCH);
155                         string++;
156                         break;
157                 }
158         /* NOTREACHED */
159 }
160
161 static int
162 rangematch(pattern, test, flags, newp)
163         const char *pattern;
164         char test;
165         int flags;
166         char **newp;
167 {
168         int negate, ok;
169         char c, c2;
170
171         /*
172          * A bracket expression starting with an unquoted circumflex
173          * character produces unspecified results (IEEE 1003.2-1992,
174          * 3.13.2).  This implementation treats it like '!', for
175          * consistency with the regular expression syntax.
176          * J.T. Conklin (conklin@ngai.kaleida.com)
177          */
178         if ( (negate = (*pattern == '!' || *pattern == '^')) )
179                 ++pattern;
180
181         if (flags & FNM_CASEFOLD)
182                 test = tolower((unsigned char)test);
183
184         /*
185          * A right bracket shall lose its special meaning and represent
186          * itself in a bracket expression if it occurs first in the list.
187          * -- POSIX.2 2.8.3.2
188          */
189         ok = 0;
190         c = *pattern++;
191         do {
192                 if (c == '\\' && !(flags & FNM_NOESCAPE))
193                         c = *pattern++;
194                 if (c == EOS)
195                         return (RANGE_ERROR);
196
197                 if (c == '/' && (flags & FNM_PATHNAME))
198                         return (RANGE_NOMATCH);
199
200                 if (flags & FNM_CASEFOLD)
201                         c = tolower((unsigned char)c);
202
203                 if (*pattern == '-'
204                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
205                         pattern += 2;
206                         if (c2 == '\\' && !(flags & FNM_NOESCAPE))
207                                 c2 = *pattern++;
208                         if (c2 == EOS)
209                                 return (RANGE_ERROR);
210
211                         if (flags & FNM_CASEFOLD)
212                                 c2 = tolower((unsigned char)c2);
213
214                         if (c <= test && test <= c2)
215                                 ok = 1;
216                 } else if (c == test)
217                         ok = 1;
218         } while ((c = *pattern++) != ']');
219
220         *newp = (char *)(uintptr_t)pattern;
221         return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
222 }