home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / gen_library / rcs / fnmatch.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  4.3 KB  |  175 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    92.06.08.18.31.20;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @initial checkin
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1989 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * This code is derived from software contributed to Berkeley by
  30.  * Guido van Rossum.
  31.  *
  32.  * Redistribution and use in source and binary forms, with or without
  33.  * modification, are permitted provided that the following conditions
  34.  * are met:
  35.  * 1. Redistributions of source code must retain the above copyright
  36.  *    notice, this list of conditions and the following disclaimer.
  37.  * 2. Redistributions in binary form must reproduce the above copyright
  38.  *    notice, this list of conditions and the following disclaimer in the
  39.  *    documentation and/or other materials provided with the distribution.
  40.  * 3. All advertising materials mentioning features or use of this software
  41.  *    must display the following acknowledgement:
  42.  *    This product includes software developed by the University of
  43.  *    California, Berkeley and its contributors.
  44.  * 4. Neither the name of the University nor the names of its contributors
  45.  *    may be used to endorse or promote products derived from this software
  46.  *    without specific prior written permission.
  47.  *
  48.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  49.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58.  * SUCH DAMAGE.
  59.  */
  60.  
  61. #if defined(LIBC_SCCS) && !defined(lint)
  62. static char sccsid[] = "@@(#)fnmatch.c    5.4 (Berkeley) 2/23/91";
  63. #endif /* LIBC_SCCS and not lint */
  64.  
  65. #define KERNEL
  66. #include "ixemul.h"
  67.  
  68. /*
  69.  * Function fnmatch() as proposed in Posix 1003.2 B.6 (rev. 9).
  70.  * Compares a filename or pathname to a pattern.
  71.  */
  72.  
  73. #include <unistd.h>
  74. #include <string.h>
  75.  
  76. #define    EOS    '\0'
  77.  
  78. static char *
  79. rangematch(pattern, test)
  80.     register char *pattern, test;
  81. {
  82.     register char c, c2;
  83.     int negate, ok;
  84.  
  85.     if (negate = (*pattern == '!'))
  86.         ++pattern;
  87.  
  88.     /*
  89.      * TO DO: quoting
  90.      */
  91.  
  92.     for (ok = 0; (c = *pattern++) != ']';) {
  93.         if (c == EOS)
  94.             return(NULL);        /* illegal pattern */
  95.         if (*pattern == '-' && (c2 = pattern[1]) != EOS && c2 != ']') {
  96.             if (c <= test && test <= c2)
  97.                 ok = 1;
  98.             pattern += 2;
  99.         }
  100.         else if (c == test)
  101.             ok = 1;
  102.     }
  103.     return(ok == negate ? NULL : pattern);
  104. }
  105.  
  106. fnmatch(pattern, string, flags)
  107.     register const char *pattern;
  108.     register const char *string;
  109.     int flags;
  110. {
  111.     register char c;
  112.     char test, *rangematch();
  113.  
  114.     for (;;)
  115.         switch (c = *pattern++) {
  116.         case EOS:
  117.             return(*string == EOS);
  118.         case '?':
  119.             if ((test = *string++) == EOS ||
  120.                 test == '/' && flags & FNM_PATHNAME)
  121.                 return(0);
  122.             break;
  123.         case '*':
  124.             c = *pattern;
  125.             /* collapse multiple stars */
  126.             while (c == '*')
  127.                 c = *++pattern;
  128.  
  129.             /* optimize for pattern with * at end or before / */
  130.             if (c == EOS)
  131.                 if (flags & FNM_PATHNAME)
  132.                     return(!index(string, '/'));
  133.                 else
  134.                     return(1);
  135.             else if (c == '/' && flags & FNM_PATHNAME) {
  136.                 if ((string = index(string, '/')) == NULL)
  137.                     return(0);
  138.                 break;
  139.             }
  140.  
  141.             /* general case, use recursion */
  142.             while ((test = *string) != EOS) {
  143.                 if (fnmatch(pattern, string, flags))
  144.                     return(1);
  145.                 if (test == '/' && flags & FNM_PATHNAME)
  146.                     break;
  147.                 ++string;
  148.             }
  149.             return(0);
  150.         case '[':
  151.             if ((test = *string++) == EOS ||
  152.                 test == '/' && flags & FNM_PATHNAME)
  153.                 return(0);
  154.             if ((pattern = rangematch(pattern, test)) == NULL)
  155.                 return(0);
  156.             break;
  157.         case '\\':
  158.             if (flags & FNM_QUOTE) {
  159.                 if ((c = *pattern++) == EOS) {
  160.                     c = '\\';
  161.                     --pattern;
  162.                 }
  163.                 if (c != *string++)
  164.                     return(0);
  165.                 break;
  166.             }
  167.             /* FALLTHROUGH */
  168.         default:
  169.             if (c != *string++)
  170.                 return(0);
  171.             break;
  172.         }
  173. }
  174. @
  175.