[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.4

1.1       deraadt     1: /*
1.3       deraadt     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:  */
1.1       deraadt    15:
                     16: #include "includes.h"
1.4     ! markus     17: RCSID("$Id: match.c,v 1.3 1999/11/24 00:26:02 deraadt Exp $");
1.1       deraadt    18:
                     19: #include "ssh.h"
                     20:
1.4     ! markus     21: /*
        !            22:  * Returns true if the given string matches the pattern (which may contain ?
        !            23:  * and * as wildcards), and zero if it does not match.
        !            24:  */
1.2       markus     25:
                     26: int
                     27: match_pattern(const char *s, const char *pattern)
1.1       deraadt    28: {
1.2       markus     29:        for (;;) {
                     30:                /* If at end of pattern, accept if also at end of string. */
                     31:                if (!*pattern)
                     32:                        return !*s;
                     33:
                     34:                if (*pattern == '*') {
                     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 != '*') {
1.4     ! markus     44:                                /*
        !            45:                                 * Look instances of the next character in
        !            46:                                 * pattern, and try to match starting from
        !            47:                                 * those.
        !            48:                                 */
1.2       markus     49:                                for (; *s; s++)
                     50:                                        if (*s == *pattern &&
                     51:                                            match_pattern(s + 1, pattern + 1))
                     52:                                                return 1;
                     53:                                /* Failed. */
                     54:                                return 0;
                     55:                        }
1.4     ! markus     56:                        /*
        !            57:                         * Move ahead one character at a time and try to
        !            58:                         * match at each position.
        !            59:                         */
1.2       markus     60:                        for (; *s; s++)
                     61:                                if (match_pattern(s, pattern))
                     62:                                        return 1;
                     63:                        /* Failed. */
                     64:                        return 0;
                     65:                }
1.4     ! markus     66:                /*
        !            67:                 * There must be at least one more character in the string.
        !            68:                 * If we are at the end, fail.
        !            69:                 */
1.2       markus     70:                if (!*s)
                     71:                        return 0;
                     72:
1.4     ! markus     73:                /* Check if the next character of the string is acceptable. */
1.2       markus     74:                if (*pattern != '?' && *pattern != *s)
                     75:                        return 0;
                     76:
1.4     ! markus     77:                /* Move to the next character, both in string and in pattern. */
1.2       markus     78:                s++;
                     79:                pattern++;
                     80:        }
                     81:        /* NOTREACHED */
1.1       deraadt    82: }