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 <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=no)
rm -f core core.* *.core])dnl
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
#line 6405 "configure"
#include "confdefs.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
if { (eval echo configure:6411: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+.\" $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.
.\"
.\" 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.
.\"
.\"
.\" @(#)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 <fnmatch.h>
-
-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.
+.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 <fnmatch.h>
+.Ft int
+.Fn fnmatch "const char *pattern" "const char *string" "int flags"
+.Sh DESCRIPTION
+The
+.Fn fnmatch
+function
+matches patterns according to the globbing rules used by the shell.
It checks the string specified by the
-.I string
+.Fa string
argument to see if it matches the pattern specified by the
-.I pattern
+.Fa pattern
argument.
-.PP
+.Pp
The
-.I flags
+.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.
* 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.
*
*/
#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 */
/*
#include "config.h"
+#include <ctype.h>
#include <stdio.h>
#ifdef HAVE_STRING_H
#include <string.h>
#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)
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)
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;
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;
}
case '[':
if (*string == EOS)
return (FNM_NOMATCH);
- if (*string == '/' && flags & FNM_PATHNAME)
+ if (*string == '/' && (flags & FNM_PATHNAME))
+ return (FNM_NOMATCH);
+ if (*string == '.' && (flags & FNM_PERIOD) &&
+ (string == stringstart ||
+ ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
return (FNM_NOMATCH);
- if ((pattern =
- rangematch(pattern, *string, flags)) == NULL)
+
+ 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 '\\':
}
/* 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;
*/
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);
}