4 /* ----- THE FOLLOWING UP TO 'END' is glibc-2.1.2 posix/fnmatch.c
5 except for the parts with '#if 0' */
7 /* Copyright (C) 1991, 92, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
8 This file is part of the GNU C Library.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this library; see the file COPYING.LIB. If not,
22 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #if 0 /* header files included better by ../rsync.h */
31 /* Enable GNU extensions in fnmatch.h. */
33 # define _GNU_SOURCE 1
40 #if HAVE_STRING_H || defined _LIBC
46 #if defined STDC_HEADERS || defined _LIBC
51 /* For platform which support the ISO C amendement 1 functionality we
52 support user defined character classes. */
53 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
54 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
59 /* Comment out all this code if we are using the GNU C Library, and are not
60 actually compiling the library itself. This code is part of the GNU C
61 Library, but also included in many other GNU distributions. Compiling
62 and linking in this code is a waste when using the GNU C library
63 (especially if it is a shared library). Rather than having every GNU
64 program understand `configure --with-gnu-libc' and omit the object files,
65 it is simpler to just do this in the source for each such file. */
69 # if defined STDC_HEADERS || !defined isascii
72 # define ISASCII(c) isascii(c)
76 # define ISBLANK(c) (ISASCII (c) && isblank (c))
78 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
81 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
83 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
86 #define ISPRINT(c) (ISASCII (c) && isprint (c))
87 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
88 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
89 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
90 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
91 #define ISLOWER(c) (ISASCII (c) && islower (c))
92 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
93 #define ISSPACE(c) (ISASCII (c) && isspace (c))
94 #define ISUPPER(c) (ISASCII (c) && isupper (c))
95 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
97 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
99 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
100 /* The GNU C library provides support for user-defined character classes
101 and the functions from ISO C amendement 1. */
102 # ifdef CHARCLASS_NAME_MAX
103 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
105 /* This shouldn't happen but some implementation might still have this
106 problem. Use a reasonable default value. */
107 # define CHAR_CLASS_MAX_LENGTH 256
111 # define IS_CHAR_CLASS(string) __wctype (string)
113 # define IS_CHAR_CLASS(string) wctype (string)
116 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
118 # define IS_CHAR_CLASS(string) \
119 (STREQ (string, "alpha") || STREQ (string, "upper") \
120 || STREQ (string, "lower") || STREQ (string, "digit") \
121 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
122 || STREQ (string, "space") || STREQ (string, "print") \
123 || STREQ (string, "punct") || STREQ (string, "graph") \
124 || STREQ (string, "cntrl") || STREQ (string, "blank"))
127 /* Avoid depending on library functions or files
128 whose names are inconsistent. */
130 # if !defined _LIBC && !defined getenv
131 extern char *getenv ();
138 /* Match STRING against the filename pattern PATTERN, returning zero if
139 it matches, nonzero if not. */
144 internal_fnmatch (const char *pattern
, const char *string
,
145 int no_leading_period
, int flags
)
147 register const char *p
= pattern
, *n
= string
;
148 register unsigned char c
;
150 /* Note that this evaluates C many times. */
152 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
154 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
157 while ((c
= *p
++) != '\0')
166 else if (*n
== '/' && (flags
& FNM_FILE_NAME
))
168 else if (*n
== '.' && no_leading_period
170 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
175 if (!(flags
& FNM_NOESCAPE
))
179 /* Trailing \ loses. */
183 if (FOLD ((unsigned char) *n
) != c
)
188 if (*n
== '.' && no_leading_period
190 || (n
[-1] == '/' && (flags
& FNM_FILE_NAME
))))
193 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
195 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
196 /* A slash does not match a wildcard under FNM_FILE_NAME. */
200 /* A ? needs to match one character. */
202 /* There isn't another character; no match. */
205 /* One character of the string is consumed in matching
206 this ? wildcard, so *??? won't match if there are
207 less than three characters. */
213 /* The wildcard(s) is/are the last element of the pattern.
214 If the name is a file name and contains another slash
215 this does mean it cannot match. */
216 return ((flags
& FNM_FILE_NAME
) && strchr (n
, '/') != NULL
223 endp
= __strchrnul (n
, (flags
& FNM_FILE_NAME
) ? '/' : '\0');
225 /* replace call to internal glibc function with equivalent */
226 if (!(flags
& FNM_FILE_NAME
) || ((endp
= strchr(n
, '/')) == NULL
))
227 endp
= n
+ strlen(n
);
232 int flags2
= ((flags
& FNM_FILE_NAME
)
233 ? flags
: (flags
& ~FNM_PERIOD
));
235 for (--p
; n
< endp
; ++n
)
236 if (internal_fnmatch (p
, n
,
246 else if (c
== '/' && (flags
& FNM_FILE_NAME
))
248 while (*n
!= '\0' && *n
!= '/')
251 && (internal_fnmatch (p
, n
+ 1, flags
& FNM_PERIOD
,
257 int flags2
= ((flags
& FNM_FILE_NAME
)
258 ? flags
: (flags
& ~FNM_PERIOD
));
260 if (c
== '\\' && !(flags
& FNM_NOESCAPE
))
263 for (--p
; n
< endp
; ++n
)
264 if (FOLD ((unsigned char) *n
) == c
265 && (internal_fnmatch (p
, n
,
276 /* If we come here no match is possible with the wildcard. */
281 /* Nonzero if the sense of the character class is inverted. */
282 static int posixly_correct
;
286 if (posixly_correct
== 0)
287 posixly_correct
= getenv ("POSIXLY_CORRECT") != NULL
? 1 : -1;
292 if (*n
== '.' && no_leading_period
&& (n
== string
298 if (*n
== '/' && (flags
& FNM_FILE_NAME
))
299 /* `/' cannot be matched. */
302 not = (*p
== '!' || (posixly_correct
< 0 && *p
== '^'));
309 unsigned char fn
= FOLD ((unsigned char) *n
);
311 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
315 c
= FOLD ((unsigned char) *p
);
321 else if (c
== '[' && *p
== ':')
323 /* Leave room for the null. */
324 char str
[CHAR_CLASS_MAX_LENGTH
+ 1];
326 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
329 const char *startp
= p
;
333 if (c1
== CHAR_CLASS_MAX_LENGTH
)
334 /* The name is too long and therefore the pattern
339 if (c
== ':' && p
[1] == ']')
344 if (c
< 'a' || c
>= 'z')
346 /* This cannot possibly be a character class name.
347 Match it as a normal range. */
356 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
357 wt
= IS_CHAR_CLASS (str
);
359 /* Invalid character class name. */
362 if (__iswctype (__btowc ((unsigned char) *n
), wt
))
365 if ((STREQ (str
, "alnum") && ISALNUM ((unsigned char) *n
))
366 || (STREQ (str
, "alpha") && ISALPHA ((unsigned char) *n
))
367 || (STREQ (str
, "blank") && ISBLANK ((unsigned char) *n
))
368 || (STREQ (str
, "cntrl") && ISCNTRL ((unsigned char) *n
))
369 || (STREQ (str
, "digit") && ISDIGIT ((unsigned char) *n
))
370 || (STREQ (str
, "graph") && ISGRAPH ((unsigned char) *n
))
371 || (STREQ (str
, "lower") && ISLOWER ((unsigned char) *n
))
372 || (STREQ (str
, "print") && ISPRINT ((unsigned char) *n
))
373 || (STREQ (str
, "punct") && ISPUNCT ((unsigned char) *n
))
374 || (STREQ (str
, "space") && ISSPACE ((unsigned char) *n
))
375 || (STREQ (str
, "upper") && ISUPPER ((unsigned char) *n
))
376 || (STREQ (str
, "xdigit") && ISXDIGIT ((unsigned char) *n
)))
381 /* [ (unterminated) loses. */
392 if (c
== '-' && *p
!= ']')
395 unsigned char cend
= *p
++;
396 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
401 if (cold
<= fn
&& fn
<= FOLD (cend
))
417 /* Skip the rest of the [...] that already matched. */
421 /* [... (unterminated) loses. */
425 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
429 /* XXX 1003.2d11 is unclear if this is right. */
432 else if (c
== '[' && *p
== ':')
437 while (*p
!= ':' || p
[1] == ']');
448 if (c
!= FOLD ((unsigned char) *n
))
458 if ((flags
& FNM_LEADING_DIR
) && *n
== '/')
459 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
469 fnmatch (pattern
, string
, flags
)
474 return internal_fnmatch (pattern
, string
, flags
& FNM_PERIOD
, flags
);
477 #endif /* _LIBC or not __GNU_LIBRARY__. */
478 /* ----- END glibc-2.1.2 posix/fnmatch.c */
480 #else /* HAVE_FNMATCH */
481 void fnmatch_dummy(void) {}