fnmatch.c 5.7 KB

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