home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / fnmatch.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  181 lines

  1. /*    $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd 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. #if defined(LIBC_SCCS) && !defined(lint)
  40. #if 0
  41. static char sccsid[] = "@(#)fnmatch.c    8.2 (Berkeley) 4/16/94";
  42. #else
  43. static char rcsid[] = "$NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $";
  44. #endif
  45. #endif /* LIBC_SCCS and not lint */
  46.  
  47. /*
  48.  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  49.  * Compares a filename or pathname to a pattern.
  50.  */
  51.  
  52. #define _KERNEL
  53. #include "ixemul.h"
  54.  
  55. #include <fnmatch.h>
  56. #include <string.h>
  57.  
  58. #define    EOS    '\0'
  59.  
  60. static const char *rangematch __P((const char *, int, int));
  61.  
  62. int
  63. fnmatch(pattern, string, flags)
  64.     const char *pattern, *string;
  65.     int flags;
  66. {
  67.     const char *stringstart;
  68.     char c, test;
  69.  
  70.     for (stringstart = string;;)
  71.         switch (c = *pattern++) {
  72.         case EOS:
  73.             return (*string == EOS ? 0 : FNM_NOMATCH);
  74.         case '?':
  75.             if (*string == EOS)
  76.                 return (FNM_NOMATCH);
  77.             if (*string == '/' && (flags & FNM_PATHNAME))
  78.                 return (FNM_NOMATCH);
  79.             if (*string == '.' && (flags & FNM_PERIOD) &&
  80.                 (string == stringstart ||
  81.                 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  82.                 return (FNM_NOMATCH);
  83.             ++string;
  84.             break;
  85.         case '*':
  86.             c = *pattern;
  87.             /* Collapse multiple stars. */
  88.             while (c == '*')
  89.                 c = *++pattern;
  90.  
  91.             if (*string == '.' && (flags & FNM_PERIOD) &&
  92.                 (string == stringstart ||
  93.                 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  94.                 return (FNM_NOMATCH);
  95.  
  96.             /* Optimize for pattern with * at end or before /. */
  97.             if (c == EOS)
  98.                 if (flags & FNM_PATHNAME)
  99.                     return (strchr(string, '/') == NULL ?
  100.                         0 : FNM_NOMATCH);
  101.                 else
  102.                     return (0);
  103.             else if (c == '/' && flags & FNM_PATHNAME) {
  104.                 if ((string = strchr(string, '/')) == NULL)
  105.                     return (FNM_NOMATCH);
  106.                 break;
  107.             }
  108.  
  109.             /* General case, use recursion. */
  110.             while ((test = *string) != EOS) {
  111.                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  112.                     return (0);
  113.                 if (test == '/' && flags & FNM_PATHNAME)
  114.                     break;
  115.                 ++string;
  116.             }
  117.             return (FNM_NOMATCH);
  118.         case '[':
  119.             if (*string == EOS)
  120.                 return (FNM_NOMATCH);
  121.             if (*string == '/' && flags & FNM_PATHNAME)
  122.                 return (FNM_NOMATCH);
  123.             if ((pattern =
  124.                 rangematch(pattern, *string, flags)) == NULL)
  125.                 return (FNM_NOMATCH);
  126.             ++string;
  127.             break;
  128.         case '\\':
  129.             if (!(flags & FNM_NOESCAPE)) {
  130.                 if ((c = *pattern++) == EOS) {
  131.                     c = '\\';
  132.                     --pattern;
  133.                 }
  134.             }
  135.             /* FALLTHROUGH */
  136.         default:
  137.             if (c != *string++)
  138.                 return (FNM_NOMATCH);
  139.             break;
  140.         }
  141.     /* NOTREACHED */
  142. }
  143.  
  144. static const char *
  145. rangematch(pattern, test, flags)
  146.     const char *pattern;
  147.     int test, flags;
  148. {
  149.     int negate, ok;
  150.     char c, c2;
  151.  
  152.     /*
  153.      * A bracket expression starting with an unquoted circumflex
  154.      * character produces unspecified results (IEEE 1003.2-1992,
  155.      * 3.13.2).  This implementation treats it like '!', for
  156.      * consistency with the regular expression syntax.
  157.      * J.T. Conklin (conklin@ngai.kaleida.com)
  158.      */
  159.     if ((negate = (*pattern == '!' || *pattern == '^')))
  160.         ++pattern;
  161.     
  162.     for (ok = 0; (c = *pattern++) != ']';) {
  163.         if (c == '\\' && !(flags & FNM_NOESCAPE))
  164.             c = *pattern++;
  165.         if (c == EOS)
  166.             return (NULL);
  167.         if (*pattern == '-' 
  168.             && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  169.             pattern += 2;
  170.             if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  171.                 c2 = *pattern++;
  172.             if (c2 == EOS)
  173.                 return (NULL);
  174.             if (c <= test && test <= c2)
  175.                 ok = 1;
  176.         } else if (c == test)
  177.             ok = 1;
  178.     }
  179.     return (ok == negate ? NULL : pattern);
  180. }
  181.