Lines 207-238
cam_strvis_sbuf(struct sbuf *sb, const u
Link Here
|
207 |
/* |
207 |
/* |
208 |
* Compare string with pattern, returning 0 on match. |
208 |
* Compare string with pattern, returning 0 on match. |
209 |
* Short pattern matches trailing blanks in name, |
209 |
* Short pattern matches trailing blanks in name, |
210 |
* wildcard '*' in pattern matches rest of name, |
210 |
* Shell globbing rules apply: * matches 0 or more characters, |
211 |
* wildcard '?' matches a single non-space character. |
211 |
* ? matchces one character, [...] denotes a set to match one char, |
|
|
212 |
* [^...] denotes a complimented set to match one character. |
213 |
* Spaces in str used to match anything in the pattern string |
214 |
* but was removed because it's a bug. No current patterns require |
215 |
* it, as far as I know, but it's impossible to know what drives |
216 |
* returned. |
217 |
* |
218 |
* Each '*' generates recursion, so keep the number of * in check. |
212 |
*/ |
219 |
*/ |
213 |
int |
220 |
int |
214 |
cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) |
221 |
cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) |
215 |
{ |
222 |
{ |
216 |
|
223 |
|
217 |
while (*pattern != '\0'&& str_len > 0) { |
224 |
while (*pattern != '\0' && str_len > 0) { |
|
|
225 |
if (*pattern == '*') { |
226 |
pattern++; |
227 |
if (*pattern == '\0') |
228 |
return (0); |
229 |
do { |
230 |
if (cam_strmatch(str, pattern, str_len) == 0) |
231 |
return (0); |
232 |
str++; |
233 |
str_len--; |
234 |
} while (str_len > 0); |
235 |
return (1); |
236 |
} else if (*pattern == '[') { |
237 |
int negate_range, ok; |
238 |
uint8_t pc, sc; |
218 |
|
239 |
|
219 |
if (*pattern == '*') { |
240 |
ok = 0; |
220 |
return (0); |
241 |
sc = *str++; |
|
|
242 |
str_len--; |
243 |
if ((negate_range = (*pattern == '^')) != 0) |
244 |
pattern++; |
245 |
while (((pc = *pattern) != ']') && *pattern != '\0') { |
246 |
pattern++; |
247 |
if (*pattern == '-') { |
248 |
if (pattern[1] == '\0') /* Bad pattern */ |
249 |
return (1); |
250 |
if (sc >= pc && sc <= pattern[1]) |
251 |
ok = 1; |
252 |
pattern += 2; |
253 |
} else if (pc == sc) |
254 |
ok = 1; |
255 |
} |
256 |
if (ok == negate_range) |
257 |
return (1); |
258 |
} else if (*pattern == '?') { |
259 |
/* NB: || *str == ' ' of the old code is a bug and was removed */ |
260 |
/* if you add it back, keep this the last if before the naked else */ |
261 |
pattern++; |
262 |
str++; |
263 |
str_len--; |
264 |
} else { |
265 |
if (*str != *pattern) |
266 |
return (1); |
267 |
pattern++; |
268 |
str++; |
269 |
str_len--; |
221 |
} |
270 |
} |
222 |
if ((*pattern != *str) |
|
|
223 |
&& (*pattern != '?' || *str == ' ')) { |
224 |
return (1); |
225 |
} |
226 |
pattern++; |
227 |
str++; |
228 |
str_len--; |
229 |
} |
271 |
} |
230 |
while (str_len > 0 && *str == ' ') { |
272 |
while (str_len > 0 && *str == ' ') { |
231 |
str++; |
273 |
str++; |
232 |
str_len--; |
274 |
str_len--; |
233 |
} |
275 |
} |
234 |
if (str_len > 0 && *str == 0) |
|
|
235 |
str_len = 0; |
236 |
|
276 |
|
237 |
return (str_len); |
277 |
return (str_len); |
238 |
} |
278 |
} |