home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / ap / ap_fnmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-13  |  6.3 KB  |  239 lines

  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. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)fnmatch.c    8.2 (Berkeley) 4/16/94";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. /*
  42.  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  43.  * Compares a filename or pathname to a pattern.
  44.  */
  45.  
  46. #include "ap_config.h"
  47. #include "fnmatch.h"
  48. #include <string.h>
  49.  
  50. #define    EOS    '\0'
  51.  
  52. static const char *rangematch(const char *, int, int);
  53.  
  54. API_EXPORT(int) ap_fnmatch(const char *pattern, const char *string, int flags)
  55. {
  56.     const char *stringstart;
  57.     char c, test;
  58.  
  59.     for (stringstart = string;;) {
  60.     switch (c = *pattern++) {
  61.     case EOS:
  62.         return (*string == EOS ? 0 : FNM_NOMATCH);
  63.     case '?':
  64.         if (*string == EOS) {
  65.         return (FNM_NOMATCH);
  66.         }
  67.         if (*string == '/' && (flags & FNM_PATHNAME)) {
  68.         return (FNM_NOMATCH);
  69.         }
  70.         if (*string == '.' && (flags & FNM_PERIOD) &&
  71.         (string == stringstart ||
  72.          ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
  73.         return (FNM_NOMATCH);
  74.         }
  75.         ++string;
  76.         break;
  77.     case '*':
  78.         c = *pattern;
  79.         /* Collapse multiple stars. */
  80.         while (c == '*') {
  81.         c = *++pattern;
  82.         }
  83.  
  84.         if (*string == '.' && (flags & FNM_PERIOD) &&
  85.         (string == stringstart ||
  86.          ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
  87.         return (FNM_NOMATCH);
  88.         }
  89.  
  90.         /* Optimize for pattern with * at end or before /. */
  91.         if (c == EOS) {
  92.         if (flags & FNM_PATHNAME) {
  93.             return (strchr(string, '/') == NULL ? 0 : FNM_NOMATCH);
  94.         }
  95.         else {
  96.             return (0);
  97.         }
  98.         }
  99.         else if (c == '/' && flags & FNM_PATHNAME) {
  100.             if ((string = strchr(string, '/')) == NULL) {
  101.             return (FNM_NOMATCH);
  102.         }
  103.         break;
  104.         }
  105.  
  106.         /* General case, use recursion. */
  107.         while ((test = *string) != EOS) {
  108.             if (!ap_fnmatch(pattern, string, flags & ~FNM_PERIOD)) {
  109.             return (0);
  110.         }
  111.         if (test == '/' && flags & FNM_PATHNAME) {
  112.             break;
  113.         }
  114.         ++string;
  115.         }
  116.         return (FNM_NOMATCH);
  117.     case '[':
  118.         if (*string == EOS) {
  119.         return (FNM_NOMATCH);
  120.         }
  121.         if (*string == '/' && flags & FNM_PATHNAME) {
  122.         return (FNM_NOMATCH);
  123.         }
  124.         if (*string == '.' && (flags & FNM_PERIOD) &&
  125.         (string == stringstart ||
  126.          ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
  127.             return (FNM_NOMATCH);
  128.         }
  129.         if ((pattern = rangematch(pattern, *string, flags)) == NULL) {
  130.         return (FNM_NOMATCH);
  131.         }
  132.         ++string;
  133.         break;
  134.     case '\\':
  135.         if (!(flags & FNM_NOESCAPE)) {
  136.         if ((c = *pattern++) == EOS) {
  137.             c = '\\';
  138.             --pattern;
  139.         }
  140.         }
  141.         /* FALLTHROUGH */
  142.     default:
  143.         if (flags & FNM_CASE_BLIND) {
  144.             if (ap_tolower(c) != ap_tolower(*string)) {
  145.             return (FNM_NOMATCH);
  146.         }
  147.         }
  148.         else if (c != *string) {
  149.             return (FNM_NOMATCH);
  150.         }
  151.         string++;
  152.         break;
  153.     }
  154.     /* NOTREACHED */
  155.     }
  156. }
  157.  
  158. static const char *rangematch(const char *pattern, int test, int flags)
  159. {
  160.     int negate, ok;
  161.     char c, c2;
  162.  
  163.     /*
  164.      * A bracket expression starting with an unquoted circumflex
  165.      * character produces unspecified results (IEEE 1003.2-1992,
  166.      * 3.13.2).  This implementation treats it like '!', for
  167.      * consistency with the regular expression syntax.
  168.      * J.T. Conklin (conklin@ngai.kaleida.com)
  169.      */
  170.     if ((negate = (*pattern == '!' || *pattern == '^'))) {
  171.     ++pattern;
  172.     }
  173.  
  174.     for (ok = 0; (c = *pattern++) != ']';) {
  175.         if (c == '\\' && !(flags & FNM_NOESCAPE)) {
  176.         c = *pattern++;
  177.     }
  178.     if (c == EOS) {
  179.         return (NULL);
  180.     }
  181.     if (*pattern == '-' && (c2 = *(pattern + 1)) != EOS && c2 != ']') {
  182.         pattern += 2;
  183.         if (c2 == '\\' && !(flags & FNM_NOESCAPE)) {
  184.         c2 = *pattern++;
  185.         }
  186.         if (c2 == EOS) {
  187.         return (NULL);
  188.         }
  189.         if ((c <= test && test <= c2)
  190.         || ((flags & FNM_CASE_BLIND)
  191.             && ((ap_tolower(c) <= ap_tolower(test))
  192.             && (ap_tolower(test) <= ap_tolower(c2))))) {
  193.         ok = 1;
  194.         }
  195.     }
  196.     else if ((c == test)
  197.          || ((flags & FNM_CASE_BLIND)
  198.              && (ap_tolower(c) == ap_tolower(test)))) {
  199.         ok = 1;
  200.     }
  201.     }
  202.     return (ok == negate ? NULL : pattern);
  203. }
  204.  
  205.  
  206. /* This function is an Apache addition */
  207. /* return non-zero if pattern has any glob chars in it */
  208. API_EXPORT(int) ap_is_fnmatch(const char *pattern)
  209. {
  210.     int nesting;
  211.  
  212.     nesting = 0;
  213.     while (*pattern) {
  214.     switch (*pattern) {
  215.     case '?':
  216.     case '*':
  217.         return 1;
  218.  
  219.     case '\\':
  220.         if (*pattern++ == '\0') {
  221.         return 0;
  222.         }
  223.         break;
  224.  
  225.     case '[':    /* '[' is only a glob if it has a matching ']' */
  226.         ++nesting;
  227.         break;
  228.  
  229.     case ']':
  230.         if (nesting) {
  231.         return 1;
  232.         }
  233.         break;
  234.     }
  235.     ++pattern;
  236.     }
  237.     return 0;
  238. }
  239.