fnmatch.c revision 19132
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Guido van Rossum.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
381573Srgrimesstatic char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes/*
421573Srgrimes * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
431573Srgrimes * Compares a filename or pathname to a pattern.
441573Srgrimes */
451573Srgrimes
4619059Swosch#include <ctype.h>
471573Srgrimes#include <fnmatch.h>
4817552Sache#include <locale.h>
491573Srgrimes#include <string.h>
5019059Swosch#include <stdio.h>
511573Srgrimes
521573Srgrimes#define	EOS	'\0'
531573Srgrimes
5419132Sachestatic const char *rangematch __P((const char *, char, int));
551573Srgrimes
561573Srgrimesint
571573Srgrimesfnmatch(pattern, string, flags)
581573Srgrimes	const char *pattern, *string;
591573Srgrimes	int flags;
601573Srgrimes{
611573Srgrimes	const char *stringstart;
621573Srgrimes	char c, test;
631573Srgrimes
641573Srgrimes	for (stringstart = string;;)
651573Srgrimes		switch (c = *pattern++) {
661573Srgrimes		case EOS:
6719132Sache			if ((flags & FNM_LEADING_DIR) && *string == '/')
6819132Sache				return (0);
691573Srgrimes			return (*string == EOS ? 0 : FNM_NOMATCH);
701573Srgrimes		case '?':
711573Srgrimes			if (*string == EOS)
721573Srgrimes				return (FNM_NOMATCH);
731573Srgrimes			if (*string == '/' && (flags & FNM_PATHNAME))
741573Srgrimes				return (FNM_NOMATCH);
751573Srgrimes			if (*string == '.' && (flags & FNM_PERIOD) &&
761573Srgrimes			    (string == stringstart ||
771573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
781573Srgrimes				return (FNM_NOMATCH);
791573Srgrimes			++string;
801573Srgrimes			break;
811573Srgrimes		case '*':
821573Srgrimes			c = *pattern;
831573Srgrimes			/* Collapse multiple stars. */
841573Srgrimes			while (c == '*')
851573Srgrimes				c = *++pattern;
861573Srgrimes
871573Srgrimes			if (*string == '.' && (flags & FNM_PERIOD) &&
881573Srgrimes			    (string == stringstart ||
891573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
901573Srgrimes				return (FNM_NOMATCH);
911573Srgrimes
921573Srgrimes			/* Optimize for pattern with * at end or before /. */
931573Srgrimes			if (c == EOS)
941573Srgrimes				if (flags & FNM_PATHNAME)
951573Srgrimes					return (strchr(string, '/') == NULL ?
961573Srgrimes					    0 : FNM_NOMATCH);
971573Srgrimes				else
981573Srgrimes					return (0);
991573Srgrimes			else if (c == '/' && flags & FNM_PATHNAME) {
1001573Srgrimes				if ((string = strchr(string, '/')) == NULL)
1011573Srgrimes					return (FNM_NOMATCH);
1021573Srgrimes				break;
1031573Srgrimes			}
1041573Srgrimes
1051573Srgrimes			/* General case, use recursion. */
1061573Srgrimes			while ((test = *string) != EOS) {
1071573Srgrimes				if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
1081573Srgrimes					return (0);
1091573Srgrimes				if (test == '/' && flags & FNM_PATHNAME)
1101573Srgrimes					break;
1111573Srgrimes				++string;
1121573Srgrimes			}
1131573Srgrimes			return (FNM_NOMATCH);
1141573Srgrimes		case '[':
1151573Srgrimes			if (*string == EOS)
1161573Srgrimes				return (FNM_NOMATCH);
1171573Srgrimes			if (*string == '/' && flags & FNM_PATHNAME)
1181573Srgrimes				return (FNM_NOMATCH);
1191573Srgrimes			if ((pattern =
1201573Srgrimes			    rangematch(pattern, *string, flags)) == NULL)
1211573Srgrimes				return (FNM_NOMATCH);
1221573Srgrimes			++string;
1231573Srgrimes			break;
1241573Srgrimes		case '\\':
1251573Srgrimes			if (!(flags & FNM_NOESCAPE)) {
1261573Srgrimes				if ((c = *pattern++) == EOS) {
1271573Srgrimes					c = '\\';
1281573Srgrimes					--pattern;
1291573Srgrimes				}
1301573Srgrimes			}
1311573Srgrimes			/* FALLTHROUGH */
1321573Srgrimes		default:
13319059Swosch			if (c == *string)
13419059Swosch				;
13519132Sache			else if ((flags & FNM_CASEFOLD) &&
13619132Sache				 (tolower((unsigned char)c) ==
13719132Sache				  tolower((unsigned char)*string)))
13819059Swosch				;
13919059Swosch			else
1401573Srgrimes				return (FNM_NOMATCH);
14119059Swosch			string++;
1421573Srgrimes			break;
1431573Srgrimes		}
1441573Srgrimes	/* NOTREACHED */
1451573Srgrimes}
1461573Srgrimes
1471573Srgrimesstatic const char *
1481573Srgrimesrangematch(pattern, test, flags)
1491573Srgrimes	const char *pattern;
15019132Sache	char test;
15119132Sache	int flags;
1521573Srgrimes{
1531573Srgrimes	int negate, ok;
1541573Srgrimes	char c, c2;
1551573Srgrimes
1561573Srgrimes	/*
1571573Srgrimes	 * A bracket expression starting with an unquoted circumflex
1581573Srgrimes	 * character produces unspecified results (IEEE 1003.2-1992,
1591573Srgrimes	 * 3.13.2).  This implementation treats it like '!', for
1601573Srgrimes	 * consistency with the regular expression syntax.
1611573Srgrimes	 * J.T. Conklin (conklin@ngai.kaleida.com)
1621573Srgrimes	 */
16317141Sjkh	if ( (negate = (*pattern == '!' || *pattern == '^')) )
1641573Srgrimes		++pattern;
1658870Srgrimes
16619132Sache	if (flags & FNM_CASEFOLD)
16719132Sache		test = tolower((unsigned char)test);
16819059Swosch
1691573Srgrimes	for (ok = 0; (c = *pattern++) != ']';) {
1701573Srgrimes		if (c == '\\' && !(flags & FNM_NOESCAPE))
1711573Srgrimes			c = *pattern++;
1721573Srgrimes		if (c == EOS)
1731573Srgrimes			return (NULL);
17419059Swosch
17519132Sache		if (flags & FNM_CASEFOLD)
17619132Sache			c = tolower((unsigned char)c);
17719059Swosch
1788870Srgrimes		if (*pattern == '-'
1791573Srgrimes		    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
1801573Srgrimes			pattern += 2;
1811573Srgrimes			if (c2 == '\\' && !(flags & FNM_NOESCAPE))
1821573Srgrimes				c2 = *pattern++;
1831573Srgrimes			if (c2 == EOS)
1841573Srgrimes				return (NULL);
18519059Swosch
18619132Sache			if (flags & FNM_CASEFOLD)
18719132Sache				c2 = tolower((unsigned char)c2);
18819059Swosch
18917552Sache			if (   collate_range_cmp(c, test) <= 0
19017552Sache			    && collate_range_cmp(test, c2) <= 0
19117533Sache			   )
1921573Srgrimes				ok = 1;
1931573Srgrimes		} else if (c == test)
1941573Srgrimes			ok = 1;
1951573Srgrimes	}
1961573Srgrimes	return (ok == negate ? NULL : pattern);
1971573Srgrimes}
198