fnmatch.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* $OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert 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. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. #if 0
  39. static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
  40. #else
  41. static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
  42. #endif
  43. #endif /* LIBC_SCCS and not lint */
  44. /*
  45. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  46. * Compares a filename or pathname to a pattern.
  47. */
  48. #include <libtar/config.h>
  49. #include <stdio.h>
  50. #ifdef STDC_HEADERS
  51. # include <string.h>
  52. #endif
  53. #ifdef HAVE_CTYPE_H
  54. # include <ctype.h>
  55. #endif
  56. #include <libtar/compat.h>
  57. #define EOS '\0'
  58. #define RANGE_MATCH 1
  59. #define RANGE_NOMATCH 0
  60. #define RANGE_ERROR (-1)
  61. static int rangematch (const char *, char, int, char **);
  62. int
  63. fnmatch(pattern, string, flags)
  64. const char *pattern, *string;
  65. int flags;
  66. {
  67. const char *stringstart;
  68. char *newp;
  69. char c, test;
  70. for (stringstart = string;;)
  71. switch (c = *pattern++) {
  72. case EOS:
  73. if ((flags & FNM_LEADING_DIR) && *string == '/')
  74. return (0);
  75. return (*string == EOS ? 0 : FNM_NOMATCH);
  76. case '?':
  77. if (*string == EOS)
  78. return (FNM_NOMATCH);
  79. if (*string == '/' && (flags & FNM_PATHNAME))
  80. return (FNM_NOMATCH);
  81. if (*string == '.' && (flags & FNM_PERIOD) &&
  82. (string == stringstart ||
  83. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  84. return (FNM_NOMATCH);
  85. ++string;
  86. break;
  87. case '*':
  88. c = *pattern;
  89. /* Collapse multiple stars. */
  90. while (c == '*')
  91. c = *++pattern;
  92. if (*string == '.' && (flags & FNM_PERIOD) &&
  93. (string == stringstart ||
  94. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  95. return (FNM_NOMATCH);
  96. /* Optimize for pattern with * at end or before /. */
  97. if (c == EOS) {
  98. if (flags & FNM_PATHNAME)
  99. return ((flags & FNM_LEADING_DIR) ||
  100. strchr(string, '/') == NULL ?
  101. 0 : FNM_NOMATCH);
  102. else
  103. return (0);
  104. } else if (c == '/' && (flags & FNM_PATHNAME)) {
  105. if ((string = strchr(string, '/')) == NULL)
  106. return (FNM_NOMATCH);
  107. break;
  108. }
  109. /* General case, use recursion. */
  110. while ((test = *string) != EOS) {
  111. if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  112. return (0);
  113. if (test == '/' && (flags & FNM_PATHNAME))
  114. break;
  115. ++string;
  116. }
  117. return (FNM_NOMATCH);
  118. case '[':
  119. if (*string == EOS)
  120. return (FNM_NOMATCH);
  121. if (*string == '/' && (flags & FNM_PATHNAME))
  122. return (FNM_NOMATCH);
  123. if (*string == '.' && (flags & FNM_PERIOD) &&
  124. (string == stringstart ||
  125. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  126. return (FNM_NOMATCH);
  127. switch (rangematch(pattern, *string, flags, &newp)) {
  128. case RANGE_ERROR:
  129. /* not a good range, treat as normal text */
  130. goto normal;
  131. case RANGE_MATCH:
  132. pattern = newp;
  133. break;
  134. case RANGE_NOMATCH:
  135. return (FNM_NOMATCH);
  136. }
  137. ++string;
  138. break;
  139. case '\\':
  140. if (!(flags & FNM_NOESCAPE)) {
  141. if ((c = *pattern++) == EOS) {
  142. c = '\\';
  143. --pattern;
  144. }
  145. }
  146. /* FALLTHROUGH */
  147. default:
  148. normal:
  149. if (c != *string && !((flags & FNM_CASEFOLD) &&
  150. (tolower((unsigned char)c) ==
  151. tolower((unsigned char)*string))))
  152. return (FNM_NOMATCH);
  153. ++string;
  154. break;
  155. }
  156. /* NOTREACHED */
  157. }
  158. static int
  159. rangematch(pattern, test, flags, newp)
  160. const char *pattern;
  161. char test;
  162. int flags;
  163. char **newp;
  164. {
  165. int negate, ok;
  166. char c, c2;
  167. /*
  168. * A bracket expression starting with an unquoted circumflex
  169. * character produces unspecified results (IEEE 1003.2-1992,
  170. * 3.13.2). This implementation treats it like '!', for
  171. * consistency with the regular expression syntax.
  172. * J.T. Conklin ([email protected])
  173. */
  174. negate = (*pattern == '!' || *pattern == '^');
  175. if (negate)
  176. ++pattern;
  177. if (flags & FNM_CASEFOLD)
  178. test = tolower((unsigned char)test);
  179. /*
  180. * A right bracket shall lose its special meaning and represent
  181. * itself in a bracket expression if it occurs first in the list.
  182. * -- POSIX.2 2.8.3.2
  183. */
  184. ok = 0;
  185. c = *pattern++;
  186. do {
  187. if (c == '\\' && !(flags & FNM_NOESCAPE))
  188. c = *pattern++;
  189. if (c == EOS)
  190. return (RANGE_ERROR);
  191. if (c == '/' && (flags & FNM_PATHNAME))
  192. return (RANGE_NOMATCH);
  193. if ((flags & FNM_CASEFOLD))
  194. c = tolower((unsigned char)c);
  195. if (*pattern == '-'
  196. && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  197. pattern += 2;
  198. if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  199. c2 = *pattern++;
  200. if (c2 == EOS)
  201. return (RANGE_ERROR);
  202. if (flags & FNM_CASEFOLD)
  203. c2 = tolower((unsigned char)c2);
  204. if (c <= test && test <= c2)
  205. ok = 1;
  206. } else if (c == test)
  207. ok = 1;
  208. } while ((c = *pattern++) != ']');
  209. *newp = (char *)pattern;
  210. return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
  211. }