[BACK]Return to match.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/match.c, Revision 1.2

1.1       deraadt     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"
1.2     ! markus     17: RCSID("$Id: match.c,v 1.1 1999/09/26 20:53:36 deraadt Exp $");
1.1       deraadt    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. */
1.2     ! markus     23:
        !            24: int
        !            25: match_pattern(const char *s, const char *pattern)
1.1       deraadt    26: {
1.2     ! markus     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 */
1.1       deraadt    77: }