From 4fc832c6c28ab01acbd803f5fd75a527c22d569c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 23 Mar 2000 15:11:50 +0000 Subject: [PATCH] Pull in newer fnmatch(3) that supports FNM_CASEFOLD Check for FNM_CASEFOLD in configure --- aclocal.m4 | 4 +- configure | 6 +-- fnmatch.3 | 153 ++++++++++++++++++++++++++++++++++------------------- fnmatch.c | 91 +++++++++++++++++++++++-------- 4 files changed, 174 insertions(+), 80 deletions(-) diff --git a/aclocal.m4 b/aclocal.m4 index 5339f0b99..a04fda5c4 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -233,11 +233,11 @@ dnl dnl check for working fnmatch(3) dnl 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, [rm -f conftestdata; > conftestdata AC_TRY_RUN([#include -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=no) rm -f core core.* *.core])dnl diff --git a/configure b/configure index 3c800ae99..bd4bc8ecd 100755 --- a/configure +++ b/configure @@ -6391,8 +6391,8 @@ else LIBOBJS="$LIBOBJS utime.o" fi -echo $ac_n "checking for working fnmatch""... $ac_c" 1>&6 -echo "configure:6396: checking for working fnmatch" >&5 +echo $ac_n "checking for working fnmatch with FNM_CASEFOLD""... $ac_c" 1>&6 +echo "configure:6396: checking for working fnmatch with FNM_CASEFOLD" >&5 if eval "test \"`echo '$''{'sudo_cv_func_fnmatch'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -6404,7 +6404,7 @@ else #line 6405 "configure" #include "confdefs.h" #include -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 if { (eval echo configure:6411: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null diff --git a/fnmatch.3 b/fnmatch.3 index 2843b2189..d4efe72e1 100644 --- a/fnmatch.3 +++ b/fnmatch.3 @@ -1,3 +1,5 @@ +.\" $OpenBSD: fnmatch.3,v 1.7 1999/06/05 04:47:41 aaron Exp $ +.\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -11,7 +13,11 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" 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 .\" without specific prior written permission. .\" @@ -29,75 +35,114 @@ .\" .\" @(#)fnmatch.3 8.3 (Berkeley) 4/28/95 .\" -.TH FNMATCH 3 "April 28, 1995" -.UC 7 -.SH NAME -fnmatch \- match filename or pathname -.SH SYNOPSIS -.nf -#include - -int fnmatch(const char *pattern, const char *string, int flags); -.fi -.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 +.Dd April 28, 1995 +.Dt FNMATCH 3 +.Os +.Sh NAME +.Nm fnmatch +.Nd match filename or pathname using shell globbing rules +.Sh SYNOPSIS +.Fd #include +.Ft int +.Fn fnmatch "const char *pattern" "const char *string" "int flags" +.Sh DESCRIPTION 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 -.I pattern +.Fa pattern and -.I string. +.Fa string . The value of -.I flags -is the bitwise inclusive OR of any of the following +.Fa flags +is the bitwise inclusive +.Tn OR +of any of the following constants, which are defined in the include file -.IR fnmatch.h . -.TP -FNM_NOESCAPE -Normally, every occurrence of a backslash (`\e') followed by a character in -.I pattern +.Aq Pa fnmatch.h . +.Bl -tag -width FNM_PATHNAME +.It Dv FNM_NOESCAPE +Normally, every occurrence of a backslash +.Pq Sq \e +followed by a character in +.Fa pattern is replaced by that character. This is done to negate any special meaning for the character. If the -.I FNM_NOESCAPE +.Dv FNM_NOESCAPE flag is set, a backslash character is treated as an ordinary character. -.TP -FNM_PATHNAME +.It Dv FNM_PATHNAME Slash characters in -.I string +.Fa string must be explicitly matched by slashes in -.IR pattern . +.Fa pattern . If this flag is not set, then slashes are treated as regular characters. -.TP -FNM_PERIOD -Leading periods in strings match periods in patterns. -The definition of ``leading'' is related to the specification of -.I FNM_PATHNAME. -A period is always ``leading'' if it is the first character in -.I string . +.It Dv FNM_PERIOD +Leading periods in +.Fa string +must be explicitly matched by periods in +.Fa pattern . +If this flag is not set, then leading periods are treated as regular +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 -.I FNM_PATHNAME +.Dv FNM_PATHNAME is set, -a period is ``leading'' if it immediately follows a slash. -.SH RETURN VALUES +a period is leading +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 -.I fnmatch +.Fn fnmatch function returns zero if -.I string +.Fa string matches the pattern specified by -.I pattern , +.Fa pattern , otherwise, it returns the value -.I FNM_NOMATCH . -.SH SEE ALSO -sh(1), glob(3), regex(3), regexp(3). -.SH BUGS -The pattern ``*'' matches the empty string, even if -.I FNM_PATHNAME +.Dv FNM_NOMATCH . +.Sh SEE ALSO +.Xr sh 1 , +.Xr glob 3 , +.Xr regex 3 +.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. diff --git a/fnmatch.c b/fnmatch.c index 934543008..1238f1bad 100644 --- a/fnmatch.c +++ b/fnmatch.c @@ -13,7 +13,11 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * 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 * without specific prior written permission. * @@ -31,7 +35,7 @@ */ #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 */ /* @@ -41,6 +45,7 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; #include "config.h" +#include #include #ifdef HAVE_STRING_H #include @@ -55,7 +60,11 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; #undef EOS #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 fnmatch(pattern, string, flags) @@ -63,11 +72,14 @@ fnmatch(pattern, string, flags) int flags; { const char *stringstart; + char *newp; char c, test; for (stringstart = string;;) switch (c = *pattern++) { case EOS: + if ((flags & FNM_LEADING_DIR) && *string == '/') + return (0); return (*string == EOS ? 0 : FNM_NOMATCH); case '?': if (*string == EOS) @@ -92,13 +104,14 @@ fnmatch(pattern, string, flags) return (FNM_NOMATCH); /* Optimize for pattern with * at end or before /. */ - if (c == EOS) + if (c == EOS) { if (flags & FNM_PATHNAME) - return (strchr(string, '/') == NULL ? + return ((flags & FNM_LEADING_DIR) || + strchr(string, '/') == NULL ? 0 : FNM_NOMATCH); else return (0); - else if (c == '/' && flags & FNM_PATHNAME) { + } else if (c == '/' && (flags & FNM_PATHNAME)) { if ((string = strchr(string, '/')) == NULL) return (FNM_NOMATCH); break; @@ -108,7 +121,7 @@ fnmatch(pattern, string, flags) while ((test = *string) != EOS) { if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) return (0); - if (test == '/' && flags & FNM_PATHNAME) + if (test == '/' && (flags & FNM_PATHNAME)) break; ++string; } @@ -116,11 +129,23 @@ fnmatch(pattern, string, flags) case '[': if (*string == EOS) return (FNM_NOMATCH); - if (*string == '/' && flags & FNM_PATHNAME) + if (*string == '/' && (flags & FNM_PATHNAME)) return (FNM_NOMATCH); - if ((pattern = - rangematch(pattern, *string, flags)) == NULL) + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 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; break; case '\\': @@ -132,17 +157,23 @@ fnmatch(pattern, string, flags) } /* FALLTHROUGH */ default: - if (c != *string++) + normal: + if (c != *string && !((flags & FNM_CASEFOLD) && + (tolower((unsigned char)c) == + tolower((unsigned char)*string)))) return (FNM_NOMATCH); + ++string; break; } /* NOTREACHED */ } -static const char * -rangematch(pattern, test, flags) +static int +rangematch(pattern, test, flags, newp) const char *pattern; - int test, flags; + char test; + int flags; + char **newp; { int negate, ok; char c, c2; @@ -156,23 +187,41 @@ rangematch(pattern, test, flags) */ if ((negate = (*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)) c = *pattern++; if (c == EOS) - return (NULL); - if (*pattern == '-' + return (RANGE_ERROR); + if (c == '/' && (flags & FNM_PATHNAME)) + return (RANGE_NOMATCH); + if ((flags & FNM_CASEFOLD)) + c = tolower((unsigned char)c); + if (*pattern == '-' && (c2 = *(pattern+1)) != EOS && c2 != ']') { pattern += 2; if (c2 == '\\' && !(flags & FNM_NOESCAPE)) c2 = *pattern++; if (c2 == EOS) - return (NULL); + return (RANGE_ERROR); + if (flags & FNM_CASEFOLD) + c2 = tolower((unsigned char)c2); if (c <= test && test <= c2) ok = 1; } else if (c == test) ok = 1; - } - return (ok == negate ? NULL : pattern); + } while ((c = *pattern++) != ']'); + + *newp = (char *)pattern; + return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); }