root/match.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. match
  2. match_one

   1 /* match.c - simple shell-style filename matcher
   2 **
   3 ** Only does ? * and **, and multiple patterns separated by |.  Returns 1 or 0.
   4 **
   5 ** Copyright (c) 1995,2000 by Jef Poskanzer <jef@mail.acme.com>.
   6 ** Copyright (c) 2023 by Amelia Zabardast Ziabari <ame@psianesia.org>.
   7 ** All rights reserved.
   8 **
   9 ** Redistribution and use in source and binary forms, with or without
  10 ** modification, are permitted provided that the following conditions
  11 ** are met:
  12 ** 1. Redistributions of source code must retain the above copyright
  13 **    notice, this list of conditions and the following disclaimer.
  14 ** 2. Redistributions in binary form must reproduce the above copyright
  15 **    notice, this list of conditions and the following disclaimer in the
  16 **    documentation and/or other materials provided with the distribution.
  17 **
  18 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28 ** SUCH DAMAGE.
  29 */
  30 
  31 
  32 #include <string.h>
  33 
  34 #include "match.h"
  35 
  36 static int match_one( const char* pattern, int patternlen,
  37     const char* string );
  38 
  39 int
  40 match( const char* pattern, const char* string )
  41     {
  42     const char* or;
  43 
  44     for (;;)
  45         {
  46         or = strchr( pattern, '|' );
  47         if ( or == (char*) 0 )
  48             return match_one( pattern, strlen( pattern ), string );
  49         if ( match_one( pattern, or - pattern, string ) )
  50             return 1;
  51         pattern = or + 1;
  52         }
  53     }
  54 
  55 
  56 static int
  57 match_one( const char* pattern, int patternlen, const char* string )
  58     {
  59     const char* p;
  60 
  61     for ( p = pattern; p - pattern < patternlen; ++p, ++string )
  62         {
  63         if ( *p == '?' && *string != '\0' )
  64             continue;
  65         if ( *p == '*' )
  66             {
  67             int i, pl;
  68             ++p;
  69             if ( *p == '*' )
  70                 {
  71                 /* Double-wildcard matches anything. */
  72                 ++p;
  73                 i = strlen( string );
  74                 }
  75             else
  76                 /* Single-wildcard matches anything but slash. */
  77                 i = strcspn( string, "/" );
  78             pl = patternlen - ( p - pattern );
  79             for ( ; i >= 0; --i )
  80                 if ( match_one( p, pl, &(string[i]) ) )
  81                     return 1;
  82             return 0;
  83             }
  84         if ( *p != *string )
  85             return 0;
  86         }
  87     if ( *string == '\0' )
  88         return 1;
  89     return 0;
  90     }

/* [previous][next][first][last][top][bottom][index][help] */