1 /* Copyright (C) 1991, 1992 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. */
21 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
25 /* Match STRING against the filename pattern PATTERN, returning zero if
26 it matches, nonzero if not. */
28 fnmatch (pattern
, string
, flags
)
33 register const char *p
= pattern
, *n
= string
;
36 if ((flags
& ~__FNM_FLAGS
) != 0)
42 while ((c
= *p
++) != '\0')
49 else if ((flags
& FNM_PATHNAME
) && *n
== '/')
51 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
52 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
57 if (!(flags
& FNM_NOESCAPE
))
64 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
65 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
68 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++, ++n
)
69 if (((flags
& FNM_PATHNAME
) && *n
== '/') ||
70 (c
== '?' && *n
== '\0'))
77 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
78 for (--p
; *n
!= '\0'; ++n
)
79 if ((c
== '[' || *n
== c1
) &&
80 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
87 /* Nonzero if the sense of the character class is inverted. */
93 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
94 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
97 not = (*p
== '!' || *p
== '^');
104 register char cstart
= c
, cend
= c
;
106 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
107 cstart
= cend
= *p
++;
110 /* [ (unterminated) loses. */
115 if ((flags
& FNM_PATHNAME
) && c
== '/')
116 /* [/] can never match. */
119 if (c
== '-' && *p
!= ']')
122 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
129 if (*n
>= cstart
&& *n
<= cend
)
140 /* Skip the rest of the [...] that already matched. */
144 /* [... (unterminated) loses. */
148 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
149 /* 1003.2d11 is unclear if this is right. %%% */