OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [posix/] [fnmatch.c] - Diff between revs 207 and 345

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*
/*
 * Copyright (c) 1989, 1993, 1994
 * Copyright (c) 1989, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * This code is derived from software contributed to Berkeley by
 * This code is derived from software contributed to Berkeley by
 * Guido van Rossum.
 * Guido van Rossum.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#ifndef _NO_FNMATCH
#ifndef _NO_FNMATCH
 
 
#if defined(LIBC_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)fnmatch.c   8.2 (Berkeley) 4/16/94";
static char sccsid[] = "@(#)fnmatch.c   8.2 (Berkeley) 4/16/94";
#endif /* LIBC_SCCS and not lint */
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
#include <sys/cdefs.h>
 
 
/*
/*
 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
 * Compares a filename or pathname to a pattern.
 * Compares a filename or pathname to a pattern.
 */
 */
 
 
#include <ctype.h>
#include <ctype.h>
#include <fnmatch.h>
#include <fnmatch.h>
#include <string.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
 
 
#include "collate.h"
#include "collate.h"
 
 
#define EOS     '\0'
#define EOS     '\0'
 
 
#define RANGE_MATCH     1
#define RANGE_MATCH     1
#define RANGE_NOMATCH   0
#define RANGE_NOMATCH   0
#define RANGE_ERROR     (-1)
#define RANGE_ERROR     (-1)
 
 
static int rangematch(const char *, char, int, char **);
static int rangematch(const char *, char, int, char **);
 
 
int
int
fnmatch(pattern, string, flags)
fnmatch(pattern, string, flags)
        const char *pattern, *string;
        const char *pattern, *string;
        int flags;
        int flags;
{
{
        const char *stringstart;
        const char *stringstart;
        char *newp;
        char *newp;
        char c, test;
        char c, test;
 
 
        for (stringstart = string;;)
        for (stringstart = string;;)
                switch (c = *pattern++) {
                switch (c = *pattern++) {
                case EOS:
                case EOS:
                        if ((flags & FNM_LEADING_DIR) && *string == '/')
                        if ((flags & FNM_LEADING_DIR) && *string == '/')
                                return (0);
                                return (0);
                        return (*string == EOS ? 0 : FNM_NOMATCH);
                        return (*string == EOS ? 0 : FNM_NOMATCH);
                case '?':
                case '?':
                        if (*string == EOS)
                        if (*string == EOS)
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        if (*string == '/' && (flags & FNM_PATHNAME))
                        if (*string == '/' && (flags & FNM_PATHNAME))
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                            (string == stringstart ||
                            (string == stringstart ||
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        ++string;
                        ++string;
                        break;
                        break;
                case '*':
                case '*':
                        c = *pattern;
                        c = *pattern;
                        /* Collapse multiple stars. */
                        /* Collapse multiple stars. */
                        while (c == '*')
                        while (c == '*')
                                c = *++pattern;
                                c = *++pattern;
 
 
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                            (string == stringstart ||
                            (string == stringstart ||
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
 
 
                        /* Optimize for pattern with * at end or before /. */
                        /* Optimize for pattern with * at end or before /. */
                        if (c == EOS)
                        if (c == EOS)
                                if (flags & FNM_PATHNAME)
                                if (flags & FNM_PATHNAME)
                                        return ((flags & FNM_LEADING_DIR) ||
                                        return ((flags & FNM_LEADING_DIR) ||
                                            strchr(string, '/') == NULL ?
                                            strchr(string, '/') == NULL ?
                                            0 : FNM_NOMATCH);
                                            0 : FNM_NOMATCH);
                                else
                                else
                                        return (0);
                                        return (0);
                        else if (c == '/' && flags & FNM_PATHNAME) {
                        else if (c == '/' && flags & FNM_PATHNAME) {
                                if ((string = strchr(string, '/')) == NULL)
                                if ((string = strchr(string, '/')) == NULL)
                                        return (FNM_NOMATCH);
                                        return (FNM_NOMATCH);
                                break;
                                break;
                        }
                        }
 
 
                        /* General case, use recursion. */
                        /* General case, use recursion. */
                        while ((test = *string) != EOS) {
                        while ((test = *string) != EOS) {
                                if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
                                if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
                                        return (0);
                                        return (0);
                                if (test == '/' && flags & FNM_PATHNAME)
                                if (test == '/' && flags & FNM_PATHNAME)
                                        break;
                                        break;
                                ++string;
                                ++string;
                        }
                        }
                        return (FNM_NOMATCH);
                        return (FNM_NOMATCH);
                case '[':
                case '[':
                        if (*string == EOS)
                        if (*string == EOS)
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        if (*string == '/' && (flags & FNM_PATHNAME))
                        if (*string == '/' && (flags & FNM_PATHNAME))
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                        if (*string == '.' && (flags & FNM_PERIOD) &&
                            (string == stringstart ||
                            (string == stringstart ||
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
 
 
                        switch (rangematch(pattern, *string, flags, &newp)) {
                        switch (rangematch(pattern, *string, flags, &newp)) {
                        case RANGE_ERROR:
                        case RANGE_ERROR:
                                goto norm;
                                goto norm;
                        case RANGE_MATCH:
                        case RANGE_MATCH:
                                pattern = newp;
                                pattern = newp;
                                break;
                                break;
                        case RANGE_NOMATCH:
                        case RANGE_NOMATCH:
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        }
                        }
                        ++string;
                        ++string;
                        break;
                        break;
                case '\\':
                case '\\':
                        if (!(flags & FNM_NOESCAPE)) {
                        if (!(flags & FNM_NOESCAPE)) {
                                if ((c = *pattern++) == EOS) {
                                if ((c = *pattern++) == EOS) {
                                        c = '\\';
                                        c = '\\';
                                        --pattern;
                                        --pattern;
                                }
                                }
                        }
                        }
                        /* FALLTHROUGH */
                        /* FALLTHROUGH */
                default:
                default:
                norm:
                norm:
                        if (c == *string)
                        if (c == *string)
                                ;
                                ;
                        else if ((flags & FNM_CASEFOLD) &&
                        else if ((flags & FNM_CASEFOLD) &&
                                 (tolower((unsigned char)c) ==
                                 (tolower((unsigned char)c) ==
                                  tolower((unsigned char)*string)))
                                  tolower((unsigned char)*string)))
                                ;
                                ;
                        else
                        else
                                return (FNM_NOMATCH);
                                return (FNM_NOMATCH);
                        string++;
                        string++;
                        break;
                        break;
                }
                }
        /* NOTREACHED */
        /* NOTREACHED */
}
}
 
 
static int
static int
rangematch(pattern, test, flags, newp)
rangematch(pattern, test, flags, newp)
        const char *pattern;
        const char *pattern;
        char test;
        char test;
        int flags;
        int flags;
        char **newp;
        char **newp;
{
{
        int negate, ok;
        int negate, ok;
        char c, c2;
        char c, c2;
 
 
        /*
        /*
         * A bracket expression starting with an unquoted circumflex
         * A bracket expression starting with an unquoted circumflex
         * character produces unspecified results (IEEE 1003.2-1992,
         * character produces unspecified results (IEEE 1003.2-1992,
         * 3.13.2).  This implementation treats it like '!', for
         * 3.13.2).  This implementation treats it like '!', for
         * consistency with the regular expression syntax.
         * consistency with the regular expression syntax.
         * J.T. Conklin (conklin@ngai.kaleida.com)
         * J.T. Conklin (conklin@ngai.kaleida.com)
         */
         */
        if ( (negate = (*pattern == '!' || *pattern == '^')) )
        if ( (negate = (*pattern == '!' || *pattern == '^')) )
                ++pattern;
                ++pattern;
 
 
        if (flags & FNM_CASEFOLD)
        if (flags & FNM_CASEFOLD)
                test = tolower((unsigned char)test);
                test = tolower((unsigned char)test);
 
 
        /*
        /*
         * A right bracket shall lose its special meaning and represent
         * A right bracket shall lose its special meaning and represent
         * itself in a bracket expression if it occurs first in the list.
         * itself in a bracket expression if it occurs first in the list.
         * -- POSIX.2 2.8.3.2
         * -- POSIX.2 2.8.3.2
         */
         */
        ok = 0;
        ok = 0;
        c = *pattern++;
        c = *pattern++;
        do {
        do {
                if (c == '\\' && !(flags & FNM_NOESCAPE))
                if (c == '\\' && !(flags & FNM_NOESCAPE))
                        c = *pattern++;
                        c = *pattern++;
                if (c == EOS)
                if (c == EOS)
                        return (RANGE_ERROR);
                        return (RANGE_ERROR);
 
 
                if (c == '/' && (flags & FNM_PATHNAME))
                if (c == '/' && (flags & FNM_PATHNAME))
                        return (RANGE_NOMATCH);
                        return (RANGE_NOMATCH);
 
 
                if (flags & FNM_CASEFOLD)
                if (flags & FNM_CASEFOLD)
                        c = tolower((unsigned char)c);
                        c = tolower((unsigned char)c);
 
 
                if (*pattern == '-'
                if (*pattern == '-'
                    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
                    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
                        pattern += 2;
                        pattern += 2;
                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
                                c2 = *pattern++;
                                c2 = *pattern++;
                        if (c2 == EOS)
                        if (c2 == EOS)
                                return (RANGE_ERROR);
                                return (RANGE_ERROR);
 
 
                        if (flags & FNM_CASEFOLD)
                        if (flags & FNM_CASEFOLD)
                                c2 = tolower((unsigned char)c2);
                                c2 = tolower((unsigned char)c2);
 
 
                        if (__collate_load_error ?
                        if (__collate_load_error ?
                            c <= test && test <= c2 :
                            c <= test && test <= c2 :
                               __collate_range_cmp(c, test) <= 0
                               __collate_range_cmp(c, test) <= 0
                            && __collate_range_cmp(test, c2) <= 0
                            && __collate_range_cmp(test, c2) <= 0
                           )
                           )
                                ok = 1;
                                ok = 1;
                } else if (c == test)
                } else if (c == test)
                        ok = 1;
                        ok = 1;
        } while ((c = *pattern++) != ']');
        } while ((c = *pattern++) != ']');
 
 
        *newp = (char *)pattern;
        *newp = (char *)pattern;
        return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
        return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
}
}
 
 
#endif /* !_NO_FNMATCH  */
#endif /* !_NO_FNMATCH  */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.