add flag FNM_ICASE for case insensitve search

Reviewed by: ache
This commit is contained in:
Wolfram Schneider 1996-10-20 15:15:59 +00:00
parent 8e774bbf9d
commit 95e4966c47
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=19059
4 changed files with 46 additions and 2 deletions

View File

@ -41,6 +41,7 @@
#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
#define FNM_PERIOD 0x04 /* Period must be matched by period. */ #define FNM_PERIOD 0x04 /* Period must be matched by period. */
#define FNM_ICASE 0x08 /* case insensitive search */
#include <sys/cdefs.h> #include <sys/cdefs.h>

View File

@ -99,6 +99,11 @@ Additionally, if
.Dv FNM_PATHNAME .Dv FNM_PATHNAME
is set, is set,
a period is ``leading'' if it immediately follows a slash. a period is ``leading'' if it immediately follows a slash.
.It Dv FNM_ICASE
Ignore case distinctions in both the
.Fa pattern
and the
.Fa string .
.El .El
.Sh RETURN VALUES .Sh RETURN VALUES
The The

View File

@ -43,9 +43,11 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
* Compares a filename or pathname to a pattern. * Compares a filename or pathname to a pattern.
*/ */
#include <ctype.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#define EOS '\0' #define EOS '\0'
@ -126,8 +128,14 @@ fnmatch(pattern, string, flags)
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
if (c != *string++) if (c == *string)
;
else if ((flags & FNM_ICASE) &&
(tolower(c) == tolower(*string)))
;
else
return (FNM_NOMATCH); return (FNM_NOMATCH);
string++;
break; break;
} }
/* NOTREACHED */ /* NOTREACHED */
@ -151,11 +159,18 @@ rangematch(pattern, test, flags)
if ( (negate = (*pattern == '!' || *pattern == '^')) ) if ( (negate = (*pattern == '!' || *pattern == '^')) )
++pattern; ++pattern;
if (flags & FNM_ICASE)
test = tolower(test);
for (ok = 0; (c = *pattern++) != ']';) { for (ok = 0; (c = *pattern++) != ']';) {
if (c == '\\' && !(flags & FNM_NOESCAPE)) if (c == '\\' && !(flags & FNM_NOESCAPE))
c = *pattern++; c = *pattern++;
if (c == EOS) if (c == EOS)
return (NULL); return (NULL);
if (flags & FNM_ICASE)
c = tolower(c);
if (*pattern == '-' if (*pattern == '-'
&& (c2 = *(pattern+1)) != EOS && c2 != ']') { && (c2 = *(pattern+1)) != EOS && c2 != ']') {
pattern += 2; pattern += 2;
@ -163,6 +178,10 @@ rangematch(pattern, test, flags)
c2 = *pattern++; c2 = *pattern++;
if (c2 == EOS) if (c2 == EOS)
return (NULL); return (NULL);
if (flags & FNM_ICASE)
c2 = tolower(c2);
if ( collate_range_cmp(c, test) <= 0 if ( collate_range_cmp(c, test) <= 0
&& collate_range_cmp(test, c2) <= 0 && collate_range_cmp(test, c2) <= 0
) )

View File

@ -43,9 +43,11 @@ static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
* Compares a filename or pathname to a pattern. * Compares a filename or pathname to a pattern.
*/ */
#include <ctype.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#define EOS '\0' #define EOS '\0'
@ -126,8 +128,14 @@ fnmatch(pattern, string, flags)
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
if (c != *string++) if (c == *string)
;
else if ((flags & FNM_ICASE) &&
(tolower(c) == tolower(*string)))
;
else
return (FNM_NOMATCH); return (FNM_NOMATCH);
string++;
break; break;
} }
/* NOTREACHED */ /* NOTREACHED */
@ -151,11 +159,18 @@ rangematch(pattern, test, flags)
if ( (negate = (*pattern == '!' || *pattern == '^')) ) if ( (negate = (*pattern == '!' || *pattern == '^')) )
++pattern; ++pattern;
if (flags & FNM_ICASE)
test = tolower(test);
for (ok = 0; (c = *pattern++) != ']';) { for (ok = 0; (c = *pattern++) != ']';) {
if (c == '\\' && !(flags & FNM_NOESCAPE)) if (c == '\\' && !(flags & FNM_NOESCAPE))
c = *pattern++; c = *pattern++;
if (c == EOS) if (c == EOS)
return (NULL); return (NULL);
if (flags & FNM_ICASE)
c = tolower(c);
if (*pattern == '-' if (*pattern == '-'
&& (c2 = *(pattern+1)) != EOS && c2 != ']') { && (c2 = *(pattern+1)) != EOS && c2 != ']') {
pattern += 2; pattern += 2;
@ -163,6 +178,10 @@ rangematch(pattern, test, flags)
c2 = *pattern++; c2 = *pattern++;
if (c2 == EOS) if (c2 == EOS)
return (NULL); return (NULL);
if (flags & FNM_ICASE)
c2 = tolower(c2);
if ( collate_range_cmp(c, test) <= 0 if ( collate_range_cmp(c, test) <= 0
&& collate_range_cmp(test, c2) <= 0 && collate_range_cmp(test, c2) <= 0
) )