Home | History | Annotate | Line # | Download | only in gen
fnmatch.c revision 1.13
      1 /*	$NetBSD: fnmatch.c,v 1.13 1997/07/21 14:06:58 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Guido van Rossum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
     43 #else
     44 __RCSID("$NetBSD: fnmatch.c,v 1.13 1997/07/21 14:06:58 jtc Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 /*
     49  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
     50  * Compares a filename or pathname to a pattern.
     51  */
     52 
     53 #include "namespace.h"
     54 #include <fnmatch.h>
     55 #include <string.h>
     56 
     57 #ifdef __weak_alias
     58 __weak_alias(fnmatch,_fnmatch);
     59 #endif
     60 
     61 #define	EOS	'\0'
     62 
     63 static const char *rangematch __P((const char *, int, int));
     64 
     65 int
     66 fnmatch(pattern, string, flags)
     67 	const char *pattern, *string;
     68 	int flags;
     69 {
     70 	const char *stringstart;
     71 	char c, test;
     72 
     73 	for (stringstart = string;;)
     74 		switch (c = *pattern++) {
     75 		case EOS:
     76 			return (*string == EOS ? 0 : FNM_NOMATCH);
     77 		case '?':
     78 			if (*string == EOS)
     79 				return (FNM_NOMATCH);
     80 			if (*string == '/' && (flags & FNM_PATHNAME))
     81 				return (FNM_NOMATCH);
     82 			if (*string == '.' && (flags & FNM_PERIOD) &&
     83 			    (string == stringstart ||
     84 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
     85 				return (FNM_NOMATCH);
     86 			++string;
     87 			break;
     88 		case '*':
     89 			c = *pattern;
     90 			/* Collapse multiple stars. */
     91 			while (c == '*')
     92 				c = *++pattern;
     93 
     94 			if (*string == '.' && (flags & FNM_PERIOD) &&
     95 			    (string == stringstart ||
     96 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
     97 				return (FNM_NOMATCH);
     98 
     99 			/* Optimize for pattern with * at end or before /. */
    100 			if (c == EOS)
    101 				if (flags & FNM_PATHNAME)
    102 					return (strchr(string, '/') == NULL ?
    103 					    0 : FNM_NOMATCH);
    104 				else
    105 					return (0);
    106 			else if (c == '/' && flags & FNM_PATHNAME) {
    107 				if ((string = strchr(string, '/')) == NULL)
    108 					return (FNM_NOMATCH);
    109 				break;
    110 			}
    111 
    112 			/* General case, use recursion. */
    113 			while ((test = *string) != EOS) {
    114 				if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
    115 					return (0);
    116 				if (test == '/' && flags & FNM_PATHNAME)
    117 					break;
    118 				++string;
    119 			}
    120 			return (FNM_NOMATCH);
    121 		case '[':
    122 			if (*string == EOS)
    123 				return (FNM_NOMATCH);
    124 			if (*string == '/' && flags & FNM_PATHNAME)
    125 				return (FNM_NOMATCH);
    126 			if ((pattern =
    127 			    rangematch(pattern, *string, flags)) == NULL)
    128 				return (FNM_NOMATCH);
    129 			++string;
    130 			break;
    131 		case '\\':
    132 			if (!(flags & FNM_NOESCAPE)) {
    133 				if ((c = *pattern++) == EOS) {
    134 					c = '\\';
    135 					--pattern;
    136 				}
    137 			}
    138 			/* FALLTHROUGH */
    139 		default:
    140 			if (c != *string++)
    141 				return (FNM_NOMATCH);
    142 			break;
    143 		}
    144 	/* NOTREACHED */
    145 }
    146 
    147 static const char *
    148 rangematch(pattern, test, flags)
    149 	const char *pattern;
    150 	int test, flags;
    151 {
    152 	int negate, ok;
    153 	char c, c2;
    154 
    155 	/*
    156 	 * A bracket expression starting with an unquoted circumflex
    157 	 * character produces unspecified results (IEEE 1003.2-1992,
    158 	 * 3.13.2).  This implementation treats it like '!', for
    159 	 * consistency with the regular expression syntax.
    160 	 * J.T. Conklin (conklin (at) ngai.kaleida.com)
    161 	 */
    162 	if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
    163 		++pattern;
    164 
    165 	for (ok = 0; (c = *pattern++) != ']';) {
    166 		if (c == '\\' && !(flags & FNM_NOESCAPE))
    167 			c = *pattern++;
    168 		if (c == EOS)
    169 			return (NULL);
    170 		if (*pattern == '-'
    171 		    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
    172 			pattern += 2;
    173 			if (c2 == '\\' && !(flags & FNM_NOESCAPE))
    174 				c2 = *pattern++;
    175 			if (c2 == EOS)
    176 				return (NULL);
    177 			if (c <= test && test <= c2)
    178 				ok = 1;
    179 		} else if (c == test)
    180 			ok = 1;
    181 	}
    182 	return (ok == negate ? NULL : pattern);
    183 }
    184