home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Utilities / mkisofs-1.11-MIHS / fnmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-09  |  5.3 KB  |  227 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. NOTE: The canonical source of this file is maintained with the GNU C Library.
  4. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. static char rcsid[] ="$Id: fnmatch.c,v 1.3 1997/03/22 02:53:13 eric Rel $";
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <errno.h>
  27. #include <fnmatch.h>
  28.  
  29. #ifndef FNM_FILE_NAME
  30. #define    FNM_FILE_NAME    FNM_PATHNAME /* Preferred GNU name.  */
  31. #endif
  32.  
  33. #ifndef FNM_LEADING_DIR
  34. #define    FNM_LEADING_DIR    (1 << 3) /* Ignore `/...' after a match.  */
  35. #endif
  36.  
  37. #ifndef FNM_CASEFOLD
  38. #define    FNM_CASEFOLD    (1 << 4) /* Compare without regard to case.  */
  39. #endif
  40.  
  41.  
  42. #include <ctype.h>
  43.  
  44. #if defined (STDC_HEADERS) || !defined (isascii)
  45. #define ISASCII(c) 1
  46. #else
  47. #define ISASCII(c) isascii(c)
  48. #endif
  49.  
  50. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  51.  
  52.  
  53. /* Comment out all this code if we are using the GNU C Library, and are not
  54.    actually compiling the library itself.  This code is part of the GNU C
  55.    Library, but also included in many other GNU distributions.  Compiling
  56.    and linking in this code is a waste when using the GNU C library
  57.    (especially if it is a shared library).  Rather than having every GNU
  58.    program understand `configure --with-gnu-libc' and omit the object files,
  59.    it is simpler to just do this in the source for each such file.  */
  60.  
  61. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  62.  
  63.  
  64. #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
  65. extern int errno;
  66. #endif
  67.  
  68. /* Match STRING against the filename pattern PATTERN, returning zero if
  69.    it matches, nonzero if not.  */
  70. int
  71. fnmatch (pattern, string, flags)
  72.      const char *pattern;
  73.      const char *string;
  74.      int flags;
  75. {
  76.   register const char *p = pattern, *n = string;
  77.   register char c;
  78.  
  79. /* Note that this evalutes C many times.  */
  80. #define FOLD(c)    ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  81.  
  82.   while ((c = *p++) != '\0')
  83.     {
  84.       c = FOLD (c);
  85.  
  86.       switch (c)
  87.     {
  88.     case '?':
  89.       if (*n == '\0')
  90.         return FNM_NOMATCH;
  91.       else if ((flags & FNM_FILE_NAME) && *n == '/')
  92.         return FNM_NOMATCH;
  93.       else if ((flags & FNM_PERIOD) && *n == '.' &&
  94.            (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  95.         return FNM_NOMATCH;
  96.       break;
  97.  
  98.     case '\\':
  99.       if (!(flags & FNM_NOESCAPE))
  100.         {
  101.           c = *p++;
  102.           c = FOLD (c);
  103.         }
  104.       if (FOLD (*n) != c)
  105.         return FNM_NOMATCH;
  106.       break;
  107.  
  108.     case '*':
  109.       if ((flags & FNM_PERIOD) && *n == '.' &&
  110.           (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  111.         return FNM_NOMATCH;
  112.  
  113.       for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
  114.         if (((flags & FNM_FILE_NAME) && *n == '/') ||
  115.         (c == '?' && *n == '\0'))
  116.           return FNM_NOMATCH;
  117.  
  118.       if (c == '\0')
  119.         return 0;
  120.  
  121.       {
  122.         char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  123.         c1 = FOLD (c1);
  124.         for (--p; *n != '\0'; ++n)
  125.           if ((c == '[' || FOLD (*n) == c1) &&
  126.           fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  127.         return 0;
  128.         return FNM_NOMATCH;
  129.       }
  130.  
  131.     case '[':
  132.       {
  133.         /* Nonzero if the sense of the character class is inverted.  */
  134.         register int not;
  135.  
  136.         if (*n == '\0')
  137.           return FNM_NOMATCH;
  138.  
  139.         if ((flags & FNM_PERIOD) && *n == '.' &&
  140.         (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  141.           return FNM_NOMATCH;
  142.  
  143.         not = (*p == '!' || *p == '^');
  144.         if (not)
  145.           ++p;
  146.  
  147.         c = *p++;
  148.         for (;;)
  149.           {
  150.         register char cstart = c, cend = c;
  151.  
  152.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  153.           cstart = cend = *p++;
  154.  
  155.         cstart = cend = FOLD (cstart);
  156.  
  157.         if (c == '\0')
  158.           /* [ (unterminated) loses.  */
  159.           return FNM_NOMATCH;
  160.  
  161.         c = *p++;
  162.         c = FOLD (c);
  163.  
  164.         if ((flags & FNM_FILE_NAME) && c == '/')
  165.           /* [/] can never match.  */
  166.           return FNM_NOMATCH;
  167.  
  168.         if (c == '-' && *p != ']')
  169.           {
  170.             cend = *p++;
  171.             if (!(flags & FNM_NOESCAPE) && cend == '\\')
  172.               cend = *p++;
  173.             if (cend == '\0')
  174.               return FNM_NOMATCH;
  175.             cend = FOLD (cend);
  176.  
  177.             c = *p++;
  178.           }
  179.  
  180.         if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
  181.           goto matched;
  182.  
  183.         if (c == ']')
  184.           break;
  185.           }
  186.         if (!not)
  187.           return FNM_NOMATCH;
  188.         break;
  189.  
  190.       matched:;
  191.         /* Skip the rest of the [...] that already matched.  */
  192.         while (c != ']')
  193.           {
  194.         if (c == '\0')
  195.           /* [... (unterminated) loses.  */
  196.           return FNM_NOMATCH;
  197.  
  198.         c = *p++;
  199.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  200.           /* XXX 1003.2d11 is unclear if this is right.  */
  201.           ++p;
  202.           }
  203.         if (not)
  204.           return FNM_NOMATCH;
  205.       }
  206.       break;
  207.  
  208.     default:
  209.       if (c != FOLD (*n))
  210.         return FNM_NOMATCH;
  211.     }
  212.  
  213.       ++n;
  214.     }
  215.  
  216.   if (*n == '\0')
  217.     return 0;
  218.  
  219.   if ((flags & FNM_LEADING_DIR) && *n == '/')
  220.     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
  221.     return 0;
  222.  
  223.   return FNM_NOMATCH;
  224. }
  225.  
  226. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  227.