]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/glob/fnmatch.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
22 #if !defined (__GNU_LIBRARY__) && !defined (STDC_HEADERS)
28 /* Match STRING against the filename pattern PATTERN, returning zero if
29 it matches, FNM_NOMATCH if not. */
31 fnmatch (pattern
, string
, flags
)
36 register char *p
= pattern
, *n
= string
;
39 if ((flags
& ~__FNM_FLAGS
) != 0)
45 while ((c
= *p
++) != '\0')
52 else if ((flags
& FNM_PATHNAME
) && *n
== '/')
54 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
55 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
60 if (!(flags
& FNM_NOESCAPE
))
67 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
68 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
71 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++, ++n
)
72 if (((flags
& FNM_PATHNAME
) && *n
== '/') ||
73 (c
== '?' && *n
== '\0'))
80 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
81 for (--p
; *n
!= '\0'; ++n
)
82 if ((c
== '[' || *n
== c1
) &&
83 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
90 /* Nonzero if the sense of the character class is inverted. */
96 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
97 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
100 /* Make sure there is a closing `]'. If there isn't, the `['
101 is just a character to be matched. */
105 for (np
= p
; np
&& *np
&& *np
!= ']'; np
++);
110 return (FNM_NOMATCH
);
115 not = (*p
== '!' || *p
== '^');
122 register char cstart
= c
, cend
= c
;
124 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
125 cstart
= cend
= *p
++;
128 /* [ (unterminated) loses. */
129 return (FNM_NOMATCH
);
133 if ((flags
& FNM_PATHNAME
) && c
== '/')
134 /* [/] can never match. */
135 return (FNM_NOMATCH
);
137 if (c
== '-' && *p
!= ']')
140 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
143 return (FNM_NOMATCH
);
147 if (*n
>= cstart
&& *n
<= cend
)
154 return (FNM_NOMATCH
);
160 /* Skip the rest of the [...] that already matched. */
164 /* [... (unterminated) loses. */
165 return (FNM_NOMATCH
);
168 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
169 /* 1003.2d11 is unclear if this is right. %%% */
173 return (FNM_NOMATCH
);
179 return (FNM_NOMATCH
);
188 return (FNM_NOMATCH
);