]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
2 | ||
3 | match.c | |
4 | ||
5 | Author: Tatu Ylonen <ylo@cs.hut.fi> | |
6 | ||
7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
8 | All rights reserved | |
9 | ||
10 | Created: Thu Jun 22 01:17:50 1995 ylo | |
11 | ||
12 | Simple pattern matching, with '*' and '?' as wildcards. | |
13 | ||
14 | */ | |
15 | ||
16 | #include "includes.h" | |
17 | RCSID("$Id$"); | |
18 | ||
19 | #include "ssh.h" | |
20 | ||
21 | /* Returns true if the given string matches the pattern (which may contain | |
22 | ? and * as wildcards), and zero if it does not match. */ | |
23 | ||
24 | int match_pattern(const char *s, const char *pattern) | |
25 | { | |
26 | while (1) | |
27 | { | |
28 | /* If at end of pattern, accept if also at end of string. */ | |
29 | if (!*pattern) | |
30 | return !*s; | |
31 | ||
32 | /* Process '*'. */ | |
33 | if (*pattern == '*') | |
34 | { | |
35 | /* Skip the asterisk. */ | |
36 | pattern++; | |
37 | ||
38 | /* If at end of pattern, accept immediately. */ | |
39 | if (!*pattern) | |
40 | return 1; | |
41 | ||
42 | /* If next character in pattern is known, optimize. */ | |
43 | if (*pattern != '?' && *pattern != '*') | |
44 | { | |
45 | /* Look instances of the next character in pattern, and try | |
46 | to match starting from those. */ | |
47 | for (; *s; s++) | |
48 | if (*s == *pattern && | |
49 | match_pattern(s + 1, pattern + 1)) | |
50 | return 1; | |
51 | /* Failed. */ | |
52 | return 0; | |
53 | } | |
54 | ||
55 | /* Move ahead one character at a time and try to match at each | |
56 | position. */ | |
57 | for (; *s; s++) | |
58 | if (match_pattern(s, pattern)) | |
59 | return 1; | |
60 | /* Failed. */ | |
61 | return 0; | |
62 | } | |
63 | ||
64 | /* There must be at least one more character in the string. If we are | |
65 | at the end, fail. */ | |
66 | if (!*s) | |
67 | return 0; | |
68 | ||
69 | /* Check if the next character of the string is acceptable. */ | |
70 | if (*pattern != '?' && *pattern != *s) | |
71 | return 0; | |
72 | ||
73 | /* Move to the next character, both in string and in pattern. */ | |
74 | s++; | |
75 | pattern++; | |
76 | } | |
77 | /*NOTREACHED*/ | |
78 | } |