[BACK]Return to fnmatch.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/fnmatch.c, Revision 1.11

1.11    ! cgd         1: /*     $NetBSD$        */
        !             2:
1.1       cgd         3: /*
1.11    ! cgd         4:  * Copyright (c) 1989, 1993, 1994
1.6       cgd         5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Guido van Rossum.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #if defined(LIBC_SCCS) && !defined(lint)
1.11    ! cgd        40: #if 0
        !            41: static char sccsid[] = "@(#)fnmatch.c  8.2 (Berkeley) 4/16/94";
        !            42: #else
        !            43: static char rcsid[] = "$NetBSD$";
        !            44: #endif
1.1       cgd        45: #endif /* LIBC_SCCS and not lint */
                     46:
                     47: /*
1.7       jtc        48:  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
1.1       cgd        49:  * Compares a filename or pathname to a pattern.
                     50:  */
                     51:
1.3       jtc        52: #include <fnmatch.h>
1.1       cgd        53: #include <string.h>
                     54:
                     55: #define        EOS     '\0'
                     56:
1.7       jtc        57: static const char *rangematch __P((const char *, int, int));
1.1       cgd        58:
1.9       jtc        59: int
1.1       cgd        60: fnmatch(pattern, string, flags)
1.11    ! cgd        61:        const char *pattern, *string;
1.1       cgd        62:        int flags;
                     63: {
1.11    ! cgd        64:        const char *stringstart;
        !            65:        char c, test;
1.1       cgd        66:
1.11    ! cgd        67:        for (stringstart = string;;)
1.1       cgd        68:                switch (c = *pattern++) {
                     69:                case EOS:
1.3       jtc        70:                        return (*string == EOS ? 0 : FNM_NOMATCH);
1.1       cgd        71:                case '?':
1.8       jtc        72:                        if (*string == EOS)
1.3       jtc        73:                                return (FNM_NOMATCH);
1.8       jtc        74:                        if (*string == '/' && (flags & FNM_PATHNAME))
                     75:                                return (FNM_NOMATCH);
                     76:                        if (*string == '.' && (flags & FNM_PERIOD) &&
1.11    ! cgd        77:                            (string == stringstart ||
        !            78:                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.8       jtc        79:                                return (FNM_NOMATCH);
                     80:                        ++string;
1.1       cgd        81:                        break;
                     82:                case '*':
                     83:                        c = *pattern;
1.3       jtc        84:                        /* Collapse multiple stars. */
1.1       cgd        85:                        while (c == '*')
                     86:                                c = *++pattern;
                     87:
1.8       jtc        88:                        if (*string == '.' && (flags & FNM_PERIOD) &&
1.11    ! cgd        89:                            (string == stringstart ||
        !            90:                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.8       jtc        91:                                return (FNM_NOMATCH);
                     92:
1.3       jtc        93:                        /* Optimize for pattern with * at end or before /. */
1.1       cgd        94:                        if (c == EOS)
                     95:                                if (flags & FNM_PATHNAME)
1.10      jtc        96:                                        return (strchr(string, '/') == NULL ?
1.3       jtc        97:                                            0 : FNM_NOMATCH);
1.1       cgd        98:                                else
1.3       jtc        99:                                        return (0);
                    100:                        else if (c == '/' && flags & FNM_PATHNAME) {
1.10      jtc       101:                                if ((string = strchr(string, '/')) == NULL)
1.3       jtc       102:                                        return (FNM_NOMATCH);
1.1       cgd       103:                                break;
                    104:                        }
                    105:
1.3       jtc       106:                        /* General case, use recursion. */
1.1       cgd       107:                        while ((test = *string) != EOS) {
1.8       jtc       108:                                if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
1.3       jtc       109:                                        return (0);
1.1       cgd       110:                                if (test == '/' && flags & FNM_PATHNAME)
                    111:                                        break;
                    112:                                ++string;
                    113:                        }
1.3       jtc       114:                        return (FNM_NOMATCH);
1.1       cgd       115:                case '[':
1.8       jtc       116:                        if (*string == EOS)
                    117:                                return (FNM_NOMATCH);
                    118:                        if (*string == '/' && flags & FNM_PATHNAME)
1.3       jtc       119:                                return (FNM_NOMATCH);
1.11    ! cgd       120:                        if ((pattern =
        !           121:                            rangematch(pattern, *string, flags)) == NULL)
1.3       jtc       122:                                return (FNM_NOMATCH);
1.8       jtc       123:                        ++string;
1.1       cgd       124:                        break;
                    125:                case '\\':
1.3       jtc       126:                        if (!(flags & FNM_NOESCAPE)) {
1.1       cgd       127:                                if ((c = *pattern++) == EOS) {
                    128:                                        c = '\\';
                    129:                                        --pattern;
                    130:                                }
                    131:                        }
                    132:                        /* FALLTHROUGH */
                    133:                default:
                    134:                        if (c != *string++)
1.3       jtc       135:                                return (FNM_NOMATCH);
1.1       cgd       136:                        break;
                    137:                }
1.3       jtc       138:        /* NOTREACHED */
                    139: }
                    140:
                    141: static const char *
1.7       jtc       142: rangematch(pattern, test, flags)
1.11    ! cgd       143:        const char *pattern;
        !           144:        int test, flags;
1.3       jtc       145: {
                    146:        int negate, ok;
1.11    ! cgd       147:        char c, c2;
1.3       jtc       148:
1.11    ! cgd       149:        /*
        !           150:         * A bracket expression starting with an unquoted circumflex
1.7       jtc       151:         * character produces unspecified results (IEEE 1003.2-1992,
1.11    ! cgd       152:         * 3.13.2).  This implementation treats it like '!', for
        !           153:         * consistency with the regular expression syntax.
        !           154:         * J.T. Conklin (conklin@ngai.kaleida.com)
1.3       jtc       155:         */
1.11    ! cgd       156:        if (negate = (*pattern == '!' || *pattern == '^'))
        !           157:                ++pattern;
1.7       jtc       158:
1.3       jtc       159:        for (ok = 0; (c = *pattern++) != ']';) {
1.11    ! cgd       160:                if (c == '\\' && !(flags & FNM_NOESCAPE))
1.7       jtc       161:                        c = *pattern++;
1.11    ! cgd       162:                if (c == EOS)
1.7       jtc       163:                        return (NULL);
                    164:                if (*pattern == '-'
                    165:                    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
                    166:                        pattern += 2;
1.11    ! cgd       167:                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
1.7       jtc       168:                                c2 = *pattern++;
1.11    ! cgd       169:                        if (c2 == EOS)
1.7       jtc       170:                                return (NULL);
1.11    ! cgd       171:                        if (c <= test && test <= c2)
1.3       jtc       172:                                ok = 1;
1.11    ! cgd       173:                } else if (c == test)
1.7       jtc       174:                        ok = 1;
1.3       jtc       175:        }
                    176:        return (ok == negate ? NULL : pattern);
1.1       cgd       177: }

CVSweb <webmaster@jp.NetBSD.org>