The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/fnmatch.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d3c5961a4b6f21eb89700c91f4561f4c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.