Pull in newer fnmatch(3) that supports FNM_CASEFOLD

Check for FNM_CASEFOLD in configure
This commit is contained in:
Todd C. Miller
2000-03-23 15:11:50 +00:00
parent e5bd3d9683
commit 4fc832c6c2
4 changed files with 174 additions and 80 deletions

4
aclocal.m4 vendored
View File

@@ -233,11 +233,11 @@ dnl
dnl check for working fnmatch(3) dnl check for working fnmatch(3)
dnl dnl
AC_DEFUN(SUDO_FUNC_FNMATCH, AC_DEFUN(SUDO_FUNC_FNMATCH,
[AC_MSG_CHECKING(for working fnmatch) [AC_MSG_CHECKING(for working fnmatch with FNM_CASEFOLD)
AC_CACHE_VAL(sudo_cv_func_fnmatch, AC_CACHE_VAL(sudo_cv_func_fnmatch,
[rm -f conftestdata; > conftestdata [rm -f conftestdata; > conftestdata
AC_TRY_RUN([#include <fnmatch.h> AC_TRY_RUN([#include <fnmatch.h>
main() { exit(fnmatch("/*/bin/echo *", "/usr/bin/echo just a test", 0)); } main() { exit(fnmatch("/*/bin/echo *", "/usr/bin/echo just a test", FNM_CASEFOLD)); }
], sudo_cv_func_fnmatch=yes, sudo_cv_func_fnmatch=no, ], sudo_cv_func_fnmatch=yes, sudo_cv_func_fnmatch=no,
sudo_cv_func_fnmatch=no) sudo_cv_func_fnmatch=no)
rm -f core core.* *.core])dnl rm -f core core.* *.core])dnl

6
configure vendored
View File

@@ -6391,8 +6391,8 @@ else
LIBOBJS="$LIBOBJS utime.o" LIBOBJS="$LIBOBJS utime.o"
fi fi
echo $ac_n "checking for working fnmatch""... $ac_c" 1>&6 echo $ac_n "checking for working fnmatch with FNM_CASEFOLD""... $ac_c" 1>&6
echo "configure:6396: checking for working fnmatch" >&5 echo "configure:6396: checking for working fnmatch with FNM_CASEFOLD" >&5
if eval "test \"`echo '$''{'sudo_cv_func_fnmatch'+set}'`\" = set"; then if eval "test \"`echo '$''{'sudo_cv_func_fnmatch'+set}'`\" = set"; then
echo $ac_n "(cached) $ac_c" 1>&6 echo $ac_n "(cached) $ac_c" 1>&6
else else
@@ -6404,7 +6404,7 @@ else
#line 6405 "configure" #line 6405 "configure"
#include "confdefs.h" #include "confdefs.h"
#include <fnmatch.h> #include <fnmatch.h>
main() { exit(fnmatch("/*/bin/echo *", "/usr/bin/echo just a test", 0)); } main() { exit(fnmatch("/*/bin/echo *", "/usr/bin/echo just a test", FNM_CASEFOLD)); }
EOF EOF
if { (eval echo configure:6411: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null if { (eval echo configure:6411: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null

153
fnmatch.3
View File

@@ -1,3 +1,5 @@
.\" $OpenBSD: fnmatch.3,v 1.7 1999/06/05 04:47:41 aaron Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993 .\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
.\" .\"
@@ -11,7 +13,11 @@
.\" 2. Redistributions in binary form must reproduce the above copyright .\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the .\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution. .\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors .\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software .\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission. .\" without specific prior written permission.
.\" .\"
@@ -29,75 +35,114 @@
.\" .\"
.\" @(#)fnmatch.3 8.3 (Berkeley) 4/28/95 .\" @(#)fnmatch.3 8.3 (Berkeley) 4/28/95
.\" .\"
.TH FNMATCH 3 "April 28, 1995" .Dd April 28, 1995
.UC 7 .Dt FNMATCH 3
.SH NAME .Os
fnmatch \- match filename or pathname .Sh NAME
.SH SYNOPSIS .Nm fnmatch
.nf .Nd match filename or pathname using shell globbing rules
#include <fnmatch.h> .Sh SYNOPSIS
.Fd #include <fnmatch.h>
int fnmatch(const char *pattern, const char *string, int flags); .Ft int
.fi .Fn fnmatch "const char *pattern" "const char *string" "int flags"
.SH DESCRIPTION .Sh DESCRIPTION
.I Fnmatch
matches patterns according to the rules used by the shell.
It checks the string specified by the
.I string
argument to see if it matches the pattern specified by the
.I pattern
argument.
.PP
The The
.I flags .Fn fnmatch
function
matches patterns according to the globbing rules used by the shell.
It checks the string specified by the
.Fa string
argument to see if it matches the pattern specified by the
.Fa pattern
argument.
.Pp
The
.Fa flags
argument modifies the interpretation of argument modifies the interpretation of
.I pattern .Fa pattern
and and
.I string. .Fa string .
The value of The value of
.I flags .Fa flags
is the bitwise inclusive OR of any of the following is the bitwise inclusive
.Tn OR
of any of the following
constants, which are defined in the include file constants, which are defined in the include file
.IR fnmatch.h . .Aq Pa fnmatch.h .
.TP .Bl -tag -width FNM_PATHNAME
FNM_NOESCAPE .It Dv FNM_NOESCAPE
Normally, every occurrence of a backslash (`\e') followed by a character in Normally, every occurrence of a backslash
.I pattern .Pq Sq \e
followed by a character in
.Fa pattern
is replaced by that character. is replaced by that character.
This is done to negate any special meaning for the character. This is done to negate any special meaning for the character.
If the If the
.I FNM_NOESCAPE .Dv FNM_NOESCAPE
flag is set, a backslash character is treated as an ordinary character. flag is set, a backslash character is treated as an ordinary character.
.TP .It Dv FNM_PATHNAME
FNM_PATHNAME
Slash characters in Slash characters in
.I string .Fa string
must be explicitly matched by slashes in must be explicitly matched by slashes in
.IR pattern . .Fa pattern .
If this flag is not set, then slashes are treated as regular characters. If this flag is not set, then slashes are treated as regular characters.
.TP .It Dv FNM_PERIOD
FNM_PERIOD Leading periods in
Leading periods in strings match periods in patterns. .Fa string
The definition of ``leading'' is related to the specification of must be explicitly matched by periods in
.I FNM_PATHNAME. .Fa pattern .
A period is always ``leading'' if it is the first character in If this flag is not set, then leading periods are treated as regular
.I string . characters.
The definition of
.Dq leading
is related to the specification of
.Dv FNM_PATHNAME .
A period is always leading
if it is the first character in
.Fa string .
Additionally, if Additionally, if
.I FNM_PATHNAME .Dv FNM_PATHNAME
is set, is set,
a period is ``leading'' if it immediately follows a slash. a period is leading
.SH RETURN VALUES if it immediately follows a slash.
.It Dv FNM_LEADING_DIR
Ignore
.Nm /*
rest after successful
.Fa pattern
matching.
.It Dv FNM_CASEFOLD
Ignore case distinctions in both the
.Fa pattern
and the
.Fa string .
.El
.Sh RETURN VALUES
The The
.I fnmatch .Fn fnmatch
function returns zero if function returns zero if
.I string .Fa string
matches the pattern specified by matches the pattern specified by
.I pattern , .Fa pattern ,
otherwise, it returns the value otherwise, it returns the value
.I FNM_NOMATCH . .Dv FNM_NOMATCH .
.SH SEE ALSO .Sh SEE ALSO
sh(1), glob(3), regex(3), regexp(3). .Xr sh 1 ,
.SH BUGS .Xr glob 3 ,
The pattern ``*'' matches the empty string, even if .Xr regex 3
.I FNM_PATHNAME .Sh STANDARDS
The
.Fn fnmatch
function conforms to
.St -p1003.2-92 .
.Sh HISTORY
The
.Fn fnmatch
function first appeared in
.Bx 4.4 .
.Sh BUGS
The pattern
.Ql *
matches the empty string, even if
.Dv FNM_PATHNAME
is specified. is specified.

View File

@@ -13,7 +13,11 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors * 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -31,7 +35,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
/* /*
@@ -41,6 +45,7 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
#include "config.h" #include "config.h"
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
#include <string.h> #include <string.h>
@@ -55,7 +60,11 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
#undef EOS #undef EOS
#define EOS '\0' #define EOS '\0'
static const char *rangematch __P((const char *, int, int)); #define RANGE_MATCH 1
#define RANGE_NOMATCH 0
#define RANGE_ERROR (-1)
static int rangematch __P((const char *, char, int, char **));
int int
fnmatch(pattern, string, flags) fnmatch(pattern, string, flags)
@@ -63,11 +72,14 @@ fnmatch(pattern, string, flags)
int flags; int flags;
{ {
const char *stringstart; const char *stringstart;
char *newp;
char c, test; char c, test;
for (stringstart = string;;) for (stringstart = string;;)
switch (c = *pattern++) { switch (c = *pattern++) {
case EOS: case EOS:
if ((flags & FNM_LEADING_DIR) && *string == '/')
return (0);
return (*string == EOS ? 0 : FNM_NOMATCH); return (*string == EOS ? 0 : FNM_NOMATCH);
case '?': case '?':
if (*string == EOS) if (*string == EOS)
@@ -92,13 +104,14 @@ fnmatch(pattern, string, flags)
return (FNM_NOMATCH); return (FNM_NOMATCH);
/* Optimize for pattern with * at end or before /. */ /* Optimize for pattern with * at end or before /. */
if (c == EOS) if (c == EOS) {
if (flags & FNM_PATHNAME) if (flags & FNM_PATHNAME)
return (strchr(string, '/') == NULL ? return ((flags & FNM_LEADING_DIR) ||
strchr(string, '/') == NULL ?
0 : FNM_NOMATCH); 0 : FNM_NOMATCH);
else else
return (0); return (0);
else if (c == '/' && flags & FNM_PATHNAME) { } else if (c == '/' && (flags & FNM_PATHNAME)) {
if ((string = strchr(string, '/')) == NULL) if ((string = strchr(string, '/')) == NULL)
return (FNM_NOMATCH); return (FNM_NOMATCH);
break; break;
@@ -108,7 +121,7 @@ fnmatch(pattern, string, flags)
while ((test = *string) != EOS) { while ((test = *string) != EOS) {
if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
return (0); return (0);
if (test == '/' && flags & FNM_PATHNAME) if (test == '/' && (flags & FNM_PATHNAME))
break; break;
++string; ++string;
} }
@@ -116,11 +129,23 @@ fnmatch(pattern, string, flags)
case '[': case '[':
if (*string == EOS) if (*string == EOS)
return (FNM_NOMATCH); return (FNM_NOMATCH);
if (*string == '/' && flags & FNM_PATHNAME) if (*string == '/' && (flags & FNM_PATHNAME))
return (FNM_NOMATCH); return (FNM_NOMATCH);
if ((pattern = if (*string == '.' && (flags & FNM_PERIOD) &&
rangematch(pattern, *string, flags)) == NULL) (string == stringstart ||
((flags & FNM_PATHNAME) && *(string - 1) == '/')))
return (FNM_NOMATCH); return (FNM_NOMATCH);
switch (rangematch(pattern, *string, flags, &newp)) {
case RANGE_ERROR:
/* not a good range, treat as normal text */
goto normal;
case RANGE_MATCH:
pattern = newp;
break;
case RANGE_NOMATCH:
return (FNM_NOMATCH);
}
++string; ++string;
break; break;
case '\\': case '\\':
@@ -132,17 +157,23 @@ fnmatch(pattern, string, flags)
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
if (c != *string++) normal:
if (c != *string && !((flags & FNM_CASEFOLD) &&
(tolower((unsigned char)c) ==
tolower((unsigned char)*string))))
return (FNM_NOMATCH); return (FNM_NOMATCH);
++string;
break; break;
} }
/* NOTREACHED */ /* NOTREACHED */
} }
static const char * static int
rangematch(pattern, test, flags) rangematch(pattern, test, flags, newp)
const char *pattern; const char *pattern;
int test, flags; char test;
int flags;
char **newp;
{ {
int negate, ok; int negate, ok;
char c, c2; char c, c2;
@@ -157,22 +188,40 @@ rangematch(pattern, test, flags)
if ((negate = (*pattern == '!' || *pattern == '^'))) if ((negate = (*pattern == '!' || *pattern == '^')))
++pattern; ++pattern;
for (ok = 0; (c = *pattern++) != ']';) { if (flags & FNM_CASEFOLD)
test = tolower((unsigned char)test);
/*
* A right bracket shall lose its special meaning and represent
* itself in a bracket expression if it occurs first in the list.
* -- POSIX.2 2.8.3.2
*/
ok = 0;
c = *pattern++;
do {
if (c == '\\' && !(flags & FNM_NOESCAPE)) if (c == '\\' && !(flags & FNM_NOESCAPE))
c = *pattern++; c = *pattern++;
if (c == EOS) if (c == EOS)
return (NULL); return (RANGE_ERROR);
if (c == '/' && (flags & FNM_PATHNAME))
return (RANGE_NOMATCH);
if ((flags & FNM_CASEFOLD))
c = tolower((unsigned char)c);
if (*pattern == '-' if (*pattern == '-'
&& (c2 = *(pattern+1)) != EOS && c2 != ']') { && (c2 = *(pattern+1)) != EOS && c2 != ']') {
pattern += 2; pattern += 2;
if (c2 == '\\' && !(flags & FNM_NOESCAPE)) if (c2 == '\\' && !(flags & FNM_NOESCAPE))
c2 = *pattern++; c2 = *pattern++;
if (c2 == EOS) if (c2 == EOS)
return (NULL); return (RANGE_ERROR);
if (flags & FNM_CASEFOLD)
c2 = tolower((unsigned char)c2);
if (c <= test && test <= c2) if (c <= test && test <= c2)
ok = 1; ok = 1;
} else if (c == test) } else if (c == test)
ok = 1; ok = 1;
} } while ((c = *pattern++) != ']');
return (ok == negate ? NULL : pattern);
*newp = (char *)pattern;
return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
} }