fnmatch.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  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. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. /*
  37. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  38. * Compares a filename or pathname to a pattern.
  39. */
  40. #include <sys/param.h>
  41. #include <sys/ctype.h>
  42. #include <sys/libkern.h>
  43. #define EOS '\0'
  44. #define RANGE_MATCH 1
  45. #define RANGE_NOMATCH 0
  46. #define RANGE_ERROR (-1)
  47. static int rangematch(const char *, char, int, char **);
  48. int
  49. fnmatch(const char *pattern, const char *string, int flags)
  50. {
  51. const char *stringstart;
  52. char *newp;
  53. char c, test;
  54. for (stringstart = string;;)
  55. switch (c = *pattern++) {
  56. case EOS:
  57. if ((flags & FNM_LEADING_DIR) && *string == '/')
  58. return (0);
  59. return (*string == EOS ? 0 : FNM_NOMATCH);
  60. case '?':
  61. if (*string == EOS)
  62. return (FNM_NOMATCH);
  63. if (*string == '/' && (flags & FNM_PATHNAME))
  64. return (FNM_NOMATCH);
  65. if (*string == '.' && (flags & FNM_PERIOD) &&
  66. (string == stringstart ||
  67. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  68. return (FNM_NOMATCH);
  69. ++string;
  70. break;
  71. case '*':
  72. c = *pattern;
  73. /* Collapse multiple stars. */
  74. while (c == '*')
  75. c = *++pattern;
  76. if (*string == '.' && (flags & FNM_PERIOD) &&
  77. (string == stringstart ||
  78. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  79. return (FNM_NOMATCH);
  80. /* Optimize for pattern with * at end or before /. */
  81. if (c == EOS)
  82. if (flags & FNM_PATHNAME)
  83. return ((flags & FNM_LEADING_DIR) ||
  84. strchr(string, '/') == NULL ?
  85. 0 : FNM_NOMATCH);
  86. else
  87. return (0);
  88. else if (c == '/' && flags & FNM_PATHNAME) {
  89. if ((string = strchr(string, '/')) == NULL)
  90. return (FNM_NOMATCH);
  91. break;
  92. }
  93. /* General case, use recursion. */
  94. while ((test = *string) != EOS) {
  95. if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  96. return (0);
  97. if (test == '/' && flags & FNM_PATHNAME)
  98. break;
  99. ++string;
  100. }
  101. return (FNM_NOMATCH);
  102. case '[':
  103. if (*string == EOS)
  104. return (FNM_NOMATCH);
  105. if (*string == '/' && (flags & FNM_PATHNAME))
  106. return (FNM_NOMATCH);
  107. if (*string == '.' && (flags & FNM_PERIOD) &&
  108. (string == stringstart ||
  109. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  110. return (FNM_NOMATCH);
  111. switch (rangematch(pattern, *string, flags, &newp)) {
  112. case RANGE_ERROR:
  113. goto norm;
  114. case RANGE_MATCH:
  115. pattern = newp;
  116. break;
  117. case RANGE_NOMATCH:
  118. return (FNM_NOMATCH);
  119. }
  120. ++string;
  121. break;
  122. case '\\':
  123. if (!(flags & FNM_NOESCAPE)) {
  124. if ((c = *pattern++) == EOS) {
  125. c = '\\';
  126. --pattern;
  127. }
  128. }
  129. /* FALLTHROUGH */
  130. default:
  131. norm:
  132. if (c == *string)
  133. ;
  134. else if ((flags & FNM_CASEFOLD) &&
  135. (tolower((unsigned char)c) ==
  136. tolower((unsigned char)*string)))
  137. ;
  138. else
  139. return (FNM_NOMATCH);
  140. string++;
  141. break;
  142. }
  143. /* NOTREACHED */
  144. }
  145. static int
  146. rangematch(const char *pattern, char test, int flags, char **newp)
  147. {
  148. int negate, ok;
  149. char c, c2;
  150. /*
  151. * A bracket expression starting with an unquoted circumflex
  152. * character produces unspecified results (IEEE 1003.2-1992,
  153. * 3.13.2). This implementation treats it like '!', for
  154. * consistency with the regular expression syntax.
  155. * J.T. Conklin (conklin@ngai.kaleida.com)
  156. */
  157. if ( (negate = (*pattern == '!' || *pattern == '^')) )
  158. ++pattern;
  159. if (flags & FNM_CASEFOLD)
  160. test = tolower((unsigned char)test);
  161. /*
  162. * A right bracket shall lose its special meaning and represent
  163. * itself in a bracket expression if it occurs first in the list.
  164. * -- POSIX.2 2.8.3.2
  165. */
  166. ok = 0;
  167. c = *pattern++;
  168. do {
  169. if (c == '\\' && !(flags & FNM_NOESCAPE))
  170. c = *pattern++;
  171. if (c == EOS)
  172. return (RANGE_ERROR);
  173. if (c == '/' && (flags & FNM_PATHNAME))
  174. return (RANGE_NOMATCH);
  175. if (flags & FNM_CASEFOLD)
  176. c = tolower((unsigned char)c);
  177. if (*pattern == '-'
  178. && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  179. pattern += 2;
  180. if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  181. c2 = *pattern++;
  182. if (c2 == EOS)
  183. return (RANGE_ERROR);
  184. if (flags & FNM_CASEFOLD)
  185. c2 = tolower((unsigned char)c2);
  186. if (c <= test && test <= c2)
  187. ok = 1;
  188. } else if (c == test)
  189. ok = 1;
  190. } while ((c = *pattern++) != ']');
  191. *newp = (char *)(uintptr_t)pattern;
  192. return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
  193. }