fspr_fnmatch.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 1989, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Guido van Rossum.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. /*
  40. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  41. * Compares a filename or pathname to a pattern.
  42. */
  43. #ifndef WIN32
  44. #include "fspr_private.h"
  45. #endif
  46. #include "fspr_file_info.h"
  47. #include "fspr_fnmatch.h"
  48. #include "fspr_tables.h"
  49. #include "fspr_lib.h"
  50. #include "fspr_strings.h"
  51. #include <string.h>
  52. #if APR_HAVE_CTYPE_H
  53. # include <ctype.h>
  54. #endif
  55. #define EOS '\0'
  56. static const char *rangematch(const char *, int, int);
  57. APR_DECLARE(fspr_status_t) fspr_fnmatch(const char *pattern, const char *string, int flags)
  58. {
  59. const char *stringstart;
  60. char c, test;
  61. for (stringstart = string;;) {
  62. switch (c = *pattern++) {
  63. case EOS:
  64. return (*string == EOS ? APR_SUCCESS : APR_FNM_NOMATCH);
  65. case '?':
  66. if (*string == EOS) {
  67. return (APR_FNM_NOMATCH);
  68. }
  69. if (*string == '/' && (flags & APR_FNM_PATHNAME)) {
  70. return (APR_FNM_NOMATCH);
  71. }
  72. if (*string == '.' && (flags & APR_FNM_PERIOD) &&
  73. (string == stringstart ||
  74. ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
  75. return (APR_FNM_NOMATCH);
  76. }
  77. ++string;
  78. break;
  79. case '*':
  80. c = *pattern;
  81. /* Collapse multiple stars. */
  82. while (c == '*') {
  83. c = *++pattern;
  84. }
  85. if (*string == '.' && (flags & APR_FNM_PERIOD) &&
  86. (string == stringstart ||
  87. ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
  88. return (APR_FNM_NOMATCH);
  89. }
  90. /* Optimize for pattern with * at end or before /. */
  91. if (c == EOS) {
  92. if (flags & APR_FNM_PATHNAME) {
  93. return (strchr(string, '/') == NULL ? APR_SUCCESS : APR_FNM_NOMATCH);
  94. }
  95. else {
  96. return (APR_SUCCESS);
  97. }
  98. }
  99. else if (c == '/' && flags & APR_FNM_PATHNAME) {
  100. if ((string = strchr(string, '/')) == NULL) {
  101. return (APR_FNM_NOMATCH);
  102. }
  103. break;
  104. }
  105. /* General case, use recursion. */
  106. while ((test = *string) != EOS) {
  107. if (!fspr_fnmatch(pattern, string, flags & ~APR_FNM_PERIOD)) {
  108. return (APR_SUCCESS);
  109. }
  110. if (test == '/' && flags & APR_FNM_PATHNAME) {
  111. break;
  112. }
  113. ++string;
  114. }
  115. return (APR_FNM_NOMATCH);
  116. case '[':
  117. if (*string == EOS) {
  118. return (APR_FNM_NOMATCH);
  119. }
  120. if (*string == '/' && flags & APR_FNM_PATHNAME) {
  121. return (APR_FNM_NOMATCH);
  122. }
  123. if (*string == '.' && (flags & APR_FNM_PERIOD) &&
  124. (string == stringstart ||
  125. ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
  126. return (APR_FNM_NOMATCH);
  127. }
  128. if ((pattern = rangematch(pattern, *string, flags)) == NULL) {
  129. return (APR_FNM_NOMATCH);
  130. }
  131. ++string;
  132. break;
  133. case '\\':
  134. if (!(flags & APR_FNM_NOESCAPE)) {
  135. if ((c = *pattern++) == EOS) {
  136. c = '\\';
  137. --pattern;
  138. }
  139. }
  140. /* FALLTHROUGH */
  141. default:
  142. if (flags & APR_FNM_CASE_BLIND) {
  143. if (fspr_tolower(c) != fspr_tolower(*string)) {
  144. return (APR_FNM_NOMATCH);
  145. }
  146. }
  147. else if (c != *string) {
  148. return (APR_FNM_NOMATCH);
  149. }
  150. string++;
  151. break;
  152. }
  153. /* NOTREACHED */
  154. }
  155. }
  156. static const char *rangematch(const char *pattern, int test, int flags)
  157. {
  158. int negate, ok;
  159. char c, c2;
  160. /*
  161. * A bracket expression starting with an unquoted circumflex
  162. * character produces unspecified results (IEEE 1003.2-1992,
  163. * 3.13.2). This implementation treats it like '!', for
  164. * consistency with the regular expression syntax.
  165. * J.T. Conklin (conklin@ngai.kaleida.com)
  166. */
  167. if ((negate = (*pattern == '!' || *pattern == '^'))) {
  168. ++pattern;
  169. }
  170. for (ok = 0; (c = *pattern++) != ']';) {
  171. if (c == '\\' && !(flags & APR_FNM_NOESCAPE)) {
  172. c = *pattern++;
  173. }
  174. if (c == EOS) {
  175. return (NULL);
  176. }
  177. if (*pattern == '-' && (c2 = *(pattern + 1)) != EOS && c2 != ']') {
  178. pattern += 2;
  179. if (c2 == '\\' && !(flags & APR_FNM_NOESCAPE)) {
  180. c2 = *pattern++;
  181. }
  182. if (c2 == EOS) {
  183. return (NULL);
  184. }
  185. if ((c <= test && test <= c2)
  186. || ((flags & APR_FNM_CASE_BLIND)
  187. && ((fspr_tolower(c) <= fspr_tolower(test))
  188. && (fspr_tolower(test) <= fspr_tolower(c2))))) {
  189. ok = 1;
  190. }
  191. }
  192. else if ((c == test)
  193. || ((flags & APR_FNM_CASE_BLIND)
  194. && (fspr_tolower(c) == fspr_tolower(test)))) {
  195. ok = 1;
  196. }
  197. }
  198. return (ok == negate ? NULL : pattern);
  199. }
  200. /* This function is an Apache addition */
  201. /* return non-zero if pattern has any glob chars in it */
  202. APR_DECLARE(int) fspr_fnmatch_test(const char *pattern)
  203. {
  204. int nesting;
  205. nesting = 0;
  206. while (*pattern) {
  207. switch (*pattern) {
  208. case '?':
  209. case '*':
  210. return 1;
  211. case '\\':
  212. if (*pattern++ == '\0') {
  213. return 0;
  214. }
  215. break;
  216. case '[': /* '[' is only a glob if it has a matching ']' */
  217. ++nesting;
  218. break;
  219. case ']':
  220. if (nesting) {
  221. return 1;
  222. }
  223. break;
  224. }
  225. ++pattern;
  226. }
  227. return 0;
  228. }
  229. /* Find all files matching the specified pattern */
  230. APR_DECLARE(fspr_status_t) fspr_match_glob(const char *pattern,
  231. fspr_array_header_t **result,
  232. fspr_pool_t *p)
  233. {
  234. fspr_dir_t *dir;
  235. fspr_finfo_t finfo;
  236. fspr_status_t rv;
  237. char *path;
  238. /* XXX So, this is kind of bogus. Basically, I need to strip any leading
  239. * directories off the pattern, but there is no portable way to do that.
  240. * So, for now we just find the last occurance of '/' and if that doesn't
  241. * return anything, then we look for '\'. This means that we could
  242. * screw up on unix if the pattern is something like "foo\.*" That '\'
  243. * isn't a directory delimiter, it is a part of the filename. To fix this,
  244. * we really need fspr_filepath_basename, which will be coming as soon as
  245. * I get to it. rbb
  246. */
  247. char *idx = strrchr(pattern, '/');
  248. if (idx == NULL) {
  249. idx = strrchr(pattern, '\\');
  250. }
  251. if (idx == NULL) {
  252. path = ".";
  253. }
  254. else {
  255. path = fspr_pstrndup(p, pattern, idx - pattern);
  256. pattern = idx + 1;
  257. }
  258. *result = fspr_array_make(p, 0, sizeof(char *));
  259. rv = fspr_dir_open(&dir, path, p);
  260. if (rv != APR_SUCCESS) {
  261. return rv;
  262. }
  263. while (fspr_dir_read(&finfo, APR_FINFO_NAME, dir) == APR_SUCCESS) {
  264. if (fspr_fnmatch(pattern, finfo.name, 0) == APR_SUCCESS) {
  265. *(const char **)fspr_array_push(*result) = fspr_pstrdup(p, finfo.name);
  266. }
  267. }
  268. fspr_dir_close(dir);
  269. return APR_SUCCESS;
  270. }