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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [src/] [fnmatch.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*      $NetBSD: fnmatch.c,v 1.21 2005/12/24 21:11:16 perry Exp $       */
2
 
3
/*
4
 * Copyright (c) 1989, 1993, 1994
5
 *      The Regents of the University of California.  All rights reserved.
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. Neither the name of the University nor the names of its contributors
19
 *    may be used to endorse or promote products derived from this software
20
 *    without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * SUCH DAMAGE.
33
 */
34
 
35
/*
36
 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
37
 * Compares a filename or pathname to a pattern.
38
 */
39
 
40
#include <cyg/infra/cyg_ass.h>          // assertion macros
41
#include <ctype.h>
42
#include <fnmatch.h>
43
#include <string.h>
44
 
45
#define EOS     '\0'
46
 
47
static const char *rangematch(const char *, int, int);
48
 
49
static inline int
50
foldcase(int ch, int flags)
51
{
52
 
53
        if ((flags & FNM_CASEFOLD) != 0 && isupper(ch))
54
                return (tolower(ch));
55
        return (ch);
56
}
57
 
58
#define FOLDCASE(ch, flags)     foldcase((unsigned char)(ch), (flags))
59
 
60
int
61
fnmatch(const char *pattern, const char *string, int flags)
62
{
63
        const char *stringstart;
64
        char c, test;
65
 
66
        CYG_ASSERT(pattern != NULL, "pattern NULL pointer!");
67
        CYG_ASSERT(string != NULL, "string NULL pointer!");
68
 
69
        for (stringstart = string;;)
70
                switch (c = FOLDCASE(*pattern++, flags)) {
71
                case EOS:
72
                        if ((flags & FNM_LEADING_DIR) && *string == '/')
73
                                return (0);
74
                        return (*string == EOS ? 0 : FNM_NOMATCH);
75
                case '?':
76
                        if (*string == EOS)
77
                                return (FNM_NOMATCH);
78
                        if (*string == '/' && (flags & FNM_PATHNAME))
79
                                return (FNM_NOMATCH);
80
                        if (*string == '.' && (flags & FNM_PERIOD) &&
81
                            (string == stringstart ||
82
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
83
                                return (FNM_NOMATCH);
84
                        ++string;
85
                        break;
86
                case '*':
87
                        c = FOLDCASE(*pattern, flags);
88
                        /* Collapse multiple stars. */
89
                        while (c == '*')
90
                                c = FOLDCASE(*++pattern, flags);
91
 
92
                        if (*string == '.' && (flags & FNM_PERIOD) &&
93
                            (string == stringstart ||
94
                            ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
95
                                return (FNM_NOMATCH);
96
 
97
                        /* Optimize for pattern with * at end or before /. */
98
                        if (c == EOS) {
99
                                if (flags & FNM_PATHNAME)
100
                                        return ((flags & FNM_LEADING_DIR) ||
101
                                            strchr(string, '/') == NULL ?
102
 
103
                                else
104
                                        return (0);
105
                        } else if (c == '/' && flags & FNM_PATHNAME) {
106
                                if ((string = strchr(string, '/')) == NULL)
107
                                        return (FNM_NOMATCH);
108
                                break;
109
                        }
110
 
111
                        /* General case, use recursion. */
112
                        while ((test = FOLDCASE(*string, flags)) != EOS) {
113
                                if (!fnmatch(pattern, string,
114
                                             flags & ~FNM_PERIOD))
115
                                        return (0);
116
                                if (test == '/' && flags & FNM_PATHNAME)
117
                                        break;
118
                                ++string;
119
                        }
120
                        return (FNM_NOMATCH);
121
                case '[':
122
                        if (*string == EOS)
123
                                return (FNM_NOMATCH);
124
                        if (*string == '/' && flags & FNM_PATHNAME)
125
                                return (FNM_NOMATCH);
126
                        if ((pattern =
127
                            rangematch(pattern, FOLDCASE(*string, flags),
128
                                       flags)) == NULL)
129
                                return (FNM_NOMATCH);
130
                        ++string;
131
                        break;
132
                case '\\':
133
                        if (!(flags & FNM_NOESCAPE)) {
134
                                if ((c = FOLDCASE(*pattern++, flags)) == EOS) {
135
                                        c = '\\';
136
                                        --pattern;
137
                                }
138
                        }
139
                        /* FALLTHROUGH */
140
                default:
141
                        if (c != FOLDCASE(*string++, flags))
142
                                return (FNM_NOMATCH);
143
                        break;
144
                }
145
        /* NOTREACHED */
146
}
147
 
148
static const char *
149
rangematch(const char *pattern, int test, int flags)
150
{
151
        int negate, ok;
152
        char c, c2;
153
 
154
        CYG_ASSERT(pattern != NULL, "pattern NULL pointer!");
155
 
156
        /*
157
         * A bracket expression starting with an unquoted circumflex
158
         * character produces unspecified results (IEEE 1003.2-1992,
159
         * 3.13.2).  This implementation treats it like '!', for
160
         * consistency with the regular expression syntax.
161
         * J.T. Conklin (conklin@ngai.kaleida.com)
162
         */
163
        if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
164
                ++pattern;
165
 
166
        for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']';) {
167
                if (c == '\\' && !(flags & FNM_NOESCAPE))
168
                        c = FOLDCASE(*pattern++, flags);
169
                if (c == EOS)
170
                        return (NULL);
171
                if (*pattern == '-'
172
                    && (c2 = FOLDCASE(*(pattern+1), flags)) != EOS &&
173
                        c2 != ']') {
174
                        pattern += 2;
175
                        if (c2 == '\\' && !(flags & FNM_NOESCAPE))
176
                                c2 = FOLDCASE(*pattern++, flags);
177
                        if (c2 == EOS)
178
                                return (NULL);
179
                        if (c <= test && test <= c2)
180
                                ok = 1;
181
                } else if (c == test)
182
                        ok = 1;
183
        }
184
        return (ok == negate ? NULL : pattern);
185
}

powered by: WebSVN 2.1.0

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