fnmatch.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #if defined STDC_HEADERS || !defined isascii
  24. # define IN_CTYPE_DOMAIN(c) 1
  25. #else
  26. # define IN_CTYPE_DOMAIN(c) isascii (c)
  27. #endif
  28. #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
  29. #ifndef errno
  30. extern int errno;
  31. #endif
  32. /* Match STRING against the filename pattern PATTERN, returning zero if
  33. it matches, nonzero if not. */
  34. int
  35. fnmatch (const char *pattern, const char *string, int flags)
  36. {
  37. register const char *p = pattern, *n = string;
  38. register char c;
  39. /* Note that this evaluates C many times. */
  40. #define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER ((unsigned char) (c)) \
  41. ? tolower ((unsigned char) (c)) \
  42. : (c))
  43. while ((c = *p++) != '\0')
  44. {
  45. c = FOLD (c);
  46. switch (c)
  47. {
  48. case '?':
  49. if (*n == '\0')
  50. return FNM_NOMATCH;
  51. else if ((flags & FNM_FILE_NAME) && *n == '/')
  52. return FNM_NOMATCH;
  53. else if ((flags & FNM_PERIOD) && *n == '.' &&
  54. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  55. return FNM_NOMATCH;
  56. break;
  57. case '\\':
  58. if (!(flags & FNM_NOESCAPE))
  59. {
  60. c = *p++;
  61. if (c == '\0')
  62. /* Trailing \ loses. */
  63. return FNM_NOMATCH;
  64. c = FOLD (c);
  65. }
  66. if (FOLD (*n) != c)
  67. return FNM_NOMATCH;
  68. break;
  69. case '*':
  70. if ((flags & FNM_PERIOD) && *n == '.' &&
  71. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  72. return FNM_NOMATCH;
  73. for (c = *p++; c == '?' || c == '*'; c = *p++)
  74. {
  75. if (c == '?')
  76. {
  77. /* A ? needs to match one character. */
  78. if (*n == '\0' || (*n == '/' && (flags & FNM_FILE_NAME)))
  79. /* There isn't another character; no match. */
  80. return FNM_NOMATCH;
  81. else
  82. /* One character of the string is consumed in matching
  83. this ? wildcard, so *??? won't match if there are
  84. less than three characters. */
  85. ++n;
  86. }
  87. }
  88. if (c == '\0')
  89. {
  90. if ((flags & (FNM_FILE_NAME | FNM_LEADING_DIR)) == FNM_FILE_NAME)
  91. for (; *n != '\0'; n++)
  92. if (*n == '/')
  93. return FNM_NOMATCH;
  94. return 0;
  95. }
  96. {
  97. char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  98. c1 = FOLD (c1);
  99. for (--p; *n != '\0'; ++n)
  100. if ((c == '[' || FOLD (*n) == c1) &&
  101. fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  102. return 0;
  103. else if (*n == '/' && (flags & FNM_FILE_NAME))
  104. break;
  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. {
  122. register char cstart = c, cend = c;
  123. if (!(flags & FNM_NOESCAPE) && c == '\\')
  124. {
  125. if (*p == '\0')
  126. return FNM_NOMATCH;
  127. cstart = cend = *p++;
  128. }
  129. cstart = cend = FOLD (cstart);
  130. if (c == '\0')
  131. /* [ (unterminated) loses. */
  132. return FNM_NOMATCH;
  133. c = *p++;
  134. c = FOLD (c);
  135. if ((flags & FNM_FILE_NAME) && c == '/')
  136. /* [/] can never match. */
  137. return FNM_NOMATCH;
  138. if (c == '-' && *p != ']')
  139. {
  140. cend = *p++;
  141. if (!(flags & FNM_NOESCAPE) && cend == '\\')
  142. cend = *p++;
  143. if (cend == '\0')
  144. return FNM_NOMATCH;
  145. cend = FOLD (cend);
  146. c = *p++;
  147. }
  148. if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
  149. goto matched;
  150. if (c == ']')
  151. break;
  152. }
  153. if (!not)
  154. return FNM_NOMATCH;
  155. break;
  156. matched:;
  157. /* Skip the rest of the [...] that already matched. */
  158. while (c != ']')
  159. {
  160. if (c == '\0')
  161. /* [... (unterminated) loses. */
  162. return FNM_NOMATCH;
  163. c = *p++;
  164. if (!(flags & FNM_NOESCAPE) && c == '\\')
  165. {
  166. if (*p == '\0')
  167. return FNM_NOMATCH;
  168. /* XXX 1003.2d11 is unclear if this is right. */
  169. ++p;
  170. }
  171. }
  172. if (not)
  173. return FNM_NOMATCH;
  174. }
  175. break;
  176. default:
  177. if (c != FOLD (*n))
  178. return FNM_NOMATCH;
  179. }
  180. ++n;
  181. }
  182. if (*n == '\0')
  183. return 0;
  184. if ((flags & FNM_LEADING_DIR) && *n == '/')
  185. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  186. return 0;
  187. return FNM_NOMATCH;
  188. #undef FOLD
  189. }