fnmatch.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifdef NO_IBM_COMPILER_HORKAGE
  62. static int rangematch (const char *, char, int, char **);
  63. #else
  64. static int rangematch ();
  65. #endif
  66. int
  67. fnmatch(pattern, string, flags)
  68. const char *pattern, *string;
  69. int flags;
  70. {
  71. const char *stringstart;
  72. char *newp;
  73. char c, test;
  74. for (stringstart = string;;)
  75. switch (c = *pattern++) {
  76. case EOS:
  77. if ((flags & FNM_LEADING_DIR) && *string == '/')
  78. return (0);
  79. return (*string == EOS ? 0 : FNM_NOMATCH);
  80. case '?':
  81. if (*string == EOS)
  82. return (FNM_NOMATCH);
  83. if (*string == '/' && (flags & FNM_PATHNAME))
  84. return (FNM_NOMATCH);
  85. if (*string == '.' && (flags & FNM_PERIOD) &&
  86. (string == stringstart ||
  87. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  88. return (FNM_NOMATCH);
  89. ++string;
  90. break;
  91. case '*':
  92. c = *pattern;
  93. /* Collapse multiple stars. */
  94. while (c == '*')
  95. c = *++pattern;
  96. if (*string == '.' && (flags & FNM_PERIOD) &&
  97. (string == stringstart ||
  98. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  99. return (FNM_NOMATCH);
  100. /* Optimize for pattern with * at end or before /. */
  101. if (c == EOS) {
  102. if (flags & FNM_PATHNAME)
  103. return ((flags & FNM_LEADING_DIR) ||
  104. strchr(string, '/') == NULL ?
  105. 0 : FNM_NOMATCH);
  106. else
  107. return (0);
  108. } else if (c == '/' && (flags & FNM_PATHNAME)) {
  109. if ((string = strchr(string, '/')) == NULL)
  110. return (FNM_NOMATCH);
  111. break;
  112. }
  113. /* General case, use recursion. */
  114. while ((test = *string) != EOS) {
  115. if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  116. return (0);
  117. if (test == '/' && (flags & FNM_PATHNAME))
  118. break;
  119. ++string;
  120. }
  121. return (FNM_NOMATCH);
  122. case '[':
  123. if (*string == EOS)
  124. return (FNM_NOMATCH);
  125. if (*string == '/' && (flags & FNM_PATHNAME))
  126. return (FNM_NOMATCH);
  127. if (*string == '.' && (flags & FNM_PERIOD) &&
  128. (string == stringstart ||
  129. ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  130. return (FNM_NOMATCH);
  131. switch (rangematch(pattern, *string, flags, &newp)) {
  132. case RANGE_ERROR:
  133. /* not a good range, treat as normal text */
  134. goto normal;
  135. case RANGE_MATCH:
  136. pattern = newp;
  137. break;
  138. case RANGE_NOMATCH:
  139. return (FNM_NOMATCH);
  140. }
  141. ++string;
  142. break;
  143. case '\\':
  144. if (!(flags & FNM_NOESCAPE)) {
  145. if ((c = *pattern++) == EOS) {
  146. c = '\\';
  147. --pattern;
  148. }
  149. }
  150. /* FALLTHROUGH */
  151. default:
  152. normal:
  153. if (c != *string && !((flags & FNM_CASEFOLD) &&
  154. (tolower((unsigned char)c) ==
  155. tolower((unsigned char)*string))))
  156. return (FNM_NOMATCH);
  157. ++string;
  158. break;
  159. }
  160. /* NOTREACHED */
  161. }
  162. static int
  163. rangematch(pattern, test, flags, newp)
  164. const char *pattern;
  165. char test;
  166. int flags;
  167. char **newp;
  168. {
  169. int negate, ok;
  170. char c, c2;
  171. /*
  172. * A bracket expression starting with an unquoted circumflex
  173. * character produces unspecified results (IEEE 1003.2-1992,
  174. * 3.13.2). This implementation treats it like '!', for
  175. * consistency with the regular expression syntax.
  176. * J.T. Conklin ([email protected])
  177. */
  178. if ((negate = (*pattern == '!' || *pattern == '^')))
  179. ++pattern;
  180. if (flags & FNM_CASEFOLD)
  181. test = tolower((unsigned char)test);
  182. /*
  183. * A right bracket shall lose its special meaning and represent
  184. * itself in a bracket expression if it occurs first in the list.
  185. * -- POSIX.2 2.8.3.2
  186. */
  187. ok = 0;
  188. c = *pattern++;
  189. do {
  190. if (c == '\\' && !(flags & FNM_NOESCAPE))
  191. c = *pattern++;
  192. if (c == EOS)
  193. return (RANGE_ERROR);
  194. if (c == '/' && (flags & FNM_PATHNAME))
  195. return (RANGE_NOMATCH);
  196. if ((flags & FNM_CASEFOLD))
  197. c = tolower((unsigned char)c);
  198. if (*pattern == '-'
  199. && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  200. pattern += 2;
  201. if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  202. c2 = *pattern++;
  203. if (c2 == EOS)
  204. return (RANGE_ERROR);
  205. if (flags & FNM_CASEFOLD)
  206. c2 = tolower((unsigned char)c2);
  207. if (c <= test && test <= c2)
  208. ok = 1;
  209. } else if (c == test)
  210. ok = 1;
  211. } while ((c = *pattern++) != ']');
  212. *newp = (char *)pattern;
  213. return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
  214. }