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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [misc/] [fnmatch/] [fnmatch.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
2
 
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public License as
5
published by the Free Software Foundation; either version 2 of the
6
License, or (at your option) any later version.
7
 
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
Library General Public License for more details.
12
 
13
You should have received a copy of the GNU Library General Public
14
License along with this library; see the file COPYING.LIB.  If
15
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16
Cambridge, MA 02139, USA.  */
17
 
18
#if HAVE_CONFIG_H
19
# include <config.h>
20
#endif
21
 
22
/* Enable GNU extensions in fnmatch.h.  */
23
#ifndef _GNU_SOURCE
24
# define _GNU_SOURCE    1
25
#endif
26
 
27
#include <errno.h>
28
#include <fnmatch.h>
29
#include <ctype.h>
30
 
31
 
32
/* Comment out all this code if we are using the GNU C Library, and are not
33
   actually compiling the library itself.  This code is part of the GNU C
34
   Library, but also included in many other GNU distributions.  Compiling
35
   and linking in this code is a waste when using the GNU C library
36
   (especially if it is a shared library).  Rather than having every GNU
37
   program understand `configure --with-gnu-libc' and omit the object files,
38
   it is simpler to just do this in the source for each such file.  */
39
 
40
#if defined (_LIBC) || !defined (__GNU_LIBRARY__)
41
 
42
 
43
# if defined (STDC_HEADERS) || !defined (isascii)
44
#  define ISASCII(c) 1
45
# else
46
#  define ISASCII(c) isascii(c)
47
# endif
48
 
49
# define ISUPPER(c) (ISASCII (c) && isupper (c))
50
 
51
 
52
# ifndef errno
53
extern int errno;
54
# endif
55
 
56
/* Match STRING against the filename pattern PATTERN, returning zero if
57
   it matches, nonzero if not.  */
58
int fnmatch(pattern, string, flags)
59
const char *pattern;
60
const char *string;
61
int flags;
62
{
63
        register const char *p = pattern, *n = string;
64
        register char c;
65
 
66
/* Note that this evaluates C many times.  */
67
# define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
68
 
69
        while ((c = *p++) != '\0') {
70
                c = FOLD(c);
71
 
72
                switch (c) {
73
                case '?':
74
                        if (*n == '\0')
75
                                return FNM_NOMATCH;
76
                        else if ((flags & FNM_FILE_NAME) && *n == '/')
77
                                return FNM_NOMATCH;
78
                        else if ((flags & FNM_PERIOD) && *n == '.' &&
79
                                         (n == string
80
                                          || ((flags & FNM_FILE_NAME)
81
                                                  && n[-1] == '/'))) return FNM_NOMATCH;
82
                        break;
83
 
84
                case '\\':
85
                        if (!(flags & FNM_NOESCAPE)) {
86
                                c = *p++;
87
                                if (c == '\0')
88
                                        /* Trailing \ loses.  */
89
                                        return FNM_NOMATCH;
90
                                c = FOLD(c);
91
                        }
92
                        if (FOLD(*n) != c)
93
                                return FNM_NOMATCH;
94
                        break;
95
 
96
                case '*':
97
                        if ((flags & FNM_PERIOD) && *n == '.' &&
98
                                (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
99
                                return FNM_NOMATCH;
100
 
101
                        for (c = *p++; c == '?' || c == '*'; c = *p++) {
102
                                if ((flags & FNM_FILE_NAME) && *n == '/')
103
                                        /* A slash does not match a wildcard under FNM_FILE_NAME.  */
104
                                        return FNM_NOMATCH;
105
                                else if (c == '?') {
106
                                        /* A ? needs to match one character.  */
107
                                        if (*n == '\0')
108
                                                /* There isn't another character; no match.  */
109
                                                return FNM_NOMATCH;
110
                                        else
111
                                                /* One character of the string is consumed in matching
112
                                                   this ? wildcard, so *??? won't match if there are
113
                                                   less than three characters.  */
114
                                                ++n;
115
                                }
116
                        }
117
 
118
                        if (c == '\0')
119
                                return 0;
120
 
121
                        {
122
                                char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
123
 
124
                                c1 = FOLD(c1);
125
                                for (--p; *n != '\0'; ++n)
126
                                        if ((c == '[' || FOLD(*n) == c1) &&
127
                                                fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
128
                                                return 0;
129
                                return FNM_NOMATCH;
130
                        }
131
 
132
                case '[':
133
                {
134
                        /* Nonzero if the sense of the character class is inverted.  */
135
                        register int not;
136
 
137
                        if (*n == '\0')
138
                                return FNM_NOMATCH;
139
 
140
                        if ((flags & FNM_PERIOD) && *n == '.' &&
141
                                (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
142
                                return FNM_NOMATCH;
143
 
144
                        not = (*p == '!' || *p == '^');
145
                        if (not)
146
                                ++p;
147
 
148
                        c = *p++;
149
                        for (;;) {
150
                                register char cstart = c, cend = c;
151
 
152
                                if (!(flags & FNM_NOESCAPE) && c == '\\') {
153
                                        if (*p == '\0')
154
                                                return FNM_NOMATCH;
155
                                        cstart = cend = *p++;
156
                                }
157
 
158
                                cstart = cend = FOLD(cstart);
159
 
160
                                if (c == '\0')
161
                                        /* [ (unterminated) loses.  */
162
                                        return FNM_NOMATCH;
163
 
164
                                c = *p++;
165
                                c = FOLD(c);
166
 
167
                                if ((flags & FNM_FILE_NAME) && c == '/')
168
                                        /* [/] can never match.  */
169
                                        return FNM_NOMATCH;
170
 
171
                                if (c == '-' && *p != ']') {
172
                                        cend = *p++;
173
                                        if (!(flags & FNM_NOESCAPE) && cend == '\\')
174
                                                cend = *p++;
175
                                        if (cend == '\0')
176
                                                return FNM_NOMATCH;
177
                                        cend = FOLD(cend);
178
 
179
                                        c = *p++;
180
                                }
181
 
182
                                if (FOLD(*n) >= cstart && FOLD(*n) <= cend)
183
                                        goto matched;
184
 
185
                                if (c == ']')
186
                                        break;
187
                        }
188
                        if (!not)
189
                                return FNM_NOMATCH;
190
                        break;
191
 
192
                  matched:;
193
                        /* Skip the rest of the [...] that already matched.  */
194
                        while (c != ']') {
195
                                if (c == '\0')
196
                                        /* [... (unterminated) loses.  */
197
                                        return FNM_NOMATCH;
198
 
199
                                c = *p++;
200
                                if (!(flags & FNM_NOESCAPE) && c == '\\') {
201
                                        if (*p == '\0')
202
                                                return FNM_NOMATCH;
203
                                        /* XXX 1003.2d11 is unclear if this is right.  */
204
                                        ++p;
205
                                }
206
                        }
207
                        if (not)
208
                                return FNM_NOMATCH;
209
                }
210
                        break;
211
 
212
                default:
213
                        if (c != FOLD(*n))
214
                                return FNM_NOMATCH;
215
                }
216
 
217
                ++n;
218
        }
219
 
220
        if (*n == '\0')
221
                return 0;
222
 
223
        if ((flags & FNM_LEADING_DIR) && *n == '/')
224
                /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
225
                return 0;
226
 
227
        return FNM_NOMATCH;
228
 
229
# undef FOLD
230
}
231
 
232
#endif                                                  /* _LIBC or not __GNU_LIBRARY__.  */

powered by: WebSVN 2.1.0

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