1 /* Pattern matching (globbing).
2 Copyright (C) 1991, 1996, 1997 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget 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.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
30 /* NOTE: Some Un*xes have their own fnmatch() -- yet, they are
31 reportedly unreliable and buggy. Thus I chose never to use it;
32 this version (from GNU Bash) is used unconditionally. */
40 /* Match STRING against the filename pattern PATTERN, returning zero
41 if it matches, FNM_NOMATCH if not. */
43 fnmatch (const char *pattern, const char *string, int flags)
45 register const char *p = pattern, *n = string;
48 if ((flags & ~__FNM_FLAGS) != 0)
54 while ((c = *p++) != '\0')
61 else if ((flags & FNM_PATHNAME) && *n == '/')
63 else if ((flags & FNM_PERIOD) && *n == '.' &&
64 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
69 if (!(flags & FNM_NOESCAPE))
76 if ((flags & FNM_PERIOD) && *n == '.' &&
77 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
80 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
81 if (((flags & FNM_PATHNAME) && *n == '/') ||
82 (c == '?' && *n == '\0'))
89 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
90 for (--p; *n != '\0'; ++n)
91 if ((c == '[' || *n == c1) &&
92 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
99 /* Nonzero if the sense of the character class is
104 return (FNM_NOMATCH);
106 if ((flags & FNM_PERIOD) && *n == '.' &&
107 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
108 return (FNM_NOMATCH);
110 /* Make sure there is a closing `]'. If there isn't,
111 the `[' is just a character to be matched. */
113 register const char *np;
115 for (np = p; np && *np && *np != ']'; np++);
120 return (FNM_NOMATCH);
125 not = (*p == '!' || *p == '^');
132 register char cstart = c, cend = c;
134 if (!(flags & FNM_NOESCAPE) && c == '\\')
135 cstart = cend = *p++;
138 /* [ (unterminated) loses. */
139 return (FNM_NOMATCH);
143 if ((flags & FNM_PATHNAME) && c == '/')
144 /* [/] can never match. */
145 return (FNM_NOMATCH);
147 if (c == '-' && *p != ']')
150 if (!(flags & FNM_NOESCAPE) && cend == '\\')
153 return (FNM_NOMATCH);
157 if (*n >= cstart && *n <= cend)
164 return (FNM_NOMATCH);
170 /* Skip the rest of the [...] that already matched. */
174 /* [... (unterminated) loses. */
175 return (FNM_NOMATCH);
178 if (!(flags & FNM_NOESCAPE) && c == '\\')
179 /* 1003.2d11 is unclear if this is right. %%% */
183 return (FNM_NOMATCH);
189 return (FNM_NOMATCH);
198 return (FNM_NOMATCH);
201 /* Return non-zero if S contains globbing wildcards (`*', `?', `[' or
204 has_wildcards_p (const char *s)
207 if (*s == '*' || *s == '?' || *s == '[' || *s == ']')