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