menu: simplify usage for clients
[barebox-mini2440.git] / lib / fnmatch.c
blob223b9d3cdaef95577835f2482810e7513b59ce41
1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Enable GNU extensions in fnmatch.h. */
19 #ifndef _GNU_SOURCE
20 # define _GNU_SOURCE 1
21 #endif
23 #include <errno.h>
24 #include <fnmatch.h>
25 #include <linux/ctype.h>
28 /* Comment out all this code if we are using the GNU C Library, and are not
29 actually compiling the library itself. This code is part of the GNU C
30 Library, but also included in many other GNU distributions. Compiling
31 and linking in this code is a waste when using the GNU C library
32 (especially if it is a shared library). Rather than having every GNU
33 program understand `configure --with-gnu-libc' and omit the object files,
34 it is simpler to just do this in the source for each such file. */
36 # if defined (STDC_HEADERS) || !defined (isascii)
37 # define ISASCII(c) 1
38 # else
39 # define ISASCII(c) isascii(c)
40 # endif
42 # define ISUPPER(c) (ISASCII (c) && isupper (c))
45 # ifndef errno
46 extern int errno;
47 # endif
49 /* Match STRING against the filename pattern PATTERN, returning zero if
50 it matches, nonzero if not. */
51 int fnmatch(pattern, string, flags)
52 const char *pattern;
53 const char *string;
54 int flags;
56 register const char *p = pattern, *n = string;
57 register char c;
59 /* Note that this evaluates C many times. */
60 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
62 while ((c = *p++) != '\0') {
63 c = FOLD(c);
65 switch (c) {
66 case '?':
67 if (*n == '\0')
68 return FNM_NOMATCH;
69 else if ((flags & FNM_FILE_NAME) && *n == '/')
70 return FNM_NOMATCH;
71 else if ((flags & FNM_PERIOD) && *n == '.' &&
72 (n == string
73 || ((flags & FNM_FILE_NAME)
74 && n[-1] == '/'))) return FNM_NOMATCH;
75 break;
77 case '\\':
78 if (!(flags & FNM_NOESCAPE)) {
79 c = *p++;
80 if (c == '\0')
81 /* Trailing \ loses. */
82 return FNM_NOMATCH;
83 c = FOLD(c);
85 if (FOLD(*n) != c)
86 return FNM_NOMATCH;
87 break;
89 case '*':
90 if ((flags & FNM_PERIOD) && *n == '.' &&
91 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
92 return FNM_NOMATCH;
94 for (c = *p++; c == '?' || c == '*'; c = *p++) {
95 if ((flags & FNM_FILE_NAME) && *n == '/')
96 /* A slash does not match a wildcard under FNM_FILE_NAME. */
97 return FNM_NOMATCH;
98 else if (c == '?') {
99 /* A ? needs to match one character. */
100 if (*n == '\0')
101 /* There isn't another character; no match. */
102 return FNM_NOMATCH;
103 else
104 /* One character of the string is consumed in matching
105 this ? wildcard, so *??? won't match if there are
106 less than three characters. */
107 ++n;
111 if (c == '\0')
112 return 0;
115 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
117 c1 = FOLD(c1);
118 for (--p; *n != '\0'; ++n)
119 if ((c == '[' || FOLD(*n) == c1) &&
120 fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
121 return 0;
122 return FNM_NOMATCH;
125 case '[':
127 /* Nonzero if the sense of the character class is inverted. */
128 register int not;
130 if (*n == '\0')
131 return FNM_NOMATCH;
133 if ((flags & FNM_PERIOD) && *n == '.' &&
134 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
135 return FNM_NOMATCH;
137 not = (*p == '!' || *p == '^');
138 if (not)
139 ++p;
141 c = *p++;
142 for (;;) {
143 register char cstart = c, cend = c;
145 if (!(flags & FNM_NOESCAPE) && c == '\\') {
146 if (*p == '\0')
147 return FNM_NOMATCH;
148 cstart = cend = *p++;
151 cstart = cend = FOLD(cstart);
153 if (c == '\0')
154 /* [ (unterminated) loses. */
155 return FNM_NOMATCH;
157 c = *p++;
158 c = FOLD(c);
160 if ((flags & FNM_FILE_NAME) && c == '/')
161 /* [/] can never match. */
162 return FNM_NOMATCH;
164 if (c == '-' && *p != ']') {
165 cend = *p++;
166 if (!(flags & FNM_NOESCAPE) && cend == '\\')
167 cend = *p++;
168 if (cend == '\0')
169 return FNM_NOMATCH;
170 cend = FOLD(cend);
172 c = *p++;
175 if (FOLD(*n) >= cstart && FOLD(*n) <= cend)
176 goto matched;
178 if (c == ']')
179 break;
181 if (!not)
182 return FNM_NOMATCH;
183 break;
185 matched:;
186 /* Skip the rest of the [...] that already matched. */
187 while (c != ']') {
188 if (c == '\0')
189 /* [... (unterminated) loses. */
190 return FNM_NOMATCH;
192 c = *p++;
193 if (!(flags & FNM_NOESCAPE) && c == '\\') {
194 if (*p == '\0')
195 return FNM_NOMATCH;
196 /* XXX 1003.2d11 is unclear if this is right. */
197 ++p;
200 if (not)
201 return FNM_NOMATCH;
203 break;
205 default:
206 if (c != FOLD(*n))
207 return FNM_NOMATCH;
210 ++n;
213 if (*n == '\0')
214 return 0;
216 if ((flags & FNM_LEADING_DIR) && *n == '/')
217 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
218 return 0;
220 return FNM_NOMATCH;
222 # undef FOLD