Fix fnmatch issues according to POSIX.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/\
xcu_chap02.html#tag_02_13_01
1. A [...] pattern containing a slash is not a pattern; the [ ]'s become regular
   characters
2. A [] or a [!] is not an empty pattern, why would it? The first would never
   match and the second would always match which makes it equivalent to ?
   In those cases the ] is taken as a literal character and does not have
   special meaning.
This commit is contained in:
christos 2014-10-12 22:32:33 +00:00
parent 1b519b6d17
commit 02807d50d6
1 changed files with 15 additions and 6 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: fnmatch.c,v 1.25 2012/03/25 16:31:23 christos Exp $ */ /* $NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $ */
/* /*
* Copyright (c) 1989, 1993, 1994 * Copyright (c) 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
#else #else
__RCSID("$NetBSD: fnmatch.c,v 1.25 2012/03/25 16:31:23 christos Exp $"); __RCSID("$NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -73,7 +73,7 @@ foldcase(int ch, int flags)
static const char * static const char *
rangematch(const char *pattern, int test, int flags) rangematch(const char *pattern, int test, int flags)
{ {
int negate, ok; int negate, ok, need;
char c, c2; char c, c2;
_DIAGASSERT(pattern != NULL); _DIAGASSERT(pattern != NULL);
@ -88,7 +88,11 @@ rangematch(const char *pattern, int test, int flags)
if ((negate = (*pattern == '!' || *pattern == '^')) != 0) if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
++pattern; ++pattern;
for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']';) { need = 1;
for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']' || need;) {
need = 0;
if (c == '/')
return (void *)-1;
if (c == '\\' && !(flags & FNM_NOESCAPE)) if (c == '\\' && !(flags & FNM_NOESCAPE))
c = FOLDCASE(*pattern++, flags); c = FOLDCASE(*pattern++, flags);
if (c == EOS) if (c == EOS)
@ -113,7 +117,7 @@ rangematch(const char *pattern, int test, int flags)
static int static int
fnmatchx(const char *pattern, const char *string, int flags, size_t recursion) fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
{ {
const char *stringstart; const char *stringstart, *r;
char c, test; char c, test;
_DIAGASSERT(pattern != NULL); _DIAGASSERT(pattern != NULL);
@ -184,9 +188,14 @@ fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
return FNM_NOMATCH; return FNM_NOMATCH;
if (*string == '/' && flags & FNM_PATHNAME) if (*string == '/' && flags & FNM_PATHNAME)
return FNM_NOMATCH; return FNM_NOMATCH;
if ((pattern = rangematch(pattern, if ((r = rangematch(pattern,
FOLDCASE(*string, flags), flags)) == NULL) FOLDCASE(*string, flags), flags)) == NULL)
return FNM_NOMATCH; return FNM_NOMATCH;
if (r == (void *)-1) {
if (*string != '[')
return FNM_NOMATCH;
} else
pattern = r;
++string; ++string;
break; break;
case '\\': case '\\':