Commit | Line | Data |
---|---|---|
86bc0974 ILT |
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 | ||
8b5b6ebd ILT |
20 | /* Modified for GNU ld to include sysdep.h. */ |
21 | #include "sysdep.h" | |
22 | ||
86bc0974 ILT |
23 | #ifdef HAVE_CONFIG_H |
24 | #if defined (CONFIG_BROKETS) | |
25 | /* We use <config.h> instead of "config.h" so that a compilation | |
26 | using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h | |
27 | (which it would do because it found this file in $srcdir). */ | |
28 | #include <config.h> | |
29 | #else | |
30 | #include "config.h" | |
31 | #endif | |
32 | #endif | |
33 | ||
34 | ||
35 | #ifndef _GNU_SOURCE | |
36 | #define _GNU_SOURCE | |
37 | #endif | |
38 | ||
39 | #include <errno.h> | |
40 | #include <fnmatch.h> | |
41 | #include <ctype.h> | |
42 | ||
43 | ||
44 | /* Comment out all this code if we are using the GNU C Library, and are not | |
45 | actually compiling the library itself. This code is part of the GNU C | |
46 | Library, but also included in many other GNU distributions. Compiling | |
47 | and linking in this code is a waste when using the GNU C library | |
48 | (especially if it is a shared library). Rather than having every GNU | |
49 | program understand `configure --with-gnu-libc' and omit the object files, | |
50 | it is simpler to just do this in the source for each such file. */ | |
51 | ||
52 | #if defined (_LIBC) || !defined (__GNU_LIBRARY__) | |
53 | ||
54 | ||
55 | #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS) | |
56 | extern int errno; | |
57 | #endif | |
58 | ||
59 | /* Match STRING against the filename pattern PATTERN, returning zero if | |
60 | it matches, nonzero if not. */ | |
61 | int | |
62 | fnmatch (pattern, string, flags) | |
63 | const char *pattern; | |
64 | const char *string; | |
65 | int flags; | |
66 | { | |
67 | register const char *p = pattern, *n = string; | |
68 | register char c; | |
69 | ||
70 | /* Note that this evalutes C many times. */ | |
71 | #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c)) | |
72 | ||
73 | while ((c = *p++) != '\0') | |
74 | { | |
75 | c = FOLD (c); | |
76 | ||
77 | switch (c) | |
78 | { | |
79 | case '?': | |
80 | if (*n == '\0') | |
81 | return FNM_NOMATCH; | |
82 | else if ((flags & FNM_FILE_NAME) && *n == '/') | |
83 | return FNM_NOMATCH; | |
84 | else if ((flags & FNM_PERIOD) && *n == '.' && | |
85 | (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) | |
86 | return FNM_NOMATCH; | |
87 | break; | |
88 | ||
89 | case '\\': | |
90 | if (!(flags & FNM_NOESCAPE)) | |
91 | { | |
92 | c = *p++; | |
93 | c = FOLD (c); | |
94 | } | |
95 | if (FOLD (*n) != c) | |
96 | return FNM_NOMATCH; | |
97 | break; | |
98 | ||
99 | case '*': | |
100 | if ((flags & FNM_PERIOD) && *n == '.' && | |
101 | (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) | |
102 | return FNM_NOMATCH; | |
103 | ||
104 | for (c = *p++; c == '?' || c == '*'; c = *p++, ++n) | |
105 | if (((flags & FNM_FILE_NAME) && *n == '/') || | |
106 | (c == '?' && *n == '\0')) | |
107 | return FNM_NOMATCH; | |
108 | ||
109 | if (c == '\0') | |
110 | return 0; | |
111 | ||
112 | { | |
113 | char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c; | |
114 | c1 = FOLD (c1); | |
115 | for (--p; *n != '\0'; ++n) | |
116 | if ((c == '[' || FOLD (*n) == c1) && | |
117 | fnmatch (p, n, flags & ~FNM_PERIOD) == 0) | |
118 | return 0; | |
119 | return FNM_NOMATCH; | |
120 | } | |
121 | ||
122 | case '[': | |
123 | { | |
124 | /* Nonzero if the sense of the character class is inverted. */ | |
125 | register int not; | |
126 | ||
127 | if (*n == '\0') | |
128 | return FNM_NOMATCH; | |
129 | ||
130 | if ((flags & FNM_PERIOD) && *n == '.' && | |
131 | (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) | |
132 | return FNM_NOMATCH; | |
133 | ||
134 | not = (*p == '!' || *p == '^'); | |
135 | if (not) | |
136 | ++p; | |
137 | ||
138 | c = *p++; | |
139 | for (;;) | |
140 | { | |
141 | register char cstart = c, cend = c; | |
142 | ||
143 | if (!(flags & FNM_NOESCAPE) && c == '\\') | |
144 | cstart = cend = *p++; | |
145 | ||
146 | cstart = cend = FOLD (cstart); | |
147 | ||
148 | if (c == '\0') | |
149 | /* [ (unterminated) loses. */ | |
150 | return FNM_NOMATCH; | |
151 | ||
152 | c = *p++; | |
153 | c = FOLD (c); | |
154 | ||
155 | if ((flags & FNM_FILE_NAME) && c == '/') | |
156 | /* [/] can never match. */ | |
157 | return FNM_NOMATCH; | |
158 | ||
159 | if (c == '-' && *p != ']') | |
160 | { | |
161 | cend = *p++; | |
162 | if (!(flags & FNM_NOESCAPE) && cend == '\\') | |
163 | cend = *p++; | |
164 | if (cend == '\0') | |
165 | return FNM_NOMATCH; | |
166 | cend = FOLD (cend); | |
167 | ||
168 | c = *p++; | |
169 | } | |
170 | ||
171 | if (FOLD (*n) >= cstart && FOLD (*n) <= cend) | |
172 | goto matched; | |
173 | ||
174 | if (c == ']') | |
175 | break; | |
176 | } | |
177 | if (!not) | |
178 | return FNM_NOMATCH; | |
179 | break; | |
180 | ||
181 | matched:; | |
182 | /* Skip the rest of the [...] that already matched. */ | |
183 | while (c != ']') | |
184 | { | |
185 | if (c == '\0') | |
186 | /* [... (unterminated) loses. */ | |
187 | return FNM_NOMATCH; | |
188 | ||
189 | c = *p++; | |
190 | if (!(flags & FNM_NOESCAPE) && c == '\\') | |
191 | /* XXX 1003.2d11 is unclear if this is right. */ | |
192 | ++p; | |
193 | } | |
194 | if (not) | |
195 | return FNM_NOMATCH; | |
196 | } | |
197 | break; | |
198 | ||
199 | default: | |
200 | if (c != FOLD (*n)) | |
201 | return FNM_NOMATCH; | |
202 | } | |
203 | ||
204 | ++n; | |
205 | } | |
206 | ||
207 | if (*n == '\0') | |
208 | return 0; | |
209 | ||
210 | if ((flags & FNM_LEADING_DIR) && *n == '/') | |
211 | /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ | |
212 | return 0; | |
213 | ||
214 | return FNM_NOMATCH; | |
215 | } | |
216 | ||
217 | #endif /* _LIBC or not __GNU_LIBRARY__. */ |