match.c revision 1.2
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.2 1999/11/23 22:25:54 markus 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 25match_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; 31 32 /* Process '*'. */ 33 if (*pattern == '*') { 34 /* Skip the asterisk. */ 35 pattern++; 36 37 /* If at end of pattern, accept immediately. */ 38 if (!*pattern) 39 return 1; 40 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; 65 66 /* Check if the next character of the string is 67 acceptable. */ 68 if (*pattern != '?' && *pattern != *s) 69 return 0; 70 71 /* Move to the next character, both in string and in 72 pattern. */ 73 s++; 74 pattern++; 75 } 76 /* NOTREACHED */ 77} 78