fnmatch.c 5.5 KB

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