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

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

CVSweb <webmaster@jp.NetBSD.org>