fnmatch.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. NOTE: This source is derived from an old version taken from the GNU C
  3. Library (glibc).
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #ifdef HAVE_CONFIG_H
  17. #if defined (CONFIG_BROKETS)
  18. /* We use <config.h> instead of "config.h" so that a compilation
  19. using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  20. (which it would do because it found this file in $srcdir). */
  21. #include <config.h>
  22. #else
  23. #include "config.h"
  24. #endif
  25. #endif
  26. #ifndef _GNU_SOURCE
  27. #define _GNU_SOURCE
  28. #endif
  29. /* This code to undef const added in libiberty. */
  30. #ifndef __STDC__
  31. /* This is a separate conditional since some stdc systems
  32. reject `defined (const)'. */
  33. #ifndef const
  34. #define const
  35. #endif
  36. #endif
  37. #include <errno.h>
  38. #include <fnmatch.h>
  39. #include <safe-ctype.h>
  40. /* Comment out all this code if we are using the GNU C Library, and are not
  41. actually compiling the library itself. This code is part of the GNU C
  42. Library, but also included in many other GNU distributions. Compiling
  43. and linking in this code is a waste when using the GNU C library
  44. (especially if it is a shared library). Rather than having every GNU
  45. program understand `configure --with-gnu-libc' and omit the object files,
  46. it is simpler to just do this in the source for each such file. */
  47. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  48. #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
  49. extern int errno;
  50. #endif
  51. /* Match STRING against the filename pattern PATTERN, returning zero if
  52. it matches, nonzero if not. */
  53. int
  54. fnmatch (const char *pattern, const char *string, int flags)
  55. {
  56. register const char *p = pattern, *n = string;
  57. register unsigned char c;
  58. #define FOLD(c) ((flags & FNM_CASEFOLD) ? TOLOWER (c) : (c))
  59. while ((c = *p++) != '\0')
  60. {
  61. c = FOLD (c);
  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. case '\\':
  74. if (!(flags & FNM_NOESCAPE))
  75. {
  76. c = *p++;
  77. c = FOLD (c);
  78. }
  79. if (FOLD ((unsigned char)*n) != c)
  80. return FNM_NOMATCH;
  81. break;
  82. case '*':
  83. if ((flags & FNM_PERIOD) && *n == '.' &&
  84. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  85. return FNM_NOMATCH;
  86. for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
  87. if (((flags & FNM_FILE_NAME) && *n == '/') ||
  88. (c == '?' && *n == '\0'))
  89. return FNM_NOMATCH;
  90. if (c == '\0')
  91. return 0;
  92. {
  93. unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  94. c1 = FOLD (c1);
  95. for (--p; *n != '\0'; ++n)
  96. if ((c == '[' || FOLD ((unsigned char)*n) == c1) &&
  97. fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  98. return 0;
  99. return FNM_NOMATCH;
  100. }
  101. case '[':
  102. {
  103. /* Nonzero if the sense of the character class is inverted. */
  104. register int negate;
  105. if (*n == '\0')
  106. return FNM_NOMATCH;
  107. if ((flags & FNM_PERIOD) && *n == '.' &&
  108. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  109. return FNM_NOMATCH;
  110. negate = (*p == '!' || *p == '^');
  111. if (negate)
  112. ++p;
  113. c = *p++;
  114. for (;;)
  115. {
  116. register unsigned char cstart = c, cend = c;
  117. if (!(flags & FNM_NOESCAPE) && c == '\\')
  118. cstart = cend = *p++;
  119. cstart = cend = FOLD (cstart);
  120. if (c == '\0')
  121. /* [ (unterminated) loses. */
  122. return FNM_NOMATCH;
  123. c = *p++;
  124. c = FOLD (c);
  125. if ((flags & FNM_FILE_NAME) && c == '/')
  126. /* [/] can never match. */
  127. return FNM_NOMATCH;
  128. if (c == '-' && *p != ']')
  129. {
  130. cend = *p++;
  131. if (!(flags & FNM_NOESCAPE) && cend == '\\')
  132. cend = *p++;
  133. if (cend == '\0')
  134. return FNM_NOMATCH;
  135. cend = FOLD (cend);
  136. c = *p++;
  137. }
  138. if (FOLD ((unsigned char)*n) >= cstart
  139. && FOLD ((unsigned char)*n) <= cend)
  140. goto matched;
  141. if (c == ']')
  142. break;
  143. }
  144. if (!negate)
  145. return FNM_NOMATCH;
  146. break;
  147. matched:;
  148. /* Skip the rest of the [...] that already matched. */
  149. while (c != ']')
  150. {
  151. if (c == '\0')
  152. /* [... (unterminated) loses. */
  153. return FNM_NOMATCH;
  154. c = *p++;
  155. if (!(flags & FNM_NOESCAPE) && c == '\\')
  156. /* XXX 1003.2d11 is unclear if this is right. */
  157. ++p;
  158. }
  159. if (negate)
  160. return FNM_NOMATCH;
  161. }
  162. break;
  163. default:
  164. if (c != FOLD ((unsigned char)*n))
  165. return FNM_NOMATCH;
  166. }
  167. ++n;
  168. }
  169. if (*n == '\0')
  170. return 0;
  171. if ((flags & FNM_LEADING_DIR) && *n == '/')
  172. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  173. return 0;
  174. return FNM_NOMATCH;
  175. }
  176. #endif /* _LIBC or not __GNU_LIBRARY__. */