home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / wu-ftpd-2.4.2b13-MIHS / support / fnmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-03  |  5.6 KB  |  205 lines

  1. /*
  2.  * Copyright (c) 1989, 1993
  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. /* from: static char sccsid[] = "@(#)fnmatch.c    8.1 (Berkeley) 6/4/93"; */
  39. static char *rcsid = "$Id: fnmatch.c,v 1.5 1997/03/03 09:12:24 sob Exp $";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. /*
  43.  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  44.  * Compares a filename or pathname to a pattern.
  45.  */
  46. #ifdef BSD
  47. #include <strings.h>
  48. #else
  49. #include <string.h>
  50. #endif
  51.  
  52. #define    FNM_NOMATCH    1    /* Match failed. */
  53.  
  54. #ifndef FNM_PATHNAME
  55. #define    FNM_PATHNAME    0x01    /* Slash must be matched by slash. */
  56. #endif
  57. #ifndef    FNM_NOESCAPE
  58. #define    FNM_NOESCAPE    0x02    /* Disable backslash escaping. */
  59. #endif
  60. #ifndef FNM_PERIOD
  61. #define    FNM_PERIOD    0x04    /* Period must be matched by period. */
  62. #endif
  63.  
  64.  
  65. #define    EOS    '\0'
  66.  
  67. #ifdef __STDC__
  68. static char *rangematch(char *pattern, int test, int flags)
  69. #else
  70. static char *
  71. rangematch(pattern, test, flags)
  72.     register char *pattern;
  73.     register int test;
  74.     int flags;
  75. #endif
  76. {
  77.     register char c, c2;
  78.     int negate, ok;
  79.  
  80.     /* A bracket expression starting with an unquoted circumflex
  81.      * character produces unspecified results (IEEE 1003.2-1992,
  82.      * 3.13.2).  I have chosen to treat it like '!', for 
  83.      * consistancy with regular expression syntax.
  84.      */
  85.     if (negate = (*pattern == '!' || *pattern == '^')) {
  86.         pattern++;
  87.     }
  88.     
  89.     for (ok = 0; (c = *pattern++) != ']';) {
  90.         if (c == '\\' && !(flags & FNM_NOESCAPE)) {
  91.             c = *pattern++;
  92.             }
  93.         if (c == EOS) {
  94.             return (NULL);
  95.         }
  96.  
  97.         if (*pattern == '-' 
  98.             && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  99.             pattern += 2;
  100.             if (c2 == '\\' && !(flags & FNM_NOESCAPE)) {
  101.                 c2 = *pattern++;
  102.             }
  103.             if (c2 == EOS) {
  104.                 return (NULL);
  105.             }
  106.             
  107.             if (c <= test && test <= c2) {
  108.                 ok = 1;
  109.             }
  110.         } else if (c == test) {
  111.             ok = 1;
  112.         }
  113.     }
  114.  
  115.     return (ok == negate ? NULL : pattern);
  116. }
  117.  
  118.  
  119.  
  120.  
  121. #ifdef _STDC_
  122. int fnmatch(char *pattern, char *string, int flags)
  123. #else
  124. int
  125. fnmatch(pattern, string, flags)
  126.     register char *pattern, *string;
  127.     int flags;
  128. #endif
  129. {
  130.     char *stringstart = string;
  131.     register char c;
  132.     char test;
  133.  
  134.     for (;;)
  135.         switch (c = *pattern++) {
  136.         case EOS:
  137.             return (*string == EOS ? 0 : FNM_NOMATCH);
  138.         case '?':
  139.             if (*string == EOS)
  140.                 return (FNM_NOMATCH);
  141.             if (*string == '/' && (flags & FNM_PATHNAME))
  142.                 return (FNM_NOMATCH);
  143.             if (*string == '.' && (flags & FNM_PERIOD) &&
  144.                 (string == stringstart || ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  145.                 return (FNM_NOMATCH);
  146.             ++string;
  147.             break;
  148.         case '*':
  149.             c = *pattern;
  150.             /* Collapse multiple stars. */
  151.             while (c == '*')
  152.                 c = *++pattern;
  153.  
  154.             if (*string == '.' && (flags & FNM_PERIOD) &&
  155.                 (string == stringstart || ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  156.                 return (FNM_NOMATCH);
  157.  
  158.             /* Optimize for pattern with * at end or before /. */
  159.             if (c == EOS)
  160.                 if (flags & FNM_PATHNAME)
  161.                     return (strchr(string, '/') == NULL ?
  162.                         0 : FNM_NOMATCH);
  163.                 else
  164.                     return (0);
  165.             else if (c == '/' && flags & FNM_PATHNAME) {
  166.                 if ((string = strchr(string, '/')) == NULL)
  167.                     return (FNM_NOMATCH);
  168.                 break;
  169.             }
  170.  
  171.             /* General case, use recursion. */
  172.             while ((test = *string) != EOS) {
  173.                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  174.                     return (0);
  175.                 if (test == '/' && flags & FNM_PATHNAME)
  176.                     break;
  177.                 ++string;
  178.             }
  179.             return (FNM_NOMATCH);
  180.         case '[':
  181.             if (*string == EOS)
  182.                 return (FNM_NOMATCH);
  183.             if (*string == '/' && flags & FNM_PATHNAME)
  184.                 return (FNM_NOMATCH);
  185.             if ((pattern = rangematch(pattern, *string, flags)) == NULL)
  186.                 return (FNM_NOMATCH);
  187.             ++string;
  188.             break;
  189.         case '\\':
  190.             if (!(flags & FNM_NOESCAPE)) {
  191.                 if ((c = *pattern++) == EOS) {
  192.                     c = '\\';
  193.                     --pattern;
  194.                 }
  195.             }
  196.             /* FALLTHROUGH */
  197.         default:
  198.             if (c != *string++)
  199.                 return (FNM_NOMATCH);
  200.             break;
  201.         }
  202.     /* NOTREACHED */
  203. }
  204.  
  205.