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