Merge from vendor branch OPENSSH:
[dragonfly.git] / lib / libc / gen / 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  * @(#)fnmatch.c        8.2 (Berkeley) 4/16/94
37  * $DragonFly: src/lib/libc/gen/fnmatch.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
38  */
39
40 /*
41  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
42  * Compares a filename or pathname to a pattern.
43  */
44
45 #include <ctype.h>
46 #include <fnmatch.h>
47 #include <string.h>
48 #include <stdio.h>
49
50 #include "collate.h"
51
52 #define EOS     '\0'
53
54 #define RANGE_MATCH     1
55 #define RANGE_NOMATCH   0
56 #define RANGE_ERROR     (-1)
57
58 static int rangematch (const char *, char, int, char **);
59
60 int
61 fnmatch(const char *pattern, const char *string, int flags)
62 {
63         const char *stringstart;
64         char *newp;
65         char c, test;
66
67         for (stringstart = string;;)
68                 switch (c = *pattern++) {
69                 case EOS:
70                         if ((flags & FNM_LEADING_DIR) && *string == '/')
71                                 return (0);
72                         return (*string == EOS ? 0 : FNM_NOMATCH);
73                 case '?':
74                         if (*string == EOS)
75                                 return (FNM_NOMATCH);
76                         if (*string == '/' && (flags & FNM_PATHNAME))
77                                 return (FNM_NOMATCH);
78                         if (*string == '.' && (flags & FNM_PERIOD) &&
79                             (string == stringstart ||
80                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
81                                 return (FNM_NOMATCH);
82                         ++string;
83                         break;
84                 case '*':
85                         c = *pattern;
86                         /* Collapse multiple stars. */
87                         while (c == '*')
88                                 c = *++pattern;
89
90                         if (*string == '.' && (flags & FNM_PERIOD) &&
91                             (string == stringstart ||
92                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
93                                 return (FNM_NOMATCH);
94
95                         /* Optimize for pattern with * at end or before /. */
96                         if (c == EOS)
97                                 if (flags & FNM_PATHNAME)
98                                         return ((flags & FNM_LEADING_DIR) ||
99                                             strchr(string, '/') == NULL ?
100                                             0 : FNM_NOMATCH);
101                                 else
102                                         return (0);
103                         else if (c == '/' && flags & FNM_PATHNAME) {
104                                 if ((string = strchr(string, '/')) == NULL)
105                                         return (FNM_NOMATCH);
106                                 break;
107                         }
108
109                         /* General case, use recursion. */
110                         while ((test = *string) != EOS) {
111                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
112                                         return (0);
113                                 if (test == '/' && flags & FNM_PATHNAME)
114                                         break;
115                                 ++string;
116                         }
117                         return (FNM_NOMATCH);
118                 case '[':
119                         if (*string == EOS)
120                                 return (FNM_NOMATCH);
121                         if (*string == '/' && (flags & FNM_PATHNAME))
122                                 return (FNM_NOMATCH);
123                         if (*string == '.' && (flags & FNM_PERIOD) &&
124                             (string == stringstart ||
125                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
126                                 return (FNM_NOMATCH);
127
128                         switch (rangematch(pattern, *string, flags, &newp)) {
129                         case RANGE_ERROR:
130                                 goto norm;
131                         case RANGE_MATCH:
132                                 pattern = newp;
133                                 break;
134                         case RANGE_NOMATCH:
135                                 return (FNM_NOMATCH);
136                         }
137                         ++string;
138                         break;
139                 case '\\':
140                         if (!(flags & FNM_NOESCAPE)) {
141                                 if ((c = *pattern++) == EOS) {
142                                         c = '\\';
143                                         --pattern;
144                                 }
145                         }
146                         /* FALLTHROUGH */
147                 default:
148                 norm:
149                         if (c == *string)
150                                 ;
151                         else if ((flags & FNM_CASEFOLD) &&
152                                  (tolower((unsigned char)c) ==
153                                   tolower((unsigned char)*string)))
154                                 ;
155                         else
156                                 return (FNM_NOMATCH);
157                         string++;
158                         break;
159                 }
160         /* NOTREACHED */
161 }
162
163 static int
164 rangematch(const char *pattern, char test, int flags, char **newp)
165 {
166         int negate, ok;
167         char c, c2;
168
169         /*
170          * A bracket expression starting with an unquoted circumflex
171          * character produces unspecified results (IEEE 1003.2-1992,
172          * 3.13.2).  This implementation treats it like '!', for
173          * consistency with the regular expression syntax.
174          * J.T. Conklin (conklin@ngai.kaleida.com)
175          */
176         if ( (negate = (*pattern == '!' || *pattern == '^')) )
177                 ++pattern;
178
179         if (flags & FNM_CASEFOLD)
180                 test = tolower((unsigned char)test);
181
182         /*
183          * A right bracket shall lose its special meaning and represent
184          * itself in a bracket expression if it occurs first in the list.
185          * -- POSIX.2 2.8.3.2
186          */
187         ok = 0;
188         c = *pattern++;
189         do {
190                 if (c == '\\' && !(flags & FNM_NOESCAPE))
191                         c = *pattern++;
192                 if (c == EOS)
193                         return (RANGE_ERROR);
194
195                 if (c == '/' && (flags & FNM_PATHNAME))
196                         return (RANGE_NOMATCH);
197
198                 if (flags & FNM_CASEFOLD)
199                         c = tolower((unsigned char)c);
200
201                 if (*pattern == '-'
202                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
203                         pattern += 2;
204                         if (c2 == '\\' && !(flags & FNM_NOESCAPE))
205                                 c2 = *pattern++;
206                         if (c2 == EOS)
207                                 return (RANGE_ERROR);
208
209                         if (flags & FNM_CASEFOLD)
210                                 c2 = tolower((unsigned char)c2);
211
212                         if (__collate_load_error ?
213                             c <= test && test <= c2 :
214                                __collate_range_cmp(c, test) <= 0
215                             && __collate_range_cmp(test, c2) <= 0
216                            )
217                                 ok = 1;
218                 } else if (c == test)
219                         ok = 1;
220         } while ((c = *pattern++) != ']');
221
222         *newp = __DECONST(char *, pattern);
223         return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
224 }