fnmatch.c 6.9 KB

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