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

1.17    ! mycroft     1: /*     $NetBSD: fnmatch.c,v 1.16 1999/09/20 04:38:58 lukem Exp $       */
1.11      cgd         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:
1.12      christos   39: #include <sys/cdefs.h>
1.1       cgd        40: #if defined(LIBC_SCCS) && !defined(lint)
1.11      cgd        41: #if 0
                     42: static char sccsid[] = "@(#)fnmatch.c  8.2 (Berkeley) 4/16/94";
                     43: #else
1.17    ! mycroft    44: __RCSID("$NetBSD: fnmatch.c,v 1.16 1999/09/20 04:38:58 lukem Exp $");
1.11      cgd        45: #endif
1.1       cgd        46: #endif /* LIBC_SCCS and not lint */
                     47:
                     48: /*
1.7       jtc        49:  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
1.1       cgd        50:  * Compares a filename or pathname to a pattern.
                     51:  */
                     52:
1.13      jtc        53: #include "namespace.h"
1.15      lukem      54:
                     55: #include <assert.h>
1.3       jtc        56: #include <fnmatch.h>
1.1       cgd        57: #include <string.h>
1.13      jtc        58:
                     59: #ifdef __weak_alias
1.17    ! mycroft    60: __weak_alias(fnmatch,_fnmatch)
1.13      jtc        61: #endif
1.1       cgd        62:
                     63: #define        EOS     '\0'
                     64:
1.7       jtc        65: static const char *rangematch __P((const char *, int, int));
1.1       cgd        66:
1.9       jtc        67: int
1.1       cgd        68: fnmatch(pattern, string, flags)
1.11      cgd        69:        const char *pattern, *string;
1.1       cgd        70:        int flags;
                     71: {
1.11      cgd        72:        const char *stringstart;
                     73:        char c, test;
1.1       cgd        74:
1.15      lukem      75:        _DIAGASSERT(pattern != NULL);
                     76:        _DIAGASSERT(string != NULL);
                     77:
1.11      cgd        78:        for (stringstart = string;;)
1.1       cgd        79:                switch (c = *pattern++) {
                     80:                case EOS:
1.3       jtc        81:                        return (*string == EOS ? 0 : FNM_NOMATCH);
1.1       cgd        82:                case '?':
1.8       jtc        83:                        if (*string == EOS)
1.3       jtc        84:                                return (FNM_NOMATCH);
1.8       jtc        85:                        if (*string == '/' && (flags & FNM_PATHNAME))
                     86:                                return (FNM_NOMATCH);
                     87:                        if (*string == '.' && (flags & FNM_PERIOD) &&
1.11      cgd        88:                            (string == stringstart ||
                     89:                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.8       jtc        90:                                return (FNM_NOMATCH);
                     91:                        ++string;
1.1       cgd        92:                        break;
                     93:                case '*':
                     94:                        c = *pattern;
1.3       jtc        95:                        /* Collapse multiple stars. */
1.1       cgd        96:                        while (c == '*')
                     97:                                c = *++pattern;
                     98:
1.8       jtc        99:                        if (*string == '.' && (flags & FNM_PERIOD) &&
1.11      cgd       100:                            (string == stringstart ||
                    101:                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
1.8       jtc       102:                                return (FNM_NOMATCH);
                    103:
1.3       jtc       104:                        /* Optimize for pattern with * at end or before /. */
1.14      christos  105:                        if (c == EOS) {
1.1       cgd       106:                                if (flags & FNM_PATHNAME)
1.10      jtc       107:                                        return (strchr(string, '/') == NULL ?
1.3       jtc       108:                                            0 : FNM_NOMATCH);
1.1       cgd       109:                                else
1.3       jtc       110:                                        return (0);
1.14      christos  111:                        } else if (c == '/' && flags & FNM_PATHNAME) {
1.10      jtc       112:                                if ((string = strchr(string, '/')) == NULL)
1.3       jtc       113:                                        return (FNM_NOMATCH);
1.1       cgd       114:                                break;
                    115:                        }
                    116:
1.3       jtc       117:                        /* General case, use recursion. */
1.1       cgd       118:                        while ((test = *string) != EOS) {
1.8       jtc       119:                                if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
1.3       jtc       120:                                        return (0);
1.1       cgd       121:                                if (test == '/' && flags & FNM_PATHNAME)
                    122:                                        break;
                    123:                                ++string;
                    124:                        }
1.3       jtc       125:                        return (FNM_NOMATCH);
1.1       cgd       126:                case '[':
1.8       jtc       127:                        if (*string == EOS)
                    128:                                return (FNM_NOMATCH);
                    129:                        if (*string == '/' && flags & FNM_PATHNAME)
1.3       jtc       130:                                return (FNM_NOMATCH);
1.11      cgd       131:                        if ((pattern =
                    132:                            rangematch(pattern, *string, flags)) == NULL)
1.3       jtc       133:                                return (FNM_NOMATCH);
1.8       jtc       134:                        ++string;
1.1       cgd       135:                        break;
                    136:                case '\\':
1.3       jtc       137:                        if (!(flags & FNM_NOESCAPE)) {
1.1       cgd       138:                                if ((c = *pattern++) == EOS) {
                    139:                                        c = '\\';
                    140:                                        --pattern;
                    141:                                }
                    142:                        }
                    143:                        /* FALLTHROUGH */
                    144:                default:
                    145:                        if (c != *string++)
1.3       jtc       146:                                return (FNM_NOMATCH);
1.1       cgd       147:                        break;
                    148:                }
1.3       jtc       149:        /* NOTREACHED */
                    150: }
                    151:
                    152: static const char *
1.7       jtc       153: rangematch(pattern, test, flags)
1.11      cgd       154:        const char *pattern;
                    155:        int test, flags;
1.3       jtc       156: {
                    157:        int negate, ok;
1.11      cgd       158:        char c, c2;
1.15      lukem     159:
                    160:        _DIAGASSERT(pattern != NULL);
1.3       jtc       161:
1.11      cgd       162:        /*
                    163:         * A bracket expression starting with an unquoted circumflex
1.7       jtc       164:         * character produces unspecified results (IEEE 1003.2-1992,
1.11      cgd       165:         * 3.13.2).  This implementation treats it like '!', for
                    166:         * consistency with the regular expression syntax.
                    167:         * J.T. Conklin (conklin@ngai.kaleida.com)
1.3       jtc       168:         */
1.12      christos  169:        if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
1.11      cgd       170:                ++pattern;
1.7       jtc       171:
1.3       jtc       172:        for (ok = 0; (c = *pattern++) != ']';) {
1.11      cgd       173:                if (c == '\\' && !(flags & FNM_NOESCAPE))
1.7       jtc       174:                        c = *pattern++;
1.11      cgd       175:                if (c == EOS)
1.7       jtc       176:                        return (NULL);
                    177:                if (*pattern == '-'
                    178:                    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
                    179:                        pattern += 2;
1.11      cgd       180:                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
1.7       jtc       181:                                c2 = *pattern++;
1.11      cgd       182:                        if (c2 == EOS)
1.7       jtc       183:                                return (NULL);
1.11      cgd       184:                        if (c <= test && test <= c2)
1.3       jtc       185:                                ok = 1;
1.11      cgd       186:                } else if (c == test)
1.7       jtc       187:                        ok = 1;
1.3       jtc       188:        }
                    189:        return (ok == negate ? NULL : pattern);
1.1       cgd       190: }

CVSweb <webmaster@jp.NetBSD.org>