Debian Codegraph @

terps/alan3/fnmatch.c

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