home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / lib / fnmatch.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  212 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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #include <errno.h>
  25. #include <fnmatch.h>
  26.  
  27. #include <ctype.h>
  28.  
  29. #if defined (STDC_HEADERS) || !defined (isascii)
  30. #define ISASCII(c) 1
  31. #else
  32. #define ISASCII(c) isascii(c)
  33. #endif
  34.  
  35. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  36.  
  37.  
  38. /* Comment out all this code if we are using the GNU C Library, and are not
  39.    actually compiling the library itself.  This code is part of the GNU C
  40.    Library, but also included in many other GNU distributions.  Compiling
  41.    and linking in this code is a waste when using the GNU C library
  42.    (especially if it is a shared library).  Rather than having every GNU
  43.    program understand `configure --with-gnu-libc' and omit the object files,
  44.    it is simpler to just do this in the source for each such file.  */
  45.  
  46. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  47.  
  48.  
  49. #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
  50. extern int errno;
  51. #endif
  52.  
  53. /* Match STRING against the filename pattern PATTERN, returning zero if
  54.    it matches, nonzero if not.  */
  55. int
  56. fnmatch (pattern, string, flags)
  57.      const char *pattern;
  58.      const char *string;
  59.      int flags;
  60. {
  61.   register const char *p = pattern, *n = string;
  62.   register char c;
  63.  
  64. /* Note that this evalutes C many times.  */
  65. #define FOLD(c)    ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  66.  
  67.   while ((c = *p++) != '\0')
  68.     {
  69.       c = FOLD (c);
  70.  
  71.       switch (c)
  72.     {
  73.     case '?':
  74.       if (*n == '\0')
  75.         return FNM_NOMATCH;
  76.       else if ((flags & FNM_FILE_NAME) && *n == '/')
  77.         return FNM_NOMATCH;
  78.       else if ((flags & FNM_PERIOD) && *n == '.' &&
  79.            (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  80.         return FNM_NOMATCH;
  81.       break;
  82.  
  83.     case '\\':
  84.       if (!(flags & FNM_NOESCAPE))
  85.         {
  86.           c = *p++;
  87.           c = FOLD (c);
  88.         }
  89.       if (FOLD (*n) != c)
  90.         return FNM_NOMATCH;
  91.       break;
  92.  
  93.     case '*':
  94.       if ((flags & FNM_PERIOD) && *n == '.' &&
  95.           (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  96.         return FNM_NOMATCH;
  97.  
  98.       for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
  99.         if (((flags & FNM_FILE_NAME) && *n == '/') ||
  100.         (c == '?' && *n == '\0'))
  101.           return FNM_NOMATCH;
  102.  
  103.       if (c == '\0')
  104.         return 0;
  105.  
  106.       {
  107.         char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  108.         c1 = FOLD (c1);
  109.         for (--p; *n != '\0'; ++n)
  110.           if ((c == '[' || FOLD (*n) == c1) &&
  111.           fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  112.         return 0;
  113.         return FNM_NOMATCH;
  114.       }
  115.  
  116.     case '[':
  117.       {
  118.         /* Nonzero if the sense of the character class is inverted.  */
  119.         register int not;
  120.  
  121.         if (*n == '\0')
  122.           return FNM_NOMATCH;
  123.  
  124.         if ((flags & FNM_PERIOD) && *n == '.' &&
  125.         (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  126.           return FNM_NOMATCH;
  127.  
  128.         not = (*p == '!' || *p == '^');
  129.         if (not)
  130.           ++p;
  131.  
  132.         c = *p++;
  133.         for (;;)
  134.           {
  135.         register char cstart = c, cend = c;
  136.  
  137.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  138.           cstart = cend = *p++;
  139.  
  140.         cstart = cend = FOLD (cstart);
  141.  
  142.         if (c == '\0')
  143.           /* [ (unterminated) loses.  */
  144.           return FNM_NOMATCH;
  145.  
  146.         c = *p++;
  147.         c = FOLD (c);
  148.  
  149.         if ((flags & FNM_FILE_NAME) && c == '/')
  150.           /* [/] can never match.  */
  151.           return FNM_NOMATCH;
  152.  
  153.         if (c == '-' && *p != ']')
  154.           {
  155.             cend = *p++;
  156.             if (!(flags & FNM_NOESCAPE) && cend == '\\')
  157.               cend = *p++;
  158.             if (cend == '\0')
  159.               return FNM_NOMATCH;
  160.             cend = FOLD (cend);
  161.  
  162.             c = *p++;
  163.           }
  164.  
  165.         if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
  166.           goto matched;
  167.  
  168.         if (c == ']')
  169.           break;
  170.           }
  171.         if (!not)
  172.           return FNM_NOMATCH;
  173.         break;
  174.  
  175.       matched:;
  176.         /* Skip the rest of the [...] that already matched.  */
  177.         while (c != ']')
  178.           {
  179.         if (c == '\0')
  180.           /* [... (unterminated) loses.  */
  181.           return FNM_NOMATCH;
  182.  
  183.         c = *p++;
  184.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  185.           /* XXX 1003.2d11 is unclear if this is right.  */
  186.           ++p;
  187.           }
  188.         if (not)
  189.           return FNM_NOMATCH;
  190.       }
  191.       break;
  192.  
  193.     default:
  194.       if (c != FOLD (*n))
  195.         return FNM_NOMATCH;
  196.     }
  197.  
  198.       ++n;
  199.     }
  200.  
  201.   if (*n == '\0')
  202.     return 0;
  203.  
  204.   if ((flags & FNM_LEADING_DIR) && *n == '/')
  205.     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
  206.     return 0;
  207.  
  208.   return FNM_NOMATCH;
  209. }
  210.  
  211. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  212.