From f52b16c60da4b4c350471d7fc68089d796cc082f Mon Sep 17 00:00:00 2001 From: Yuri Pankov Date: Mon, 14 Aug 2017 21:30:42 +0300 Subject: [PATCH] 8578 sync fnmatch(3C) with upstream Reviewed by: Toomas Soome Reviewed by: C Fraire Approved by: Dan McDonald --- usr/src/head/fnmatch.h | 5 + usr/src/lib/libc/port/locale/fnmatch.c | 24 +- usr/src/man/man3c/fnmatch.3c | 405 ++++++++++++++++---------- usr/src/pkg/manifests/system-test-libctest.mf | 2 + usr/src/test/libc-tests/runfiles/default.run | 2 + usr/src/test/libc-tests/tests/Makefile | 1 + usr/src/test/libc-tests/tests/fnmatch.c | 324 +++++++++++++++++++++ 7 files changed, 602 insertions(+), 161 deletions(-) rewrite usr/src/man/man3c/fnmatch.3c (91%) create mode 100644 usr/src/test/libc-tests/tests/fnmatch.c diff --git a/usr/src/head/fnmatch.h b/usr/src/head/fnmatch.h index bfb4b925c0..9602c18bbb 100644 --- a/usr/src/head/fnmatch.h +++ b/usr/src/head/fnmatch.h @@ -19,6 +19,7 @@ * * CDDL HEADER END */ + /* * Copyright 2014 Garrett D'Amore * Copyright (c) 1994 by Sun Microsystems, Inc. @@ -37,6 +38,10 @@ extern "C" { #define FNM_PERIOD 0x04 /* Leading period in string must be exactly */ /* matched by period in pattern */ #define FNM_IGNORECASE 0x08 /* Ignore case when making comparisons */ +#define FNM_CASEFOLD 0x08 /* Ignore case when making comparisons */ + /* (for FreeBSD/Linux compatibility) */ +#define FNM_LEADING_DIR 0x10 /* Match if pattern matches initial segment */ + /* of str which is followed by a slash */ #define FNM_NOMATCH 1 /* string doesnt match the specified pattern */ #define FNM_ERROR 2 /* error occured */ diff --git a/usr/src/lib/libc/port/locale/fnmatch.c b/usr/src/lib/libc/port/locale/fnmatch.c index cf7b1c2372..2605246ae0 100644 --- a/usr/src/lib/libc/port/locale/fnmatch.c +++ b/usr/src/lib/libc/port/locale/fnmatch.c @@ -13,7 +13,7 @@ * 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. - * 4. Neither the name of the University nor the names of its contributors + * 3. 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. * @@ -107,10 +107,8 @@ fnmatch1(const char *pattern, const char *string, const char *stringstart, } switch (pc) { case EOS: - /* - * Removed FNM_LEADING_DIR, as it is not present - * on Solaris. - */ + if ((flags & FNM_LEADING_DIR) && sc == '/') + return (0); if (sc == EOS) return (0); goto backtrack; @@ -139,7 +137,8 @@ fnmatch1(const char *pattern, const char *string, const char *stringstart, /* Optimize for pattern with * at end or before /. */ 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); @@ -184,10 +183,9 @@ fnmatch1(const char *pattern, const char *string, const char *stringstart, if (!(flags & FNM_NOESCAPE)) { pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, &patmbs, loc); - if (pclen == (size_t)-1 || pclen == (size_t)-2) + if (pclen == 0 || pclen == (size_t)-1 || + pclen == (size_t)-2) return (FNM_NOMATCH); - if (pclen == 0) - pc = '\\'; pattern += pclen; } /* FALLTHROUGH */ @@ -196,7 +194,7 @@ fnmatch1(const char *pattern, const char *string, const char *stringstart, string += sclen; if (pc == sc) break; - else if ((flags & FNM_IGNORECASE) && + else if ((flags & FNM_CASEFOLD) && (towlower_l(pc, loc) == towlower_l(sc, loc))) break; else { @@ -251,7 +249,7 @@ rangematch(const char *pattern, wchar_t test, int flags, char **newp, if ((negate = (*pattern == '!' || *pattern == '^')) != 0) ++pattern; - if (flags & FNM_IGNORECASE) + if (flags & FNM_CASEFOLD) test = towlower_l(test, loc); /* @@ -276,7 +274,7 @@ rangematch(const char *pattern, wchar_t test, int flags, char **newp, return (RANGE_NOMATCH); pattern += pclen; - if (flags & FNM_IGNORECASE) + if (flags & FNM_CASEFOLD) c = towlower_l(c, loc); if (*pattern == '-' && *(pattern + 1) != EOS && @@ -292,7 +290,7 @@ rangematch(const char *pattern, wchar_t test, int flags, char **newp, if (c2 == EOS) return (RANGE_ERROR); - if (flags & FNM_IGNORECASE) + if (flags & FNM_CASEFOLD) c2 = towlower_l(c2, loc); if (loc->collate->lc_is_posix ? diff --git a/usr/src/man/man3c/fnmatch.3c b/usr/src/man/man3c/fnmatch.3c dissimilarity index 91% index 8b93c593b4..c2e82e6b99 100644 --- a/usr/src/man/man3c/fnmatch.3c +++ b/usr/src/man/man3c/fnmatch.3c @@ -1,148 +1,257 @@ -'\" te -.\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved Portions Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved -.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at -.\" http://www.opengroup.org/bookstore/. -.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. -.\" This notice shall appear on any product containing this material. -.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. -.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. -.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] -.TH FNMATCH 3C "Jun 11, 2015" -.SH NAME -fnmatch \- match filename or path name -.SH SYNOPSIS -.LP -.nf -#include - -\fBint\fR \fBfnmatch\fR(\fBconst char *\fR\fIpattern\fR, \fBconst char *\fR\fIstring\fR, \fBint\fR \fIflags\fR); -.fi - -.SH DESCRIPTION -.LP -The \fBfnmatch()\fR function matches patterns as described on the -\fBfnmatch\fR(5) manual page. It checks the \fIstring\fR argument to see if it -matches the \fIpattern\fR argument. -.sp -.LP -The \fIflags\fR argument modifies the interpretation of \fIpattern\fR and -\fIstring\fR. It is the bitwise inclusive \fBOR\fR of zero or more of the -following flags defined in the header <\fBfnmatch.h\fR>. -.sp -.ne 2 -.na -\fB\fBFNM_PATHNAME\fR \fR -.ad -.RS 18n -If set, a slash (\fB/\fR) character in \fIstring\fR will be explicitly matched -by a slash in \fIpattern\fR; it will not be matched by either the asterisk -(\fB*\fR) or question-mark (\fB?\fR) special characters, nor by a bracket -(\fB[\|]\fR) expression. -.sp -If not set, the slash character is treated as an ordinary character. -.RE - -.sp -.ne 2 -.na -\fB\fBFNM_IGNORECASE\fR\fR -.ad -.RS 18n -If set, the \fIstring\fR will be transliterated to lower case before doing the -actual match. This transliteration is done using \fBtowlower_l\fR(3C), using -the locale of the current thread. If no locale is set, then the global locale -is used instead. -.sp -If not set, the match will use \fIstring\fR with no changes, making the match -case-sensitive. -.RE - -.sp -.ne 2 -.na -\fB\fBFNM_NOESCAPE\fR\fR -.ad -.RS 18n -If not set, a backslash character (\fB\e\fR) in \fIpattern\fR followed by any -other character will match that second character in \fIstring\fR. In -particular, "\fB\e\e\fR" will match a backslash in \fIstring\fR. -.sp -If set, a backslash character will be treated as an ordinary character. -.RE - -.sp -.ne 2 -.na -\fB\fBFNM_PERIOD\fR\fR -.ad -.RS 18n -If set, a leading period in \fIstring\fR will match a period in \fIpattern\fR; -where the location of "leading" is indicated by the value of -\fBFNM_PATHNAME\fR: -.RS +4 -.TP -.ie t \(bu -.el o -If \fBFNM_PATHNAME\fR is set, a period is "leading" if it is the first -character in \fIstring\fR or if it immediately follows a slash. -.RE -.RS +4 -.TP -.ie t \(bu -.el o -If \fBFNM_PATHNAME\fR is not set, a period is "leading" only if it is the first -character of \fIstring\fR. -.RE -.RE - -.sp -.RS 18n -If not set, no special restrictions are placed on matching a period. -.SH RETURN VALUES -.LP -If \fIstring\fR matches the pattern specified by \fIpattern\fR, then -\fBfnmatch()\fR returns \fB0\fR. If there is no match, \fBfnmatch()\fR returns -\fBFNM_NOMATCH\fR, which is defined in the header <\fBfnmatch.h\fR>. If an -error occurs, \fBfnmatch()\fR returns another non-zero value. -.SH USAGE -.LP -The \fBfnmatch()\fR function has two major uses. It could be used by an -application or utility that needs to read a directory and apply a pattern -against each entry. The \fBfind\fR(1) utility is an example of this. It can -also be used by the \fBpax\fR(1) utility to process its \fIpattern\fR operands, -or by applications that need to match strings in a similar manner. -.sp -.LP -The name \fBfnmatch()\fR is intended to imply \fIfilename\fR match, rather than -\fIpathname\fR match. The default action of this function is to match -filenames, rather than path names, since it gives no special significance to -the slash character. With the \fBFNM_PATHNAME\fR flag, \fBfnmatch()\fR does -match path names, but without tilde expansion, parameter expansion, or special -treatment for period at the beginning of a filename. -.sp -.LP -The \fBfnmatch()\fR function can be used safely in multithreaded applications, -as long as \fBsetlocale\fR(3C) is not being called to change the locale. -.SH ATTRIBUTES -.LP -See \fBattributes\fR(5) for descriptions of the following attributes: -.sp - -.sp -.TS -box; -c | c -l | l . -ATTRIBUTE TYPE ATTRIBUTE VALUE -_ -CSI Enabled -_ -Interface Stability Standard -_ -MT-Level MT-Safe with exceptions -.TE - -.SH SEE ALSO -.LP -\fBfind\fR(1), \fBpax\fR(1), \fBglob\fR(3C), \fBsetlocale\fR(3C), -\fBwordexp\fR(3C), \fBattributes\fR(5), \fBfnmatch\fR(5), \fBstandards\fR(5) +.\" +.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission +.\" to reproduce portions of its copyrighted documentation. +.\" +.\" Original documentation from The Open Group can be obtained online at +.\" http://www.opengroup.org/bookstore/. +.\" +.\" The Institute of Electrical and Electronics Engineers and The Open Group, +.\" have given us permission to reprint portions of their documentation. In the +.\" following statement, the phrase "this text" refers to portions of the system +.\" documentation. +.\" +.\" Portions of this text are reprinted and reproduced in electronic form in the +.\" Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for +.\" Information Technology -- Portable Operating System Interface (POSIX), +.\" The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the +.\" Institute of Electrical and Electronics Engineers, Inc and The Open Group. +.\" +.\" In the event of any discrepancy between these versions and the original +.\" IEEE and The Open Group Standard, the original IEEE and The Open Group +.\" Standard is the referee document. +.\" +.\" The original Standard can be obtained online at +.\" http://www.opengroup.org/unix/online.html. +.\" +.\" This notice shall appear on any product containing this material. +.\" +.\" The contents of this file are subject to the terms of the +.\" Common Development and Distribution License (the "License"). +.\" You may not use this file except in compliance with the License. +.\" +.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +.\" or http://www.opensolaris.org/os/licensing. +.\" See the License for the specific language governing permissions +.\" and limitations under the License. +.\" +.\" When distributing Covered Code, include this CDDL HEADER in each +.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. +.\" If applicable, add the following below this CDDL HEADER, with the +.\" fields enclosed by brackets "[]" replaced with your own identifying +.\" information: Portions Copyright [yyyy] [name of copyright owner] +.\" +.\" +.\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved +.\" Portions Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved +.\" Copyright 2017 Nexenta Systems, Inc. +.\" +.Dd August 14, 2017 +.Dt FNMATCH 3C +.Os +.Sh NAME +.Nm fnmatch +.Nd match filename or path name +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fnmatch.h +.Ft int +.Fo fnmatch +.Fa "const char *pattern" +.Fa "const char *string" +.Fa "int flags" +.Fc +.Sh DESCRIPTION +The +.Fn fnmatch +function matches patterns as described on the +.Xr fnmatch 5 +manual page +.Po with the exceptions noted in the +.Sx STANDARDS +.Pc . +It checks the +.Fa string +argument to see if it matches the +.Fa pattern +argument. +.Pp +The +.Fa flags +argument modifies the interpretation of +.Fa pattern +and +.Fa string . +It is the bitwise inclusive OR of zero or more of the following flags defined in +the header +.In fnmatch.h . +.Bl -tag -width "FNM_LEADING_DIR" +.It Dv FNM_PATHNAME +If set, a slash +.Pq Qq / +character in +.Fa string +will be explicitly matched by a slash in +.Fa pattern ; +it will not be matched by either the asterisk +.Pq Qq * +or question-mark +.Pq Qq \&? +special characters, nor by a bracket +.Pq Qq [] +expression. +.sp +If not set, the slash character is treated as an ordinary character. +.It Dv FNM_IGNORECASE +If set, the +.Fa string +will be transliterated to lower case before doing the actual match. +This transliteration is done using +.Xr towlower_l 3C , +using the locale of the current thread. +If no locale is set, then the global locale is used instead. +.Pp +If not set, the match will use +.Fa string +with no changes, making the match case-sensitive. +.Pp +For compatibility with +.Fx +implementation of +.Fn fnmatch , +header +.In fnmatch.h +provides the +.Dv FNM_FOLDCASE +flag having the same meaning. +.It Dv FNM_NOESCAPE +If not set, a backslash character +.Pq Qq \e +in +.Fa pattern +followed by any other character will match that second character in +.Fa string . +In particular, +.Qq \e\e +will match a backslash in +.Fa string . +.Pp +If set, a backslash character will be treated as an ordinary character. +.It Dv FNM_PERIOD +If set, a leading period in +.Fa string +will match a period in +.Fa pattern ; +where the location of +.Qq leading +is indicated by the value of +.Dv FNM_PATHNAME : +.Bl -bullet +.It +If +.Dv FNM_PATHNAME +is set, a period is +.Qq leading +if it is the first character in +.Fa string +or if it immediately follows a slash. +.It +If +.Dv FNM_PATHNAME +is not set, a period is +.Qq leading +only if it is the first character of +.Fa string . +.El +.Pp +If not set, no special restrictions are placed on matching a period. +.It Dv FNM_LEADING_DIR +Match if +.Fa pattern +matches initial segment of +.Fa string +which is followed by a slash. +.El +.Sh RETURN VALUES +If +.Fa string +matches the pattern specified by +.Fa pattern , +then +.Fn fnmatch +returns 0. +If there is no match, +.Fn fnmatch +returns +.Dv FNM_NOMATCH , +which is defined in the header +.In fnmatch.h . +If an error occurs, +.Fn fnmatch +returns another non-zero value. +.Sh USAGE +The +.Fn fnmatch +function has two major uses. +It could be used by an application or utility that needs to read a directory and +apply a pattern against each entry. +The +.Xr find 1 +utility is an example of this. +It can also be used by the +.Xr pax 1 +utility to process its +.Fa pattern +operands, or by applications that need to match strings in a similar manner. +.Pp +The name +.Fn fnmatch +is intended to imply +.Sy filename +match, rather than +.Sy pathname +match. +The default action of this function is to match filenames, rather than path +names, since it gives no special significance to the slash character. +With the +.Dv FNM_PATHNAME +flag, +.Fn fnmatch +does match path names, but without tilde expansion, parameter expansion, or +special treatment for period at the beginning of a filename. +.Sh CODE SET INDEPENDENCE +.Sy Enabled +.Sh INTERFACE STABILITY +.Sy Standard +.Sh MT-LEVEL +.Sy MT-Safe with exceptions +.Pp +The +.Fn fnmatch +function can be used safely in multithreaded applications, as long as +.Xr setlocale 3C +is not being called to change the locale. +.Sh SEE ALSO +.Xr find 1 , +.Xr pax 1 , +.Xr glob 3C , +.Xr setlocale 3C , +.Xr wordexp 3C , +.Xr attributes 5 , +.Xr fnmatch 5 , +.Xr standards 5 +.Sh STANDARDS +The current implementation of the +.Fn fnmatch +function +.Em does not +conform to +.St -p1003.2 . +Collating symbol expressions, equivalence class expressions and +character class expressions are not supported. +.Sh BUGS +The pattern +.Qq * +matches the empty string, even if +.Dv FNM_PATHNAME +is specified. diff --git a/usr/src/pkg/manifests/system-test-libctest.mf b/usr/src/pkg/manifests/system-test-libctest.mf index 2b2f3329f6..bc198602a7 100644 --- a/usr/src/pkg/manifests/system-test-libctest.mf +++ b/usr/src/pkg/manifests/system-test-libctest.mf @@ -86,6 +86,8 @@ file path=opt/libc-tests/tests/endian.32 mode=0555 file path=opt/libc-tests/tests/endian.64 mode=0555 file path=opt/libc-tests/tests/env-7076.32 mode=0555 file path=opt/libc-tests/tests/env-7076.64 mode=0555 +file path=opt/libc-tests/tests/fnmatch.32 mode=0555 +file path=opt/libc-tests/tests/fnmatch.64 mode=0555 file path=opt/libc-tests/tests/fpround_test mode=0555 file path=opt/libc-tests/tests/fpround_test.$(ARCH) mode=0555 file path=opt/libc-tests/tests/fpround_test.$(ARCH64) mode=0555 diff --git a/usr/src/test/libc-tests/runfiles/default.run b/usr/src/test/libc-tests/runfiles/default.run index 93a394782b..17ce2a5b28 100644 --- a/usr/src/test/libc-tests/runfiles/default.run +++ b/usr/src/test/libc-tests/runfiles/default.run @@ -75,6 +75,8 @@ timeout = 600 [/opt/libc-tests/tests/env-7076.64] [/opt/libc-tests/tests/endian.32] [/opt/libc-tests/tests/endian.64] +[/opt/libc-tests/tests/fnmatch.32] +[/opt/libc-tests/tests/fnmatch.64] [/opt/libc-tests/tests/quick_exit] [/opt/libc-tests/tests/psignal] [/opt/libc-tests/tests/printf-6961.64] diff --git a/usr/src/test/libc-tests/tests/Makefile b/usr/src/test/libc-tests/tests/Makefile index 160e9a9383..f9b80255f0 100644 --- a/usr/src/test/libc-tests/tests/Makefile +++ b/usr/src/test/libc-tests/tests/Makefile @@ -37,6 +37,7 @@ PROGS = \ call_once \ endian \ env-7076 \ + fnmatch \ psignal-5097 \ quick_exit_order \ quick_exit_status \ diff --git a/usr/src/test/libc-tests/tests/fnmatch.c b/usr/src/test/libc-tests/tests/fnmatch.c new file mode 100644 index 0000000000..ca42d9e904 --- /dev/null +++ b/usr/src/test/libc-tests/tests/fnmatch.c @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2010 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include + +struct testcase { + const char *pattern; + const char *string; + int flags; + int result; +} testcases[] = { + { "", "", 0, 0 }, + { "a", "a", 0, 0 }, + { "a", "b", 0, FNM_NOMATCH }, + { "a", "A", 0, FNM_NOMATCH }, + { "*", "a", 0, 0 }, + { "*", "aa", 0, 0 }, + { "*a", "a", 0, 0 }, + { "*a", "b", 0, FNM_NOMATCH }, + { "*a*", "b", 0, FNM_NOMATCH }, + { "*a*b*", "ab", 0, 0 }, + { "*a*b*", "qaqbq", 0, 0 }, + { "*a*bb*", "qaqbqbbq", 0, 0 }, + { "*a*bc*", "qaqbqbcq", 0, 0 }, + { "*a*bb*", "qaqbqbb", 0, 0 }, + { "*a*bc*", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbb", 0, 0 }, + { "*a*bc", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbbq", 0, FNM_NOMATCH }, + { "*a*bc", "qaqbqbcq", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaa", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaa", 0, 0 }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaaa", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", ".........", 0, FNM_NOMATCH }, + { ".*.*.*.*.*.*.*.*.*.*", "..........", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", "...........", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "123456789", 0, FNM_NOMATCH }, + { "??????????*", "123456789", 0, FNM_NOMATCH }, + { "*??????????", "123456789", 0, FNM_NOMATCH }, + { "*?*?*?*?*?*?*?*?*?*?*", "1234567890", 0, 0 }, + { "??????????*", "1234567890", 0, 0 }, + { "*??????????", "1234567890", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "12345678901", 0, 0 }, + { "??????????*", "12345678901", 0, 0 }, + { "*??????????", "12345678901", 0, 0 }, + { "[x]", "x", 0, 0 }, + { "[*]", "*", 0, 0 }, + { "[?]", "?", 0, 0 }, + { "[", "[", 0, 0 }, + { "[[]", "[", 0, 0 }, + { "[[]", "x", 0, FNM_NOMATCH }, + { "[*]", "", 0, FNM_NOMATCH }, + { "[*]", "x", 0, FNM_NOMATCH }, + { "[?]", "x", 0, FNM_NOMATCH }, + { "*[*]*", "foo*foo", 0, 0 }, + { "*[*]*", "foo", 0, FNM_NOMATCH }, + { "[0-9]", "0", 0, 0 }, + { "[0-9]", "5", 0, 0 }, + { "[0-9]", "9", 0, 0 }, + { "[0-9]", "/", 0, FNM_NOMATCH }, + { "[0-9]", ":", 0, FNM_NOMATCH }, + { "[0-9]", "*", 0, FNM_NOMATCH }, + { "[!0-9]", "0", 0, FNM_NOMATCH }, + { "[!0-9]", "5", 0, FNM_NOMATCH }, + { "[!0-9]", "9", 0, FNM_NOMATCH }, + { "[!0-9]", "/", 0, 0 }, + { "[!0-9]", ":", 0, 0 }, + { "[!0-9]", "*", 0, 0 }, + { "*[0-9]", "a0", 0, 0 }, + { "*[0-9]", "a5", 0, 0 }, + { "*[0-9]", "a9", 0, 0 }, + { "*[0-9]", "a/", 0, FNM_NOMATCH }, + { "*[0-9]", "a:", 0, FNM_NOMATCH }, + { "*[0-9]", "a*", 0, FNM_NOMATCH }, + { "*[!0-9]", "a0", 0, FNM_NOMATCH }, + { "*[!0-9]", "a5", 0, FNM_NOMATCH }, + { "*[!0-9]", "a9", 0, FNM_NOMATCH }, + { "*[!0-9]", "a/", 0, 0 }, + { "*[!0-9]", "a:", 0, 0 }, + { "*[!0-9]", "a*", 0, 0 }, + { "*[0-9]", "a00", 0, 0 }, + { "*[0-9]", "a55", 0, 0 }, + { "*[0-9]", "a99", 0, 0 }, + { "*[0-9]", "a0a0", 0, 0 }, + { "*[0-9]", "a5a5", 0, 0 }, + { "*[0-9]", "a9a9", 0, 0 }, + { "\\*", "*", 0, 0 }, + { "\\?", "?", 0, 0 }, + { "\\[x]", "[x]", 0, 0 }, + { "\\[", "[", 0, 0 }, + { "\\\\", "\\", 0, 0 }, + { "*\\**", "foo*foo", 0, 0 }, + { "*\\**", "foo", 0, FNM_NOMATCH }, + { "*\\\\*", "foo\\foo", 0, 0 }, + { "*\\\\*", "foo", 0, FNM_NOMATCH }, + { "\\(", "(", 0, 0 }, + { "\\a", "a", 0, 0 }, + { "\\*", "a", 0, FNM_NOMATCH }, + { "\\?", "a", 0, FNM_NOMATCH }, + { "\\*", "\\*", 0, FNM_NOMATCH }, + { "\\?", "\\?", 0, FNM_NOMATCH }, + { "\\[x]", "\\[x]", 0, FNM_NOMATCH }, + { "\\[x]", "\\x", 0, FNM_NOMATCH }, + { "\\[", "\\[", 0, FNM_NOMATCH }, + { "\\(", "\\(", 0, FNM_NOMATCH }, + { "\\a", "\\a", 0, FNM_NOMATCH }, + { "\\", "\\", 0, FNM_NOMATCH }, + { "\\", "", 0, FNM_NOMATCH }, + { "\\*", "\\*", FNM_NOESCAPE, 0 }, + { "\\?", "\\?", FNM_NOESCAPE, 0 }, + { "\\", "\\", FNM_NOESCAPE, 0 }, + { "\\\\", "\\", FNM_NOESCAPE, FNM_NOMATCH }, + { "\\\\", "\\\\", FNM_NOESCAPE, 0 }, + { "*\\*", "foo\\foo", FNM_NOESCAPE, 0 }, + { "*\\*", "foo", FNM_NOESCAPE, FNM_NOMATCH }, + { "*", ".", FNM_PERIOD, FNM_NOMATCH }, + { "?", ".", FNM_PERIOD, FNM_NOMATCH }, + { ".*", ".", 0, 0 }, + { ".*", "..", 0, 0 }, + { ".*", ".a", 0, 0 }, + { "[0-9]", ".", FNM_PERIOD, FNM_NOMATCH }, + { "a*", "a.", 0, 0 }, + { "a/a", "a/a", FNM_PATHNAME, 0 }, + { "a/*", "a/a", FNM_PATHNAME, 0 }, + { "*/a", "a/a", FNM_PATHNAME, 0 }, + { "*/*", "a/a", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/x", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/.x", FNM_PATHNAME, 0 }, + { "*", "a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "*/*", "a/a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "b/*", "b/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b*/*", "a/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "b*/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "a", "A", FNM_CASEFOLD, 0 }, + { "A", "a", FNM_CASEFOLD, 0 }, + { "[a]", "A", FNM_CASEFOLD, 0 }, + { "[A]", "a", FNM_CASEFOLD, 0 }, + { "a", "b", FNM_CASEFOLD, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*a", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, + FNM_NOMATCH }, + { "*a", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, + FNM_NOMATCH }, + { "a*b/*", "abbb/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, +}; + +static const char * +flags_to_string(int flags) +{ + static const int flagvalues[] = { FNM_NOESCAPE, FNM_PATHNAME, + FNM_PERIOD, FNM_LEADING_DIR, FNM_CASEFOLD, 0 }; + static const char flagnames[] = "FNM_NOESCAPE\0FNM_PATHNAME\0" + "FNM_PERIOD\0FNM_LEADING_DIR\0FNM_CASEFOLD\0"; + static char result[sizeof (flagnames) + 3 * sizeof (int) + 2]; + char *p; + size_t i, len; + const char *fp; + + p = result; + fp = flagnames; + for (i = 0; flagvalues[i] != 0; i++) { + len = strlen(fp); + if (flags & flagvalues[i]) { + if (p != result) + *p++ = '|'; + memcpy(p, fp, len); + p += len; + flags &= ~flagvalues[i]; + } + fp += len + 1; + } + if (p == result) + memcpy(p, "0", 2); + else if (flags != 0) + sprintf(p, "%d", flags); + else + *p = '\0'; + return (result); +} + +int +main(void) +{ + size_t i; + int flags, result; + int ret = 0; + struct testcase *t; + + for (i = 0; i < sizeof (testcases) / sizeof (struct testcase); i++) { + t = &testcases[i]; + flags = t->flags; + do { + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + if (strchr(t->pattern, '\\') == NULL && + !(flags & FNM_NOESCAPE)) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') != NULL && + strchr(t->string, '\\') == NULL && + t->result == FNM_NOMATCH && + !(flags & (FNM_NOESCAPE | FNM_LEADING_DIR))) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((t->string[0] != '.' || t->pattern[0] == '.' || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || + t->result == FNM_NOMATCH) && + !(flags & FNM_PATHNAME)) { + flags |= FNM_PATHNAME; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strstr(t->string, "/.") == NULL) || + t->result == FNM_NOMATCH) && + flags & FNM_PATHNAME && !(flags & FNM_PERIOD)) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strchr(t->string, '/') == NULL) || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PATHNAME | FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || + t->result == 0) && + !(flags & FNM_LEADING_DIR)) { + flags |= FNM_LEADING_DIR; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (t->result == 0 && !(flags & FNM_CASEFOLD)) { + flags |= FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') == NULL && + t->result == 0 && + !(flags & (FNM_NOESCAPE | FNM_CASEFOLD))) { + flags |= FNM_NOESCAPE | FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + } while (0); + + if (result != t->result) { + printf("fnmatch(\"%s\", \"%s\", %s) != %d (was %d)\n", + t->pattern, t->string, flags_to_string(flags), + t->result, result); + ret = 1; + } + } + + return (ret); +} -- 2.11.4.GIT