fnmatch.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (C) 1991, 1992, 1993, 1996, 1997 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 ((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. {
  86. /* A ? needs to match one character. */
  87. if (*n == '\0')
  88. /* There isn't another character; no match. */
  89. return FNM_NOMATCH;
  90. else
  91. /* One character of the string is consumed in matching
  92. this ? wildcard, so *??? won't match if there are
  93. less than three characters. */
  94. ++n;
  95. }
  96. }
  97. if (c == '\0')
  98. return 0;
  99. {
  100. char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  101. c1 = FOLD (c1);
  102. for (--p; *n != '\0'; ++n)
  103. if ((c == '[' || FOLD (*n) == c1) &&
  104. fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  105. return 0;
  106. return FNM_NOMATCH;
  107. }
  108. case '[':
  109. {
  110. /* Nonzero if the sense of the character class is inverted. */
  111. register int not;
  112. if (*n == '\0')
  113. return FNM_NOMATCH;
  114. if ((flags & FNM_PERIOD) && *n == '.' &&
  115. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  116. return FNM_NOMATCH;
  117. not = (*p == '!' || *p == '^');
  118. if (not)
  119. ++p;
  120. c = *p++;
  121. for (;;)
  122. {
  123. register char cstart = c, cend = c;
  124. if (!(flags & FNM_NOESCAPE) && c == '\\')
  125. {
  126. if (*p == '\0')
  127. return FNM_NOMATCH;
  128. cstart = cend = *p++;
  129. }
  130. cstart = cend = FOLD (cstart);
  131. if (c == '\0')
  132. /* [ (unterminated) loses. */
  133. return FNM_NOMATCH;
  134. c = *p++;
  135. c = FOLD (c);
  136. if ((flags & FNM_FILE_NAME) && c == '/')
  137. /* [/] can never match. */
  138. return FNM_NOMATCH;
  139. if (c == '-' && *p != ']')
  140. {
  141. cend = *p++;
  142. if (!(flags & FNM_NOESCAPE) && cend == '\\')
  143. cend = *p++;
  144. if (cend == '\0')
  145. return FNM_NOMATCH;
  146. cend = FOLD (cend);
  147. c = *p++;
  148. }
  149. if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
  150. goto matched;
  151. if (c == ']')
  152. break;
  153. }
  154. if (!not)
  155. return FNM_NOMATCH;
  156. break;
  157. matched:;
  158. /* Skip the rest of the [...] that already matched. */
  159. while (c != ']')
  160. {
  161. if (c == '\0')
  162. /* [... (unterminated) loses. */
  163. return FNM_NOMATCH;
  164. c = *p++;
  165. if (!(flags & FNM_NOESCAPE) && c == '\\')
  166. {
  167. if (*p == '\0')
  168. return FNM_NOMATCH;
  169. /* XXX 1003.2d11 is unclear if this is right. */
  170. ++p;
  171. }
  172. }
  173. if (not)
  174. return FNM_NOMATCH;
  175. }
  176. break;
  177. default:
  178. if (c != FOLD (*n))
  179. return FNM_NOMATCH;
  180. }
  181. ++n;
  182. }
  183. if (*n == '\0')
  184. return 0;
  185. if ((flags & FNM_LEADING_DIR) && *n == '/')
  186. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  187. return 0;
  188. return FNM_NOMATCH;
  189. # undef FOLD
  190. }
  191. #endif /* _LIBC or not __GNU_LIBRARY__. */