Debian Codegraph @

lib/fnmatch.c

raw
1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2023 Free Software
2 Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the Free
17 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18 USA. */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 /* Enable GNU extensions in fnmatch.h. */
25 #ifndef _GNU_SOURCE
26 # define _GNU_SOURCE 1
27 #endif
28
29 #include <errno.h>
30 #include <fnmatch.h>
31 #include <ctype.h>
32
33 #if HAVE_STRING_H || defined _LIBC
34 # include <string.h>
35 #else
36 # include <strings.h>
37 #endif
38
39 #if defined STDC_HEADERS || defined _LIBC
40 # include <stdlib.h>
41 #endif
42
43 /* For platform which support the ISO C amendement 1 functionality we
44 support user defined character classes. */
45 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
46 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
47 # include <wchar.h>
48 # include <wctype.h>
49 #endif
50
51 /* Comment out all this code if we are using the GNU C Library, and are not
52 actually compiling the library itself. This code is part of the GNU C
53 Library, but also included in many other GNU distributions. Compiling
54 and linking in this code is a waste when using the GNU C library
55 (especially if it is a shared library). Rather than having every GNU
56 program understand `configure --with-gnu-libc' and omit the object files,
57 it is simpler to just do this in the source for each such file. */
58
59 #if defined _LIBC || !defined __GNU_LIBRARY__
60
61
62 # if defined STDC_HEADERS || !defined isascii
63 # define ISASCII(c) 1
64 # else
65 # define ISASCII(c) isascii(c)
66 # endif
67
68 # ifdef isblank
69 # define ISBLANK(c) (ISASCII (c) && isblank (c))
70 # else
71 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
72 # endif
73 # ifdef isgraph
74 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
75 # else
76 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
77 # endif
78
79 # define ISPRINT(c) (ISASCII (c) && isprint (c))
80 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
81 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
82 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
83 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
84 # define ISLOWER(c) (ISASCII (c) && islower (c))
85 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
86 # define ISSPACE(c) (ISASCII (c) && isspace (c))
87 # define ISUPPER(c) (ISASCII (c) && isupper (c))
88 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
89
90 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
91
92 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
93 /* The GNU C library provides support for user-defined character classes
94 and the functions from ISO C amendement 1. */
95 # ifdef CHARCLASS_NAME_MAX
96 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
97 # else
98 /* This shouldn't happen but some implementation might still have this
99 problem. Use a reasonable default value. */
100 # define CHAR_CLASS_MAX_LENGTH 256
101 # endif
102
103 # ifdef _LIBC
104 # define IS_CHAR_CLASS(string) __wctype (string)
105 # else
106 # define IS_CHAR_CLASS(string) wctype (string)
107 # endif
108 # else
109 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
110
111 # define IS_CHAR_CLASS(string) \
112 (STREQ (string, "alpha") || STREQ (string, "upper") \
113 || STREQ (string, "lower") || STREQ (string, "digit") \
114 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
115 || STREQ (string, "space") || STREQ (string, "print") \
116 || STREQ (string, "punct") || STREQ (string, "graph") \
117 || STREQ (string, "cntrl") || STREQ (string, "blank"))
118 # endif
119
120 /* Avoid depending on library functions or files
121 whose names are inconsistent. */
122
123 # if !defined _LIBC && !defined getenv
124 extern char *getenv ();
125 # endif
126
127 # ifndef errno
128 extern int errno;
129 # endif
130
131 /* Match STRING against the filename pattern PATTERN, returning zero if
132 it matches, nonzero if not. */
133 static int
134 internal_fnmatch (const char *pattern, const char *string,
135 int no_leading_period, int flags)
136 {
137 const char *p = pattern, *n = string;
138 unsigned char c;
139
140 /* Note that this evaluates C many times. */
141 # ifdef _LIBC
142 # define FOLD(c) (unsigned char)((flags & FNM_CASEFOLD) ? tolower (c) : (c))
143 # else
144 # define FOLD(c) (unsigned char)((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
145 # endif
146
147 while ((c = *p++) != '\0')
148 {
149 c = FOLD (c);
150
151 switch (c)
152 {
153 case '?':
154 if (*n == '\0')
155 return FNM_NOMATCH;
156 else if (*n == '/' && (flags & FNM_FILE_NAME))
157 return FNM_NOMATCH;
158 else if (*n == '.' && no_leading_period
159 && (n == string
160 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
161 return FNM_NOMATCH;
162 break;
163
164 case '\\':
165 if (!(flags & FNM_NOESCAPE))
166 {
167 c = *p++;
168 if (c == '\0')
169 /* Trailing \ loses. */
170 return FNM_NOMATCH;
171 c = FOLD (c);
172 }
173 if (FOLD ((unsigned char) *n) != c)
174 return FNM_NOMATCH;
175 break;
176
177 case '*':
178 if (*n == '.' && no_leading_period
179 && (n == string
180 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
181 return FNM_NOMATCH;
182
183 for (c = *p++; c == '?' || c == '*'; c = *p++)
184 {
185 if (*n == '/' && (flags & FNM_FILE_NAME))
186 /* A slash does not match a wildcard under FNM_FILE_NAME. */
187 return FNM_NOMATCH;
188 else if (c == '?')
189 {
190 /* A ? needs to match one character. */
191 if (*n == '\0')
192 /* There isn't another character; no match. */
193 return FNM_NOMATCH;
194 else
195 /* One character of the string is consumed in matching
196 this ? wildcard, so *??? won't match if there are
197 less than three characters. */
198 ++n;
199 }
200 }
201
202 if (c == '\0')
203 /* The wildcard(s) is/are the last element of the pattern.
204 If the name is a file name and contains another slash
205 this does mean it cannot match. */
206 return ((flags & FNM_FILE_NAME) && strchr (n, '/') != NULL
207 ? FNM_NOMATCH : 0);
208 else
209 {
210 const char *endp;
211
212 endp = strchr (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
213 if (endp == NULL)
214 endp = n + strlen (n);
215
216 if (c == '[')
217 {
218 int flags2 = ((flags & FNM_FILE_NAME)
219 ? flags : (flags & ~FNM_PERIOD));
220
221 for (--p; n < endp; ++n)
222 if (internal_fnmatch (p, n,
223 (no_leading_period
224 && (n == string
225 || (n[-1] == '/'
226 && (flags
227 & FNM_FILE_NAME)))),
228 flags2)
229 == 0)
230 return 0;
231 }
232 else if (c == '/' && (flags & FNM_FILE_NAME))
233 {
234 while (*n != '\0' && *n != '/')
235 ++n;
236 if (*n == '/'
237 && (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
238 flags) == 0))
239 return 0;
240 }
241 else
242 {
243 int flags2 = ((flags & FNM_FILE_NAME)
244 ? flags : (flags & ~FNM_PERIOD));
245
246 if (c == '\\' && !(flags & FNM_NOESCAPE))
247 c = *p;
248 c = FOLD (c);
249 for (--p; n < endp; ++n)
250 if (FOLD ((unsigned char) *n) == c
251 && (internal_fnmatch (p, n,
252 (no_leading_period
253 && (n == string
254 || (n[-1] == '/'
255 && (flags
256 & FNM_FILE_NAME)))),
257 flags2) == 0))
258 return 0;
259 }
260 }
261
262 /* If we come here no match is possible with the wildcard. */
263 return FNM_NOMATCH;
264
265 case '[':
266 {
267 /* Nonzero if the sense of the character class is inverted. */
268 static int posixly_correct;
269 int not;
270 char cold;
271
272 if (posixly_correct == 0)
273 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
274
275 if (*n == '\0')
276 return FNM_NOMATCH;
277
278 if (*n == '.' && no_leading_period && (n == string
279 || (n[-1] == '/'
280 && (flags
281 & FNM_FILE_NAME))))
282 return FNM_NOMATCH;
283
284 if (*n == '/' && (flags & FNM_FILE_NAME))
285 /* `/' cannot be matched. */
286 return FNM_NOMATCH;
287
288 not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
289 if (not)
290 ++p;
291
292 c = *p++;
293 for (;;)
294 {
295 unsigned char fn = FOLD ((unsigned char) *n);
296
297 if (!(flags & FNM_NOESCAPE) && c == '\\')
298 {
299 if (*p == '\0')
300 return FNM_NOMATCH;
301 c = FOLD ((unsigned char) *p);
302 ++p;
303
304 if (c == fn)
305 goto matched;
306 }
307 else if (c == '[' && *p == ':')
308 {
309 /* Leave room for the null. */
310 char str[CHAR_CLASS_MAX_LENGTH + 1];
311 size_t c1 = 0;
312 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
313 wctype_t wt;
314 # endif
315 const char *startp = p;
316
317 for (;;)
318 {
319 if (c1 == CHAR_CLASS_MAX_LENGTH)
320 /* The name is too long and therefore the pattern
321 is ill-formed. */
322 return FNM_NOMATCH;
323
324 c = *++p;
325 if (c == ':' && p[1] == ']')
326 {
327 p += 2;
328 break;
329 }
330 if (c < 'a' || c >= 'z')
331 {
332 /* This cannot possibly be a character class name.
333 Match it as a normal range. */
334 p = startp;
335 c = '[';
336 goto normal_bracket;
337 }
338 str[c1++] = c;
339 }
340 str[c1] = '\0';
341
342 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
343 wt = IS_CHAR_CLASS (str);
344 if (wt == 0)
345 /* Invalid character class name. */
346 return FNM_NOMATCH;
347
348 if (__iswctype (__btowc ((unsigned char) *n), wt))
349 goto matched;
350 # else
351 if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
352 || (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
353 || (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
354 || (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
355 || (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
356 || (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
357 || (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
358 || (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
359 || (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
360 || (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
361 || (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
362 || (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
363 goto matched;
364 # endif
365 }
366 else if (c == '\0')
367 /* [ (unterminated) loses. */
368 return FNM_NOMATCH;
369 else
370 {
371 normal_bracket:
372 if (FOLD (c) == fn)
373 goto matched;
374
375 cold = c;
376 c = *p++;
377
378 if (c == '-' && *p != ']')
379 {
380 /* It is a range. */
381 unsigned char cend = *p++;
382 if (!(flags & FNM_NOESCAPE) && cend == '\\')
383 cend = *p++;
384 if (cend == '\0')
385 return FNM_NOMATCH;
386
387 if (cold <= fn && fn <= FOLD (cend))
388 goto matched;
389
390 c = *p++;
391 }
392 }
393
394 if (c == ']')
395 break;
396 }
397
398 if (!not)
399 return FNM_NOMATCH;
400 break;
401
402 matched:
403 /* Skip the rest of the [...] that already matched. */
404 while (c != ']')
405 {
406 if (c == '\0')
407 /* [... (unterminated) loses. */
408 return FNM_NOMATCH;
409
410 c = *p++;
411 if (!(flags & FNM_NOESCAPE) && c == '\\')
412 {
413 if (*p == '\0')
414 return FNM_NOMATCH;
415 /* XXX 1003.2d11 is unclear if this is right. */
416 ++p;
417 }
418 else if (c == '[' && *p == ':')
419 {
420 do
421 if (*++p == '\0')
422 return FNM_NOMATCH;
423 while (*p != ':' || p[1] == ']');
424 p += 2;
425 c = *p;
426 }
427 }
428 if (not)
429 return FNM_NOMATCH;
430 }
431 break;
432
433 default:
434 if (c != FOLD ((unsigned char) *n))
435 return FNM_NOMATCH;
436 }
437
438 ++n;
439 }
440
441 if (*n == '\0')
442 return 0;
443
444 if ((flags & FNM_LEADING_DIR) && *n == '/')
445 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
446 return 0;
447
448 return FNM_NOMATCH;
449
450 # undef FOLD
451 }
452
453
454 int
455 fnmatch (const char *pattern, const char *string, int flags)
456 {
457 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
458 }
459
460 #endif /* _LIBC or not __GNU_LIBRARY__. */