fnmatch.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* $NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $ */
  2. /*
  3. * Copyright (c) 1989, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Guido van Rossum.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*
  34. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  35. * Compares a filename or pathname to a pattern.
  36. */
  37. #include <ctype.h>
  38. #include "fnmatch.h"
  39. #include <string.h>
  40. #define EOS '\0'
  41. static inline int foldcase(int ch, int flags)
  42. {
  43. if ((flags & FNM_CASEFOLD) != 0 && isupper(ch))
  44. return tolower(ch);
  45. return ch;
  46. }
  47. #define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
  48. static const char * rangematch(const char *pattern, int test, int flags)
  49. {
  50. int negate, ok, need;
  51. char c, c2;
  52. if (pattern == NULL)
  53. {
  54. return NULL;
  55. }
  56. /*
  57. * A bracket expression starting with an unquoted circumflex
  58. * character produces unspecified results (IEEE 1003.2-1992,
  59. * 3.13.2). This implementation treats it like '!', for
  60. * consistency with the regular expression syntax.
  61. * J.T. Conklin (conklin@ngai.kaleida.com)
  62. */
  63. if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
  64. ++pattern;
  65. need = 1;
  66. for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']' || need;) {
  67. need = 0;
  68. if (c == '/')
  69. return (void *)-1;
  70. if (c == '\\' && !(flags & FNM_NOESCAPE))
  71. c = FOLDCASE(*pattern++, flags);
  72. if (c == EOS)
  73. return NULL;
  74. if (*pattern == '-'
  75. && (c2 = FOLDCASE(*(pattern + 1), flags)) != EOS &&
  76. c2 != ']') {
  77. pattern += 2;
  78. if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  79. c2 = FOLDCASE(*pattern++, flags);
  80. if (c2 == EOS)
  81. return NULL;
  82. if (c <= test && test <= c2)
  83. ok = 1;
  84. } else if (c == test)
  85. ok = 1;
  86. }
  87. return ok == negate ? NULL : pattern;
  88. }
  89. static int fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
  90. {
  91. const char *stringstart, *r;
  92. char c, test;
  93. if ((pattern == NULL) || (string == NULL))
  94. {
  95. return FNM_NOMATCH;
  96. }
  97. if (recursion-- == 0)
  98. return FNM_NORES;
  99. for (stringstart = string;;) {
  100. switch (c = FOLDCASE(*pattern++, flags)) {
  101. case EOS:
  102. if ((flags & FNM_LEADING_DIR) && *string == '/')
  103. return 0;
  104. return *string == EOS ? 0 : FNM_NOMATCH;
  105. case '?':
  106. if (*string == EOS)
  107. return FNM_NOMATCH;
  108. if (*string == '/' && (flags & FNM_PATHNAME))
  109. return FNM_NOMATCH;
  110. if (*string == '.' && (flags & FNM_PERIOD) &&
  111. (string == stringstart ||
  112. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  113. return FNM_NOMATCH;
  114. ++string;
  115. break;
  116. case '*':
  117. c = FOLDCASE(*pattern, flags);
  118. /* Collapse multiple stars. */
  119. while (c == '*')
  120. c = FOLDCASE(*++pattern, flags);
  121. if (*string == '.' && (flags & FNM_PERIOD) &&
  122. (string == stringstart ||
  123. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  124. return FNM_NOMATCH;
  125. /* Optimize for pattern with * at end or before /. */
  126. if (c == EOS) {
  127. if (flags & FNM_PATHNAME)
  128. return (flags & FNM_LEADING_DIR) ||
  129. strchr(string, '/') == NULL ?
  130. 0 : FNM_NOMATCH;
  131. else
  132. return 0;
  133. } else if (c == '/' && flags & FNM_PATHNAME) {
  134. if ((string = strchr(string, '/')) == NULL)
  135. return FNM_NOMATCH;
  136. break;
  137. }
  138. /* General case, use recursion. */
  139. while ((test = FOLDCASE(*string, flags)) != EOS) {
  140. int e;
  141. switch ((e = fnmatchx(pattern, string,
  142. flags & ~FNM_PERIOD, recursion))) {
  143. case FNM_NOMATCH:
  144. break;
  145. default:
  146. return e;
  147. }
  148. if (test == '/' && flags & FNM_PATHNAME)
  149. break;
  150. ++string;
  151. }
  152. return FNM_NOMATCH;
  153. case '[':
  154. if (*string == EOS)
  155. return FNM_NOMATCH;
  156. if (*string == '/' && flags & FNM_PATHNAME)
  157. return FNM_NOMATCH;
  158. if ((r = rangematch(pattern,
  159. FOLDCASE(*string, flags), flags)) == NULL)
  160. return FNM_NOMATCH;
  161. if (r == (void *)-1) {
  162. if (*string != '[')
  163. return FNM_NOMATCH;
  164. } else
  165. pattern = r;
  166. ++string;
  167. break;
  168. case '\\':
  169. if (!(flags & FNM_NOESCAPE)) {
  170. if ((c = FOLDCASE(*pattern++, flags)) == EOS) {
  171. c = '\0';
  172. --pattern;
  173. }
  174. }
  175. /* FALLTHROUGH */
  176. default:
  177. if (c != FOLDCASE(*string++, flags))
  178. return FNM_NOMATCH;
  179. break;
  180. }
  181. }
  182. /* NOTREACHED */
  183. }
  184. int fnmatch(const char *pattern, const char *string, int flags)
  185. {
  186. return fnmatchx(pattern, string, flags, 64);
  187. }