]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 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 | */ | |
8efc0c15 | 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. */ | |
8efc0c15 | 23 | |
5260325f | 24 | int |
25 | match_pattern(const char *s, const char *pattern) | |
26 | { | |
27 | for (;;) { | |
28 | /* If at end of pattern, accept if also at end of string. */ | |
29 | if (!*pattern) | |
30 | return !*s; | |
8efc0c15 | 31 | |
5260325f | 32 | /* Process '*'. */ |
33 | if (*pattern == '*') { | |
34 | /* Skip the asterisk. */ | |
35 | pattern++; | |
8efc0c15 | 36 | |
5260325f | 37 | /* If at end of pattern, accept immediately. */ |
38 | if (!*pattern) | |
39 | return 1; | |
8efc0c15 | 40 | |
5260325f | 41 | /* If next character in pattern is known, optimize. */ |
42 | if (*pattern != '?' && *pattern != '*') { | |
43 | /* Look instances of the next character in | |
44 | pattern, and try to match starting from | |
45 | those. */ | |
46 | for (; *s; s++) | |
47 | if (*s == *pattern && | |
48 | match_pattern(s + 1, pattern + 1)) | |
49 | return 1; | |
50 | /* Failed. */ | |
51 | return 0; | |
52 | } | |
53 | /* Move ahead one character at a time and try to | |
54 | match at each position. */ | |
55 | for (; *s; s++) | |
56 | if (match_pattern(s, pattern)) | |
57 | return 1; | |
58 | /* Failed. */ | |
59 | return 0; | |
60 | } | |
61 | /* There must be at least one more character in the | |
62 | string. If we are at the end, fail. */ | |
63 | if (!*s) | |
64 | return 0; | |
8efc0c15 | 65 | |
5260325f | 66 | /* Check if the next character of the string is |
67 | acceptable. */ | |
68 | if (*pattern != '?' && *pattern != *s) | |
69 | return 0; | |
8efc0c15 | 70 | |
5260325f | 71 | /* Move to the next character, both in string and in |
72 | pattern. */ | |
73 | s++; | |
74 | pattern++; | |
75 | } | |
76 | /* NOTREACHED */ | |
8efc0c15 | 77 | } |