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