summaryrefslogtreecommitdiff
path: root/match.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
committerDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
commitd4a8b7e34dd619a4debf9a206c81db26d1402ea6 (patch)
treea47d770a2f790f40d18b0982d4e55fa7cfb1fa3b /match.c
Initial revision
Diffstat (limited to 'match.c')
-rw-r--r--match.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/match.c b/match.c
new file mode 100644
index 000000000..b7a7d3389
--- /dev/null
+++ b/match.c
@@ -0,0 +1,78 @@
1/*
2
3match.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved
9
10Created: Thu Jun 22 01:17:50 1995 ylo
11
12Simple pattern matching, with '*' and '?' as wildcards.
13
14*/
15
16#include "includes.h"
17RCSID("$Id: match.c,v 1.1 1999/10/27 03:42:44 damien Exp $");
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
24int 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}