pathmatch.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "lafe_platform.h"
  27. __FBSDID("$FreeBSD$");
  28. #ifdef HAVE_STRING_H
  29. #include <string.h>
  30. #endif
  31. #include "pathmatch.h"
  32. /*
  33. * Check whether a character 'c' is matched by a list specification [...]:
  34. * * Leading '!' negates the class.
  35. * * <char>-<char> is a range of characters
  36. * * \<char> removes any special meaning for <char>
  37. *
  38. * Some interesting boundary cases:
  39. * a-d-e is one range (a-d) followed by two single characters - and e.
  40. * \a-\d is same as a-d
  41. * a\-d is three single characters: a, d, -
  42. * Trailing - is not special (so [a-] is two characters a and -).
  43. * Initial - is not special ([a-] is same as [-a] is same as [\\-a])
  44. * This function never sees a trailing \.
  45. * [] always fails
  46. * [!] always succeeds
  47. */
  48. static int
  49. pm_list(const char *start, const char *end, const char c, int flags)
  50. {
  51. const char *p = start;
  52. char rangeStart = '\0', nextRangeStart;
  53. int match = 1, nomatch = 0;
  54. /* This will be used soon... */
  55. (void)flags; /* UNUSED */
  56. /* If this is a negated class, return success for nomatch. */
  57. if (*p == '!' && p < end) {
  58. match = 0;
  59. nomatch = 1;
  60. ++p;
  61. }
  62. while (p < end) {
  63. nextRangeStart = '\0';
  64. switch (*p) {
  65. case '-':
  66. /* Trailing or initial '-' is not special. */
  67. if ((rangeStart == '\0') || (p == end - 1)) {
  68. if (*p == c)
  69. return (match);
  70. } else {
  71. char rangeEnd = *++p;
  72. if (rangeEnd == '\\')
  73. rangeEnd = *++p;
  74. if ((rangeStart <= c) && (c <= rangeEnd))
  75. return (match);
  76. }
  77. break;
  78. case '\\':
  79. ++p;
  80. /* Fall through */
  81. default:
  82. if (*p == c)
  83. return (match);
  84. nextRangeStart = *p; /* Possible start of range. */
  85. }
  86. rangeStart = nextRangeStart;
  87. ++p;
  88. }
  89. return (nomatch);
  90. }
  91. /*
  92. * If s is pointing to "./", ".//", "./././" or the like, skip it.
  93. */
  94. static const char *
  95. pm_slashskip(const char *s) {
  96. while ((*s == '/')
  97. || (s[0] == '.' && s[1] == '/')
  98. || (s[0] == '.' && s[1] == '\0'))
  99. ++s;
  100. return (s);
  101. }
  102. static int
  103. pm(const char *p, const char *s, int flags)
  104. {
  105. const char *end;
  106. /*
  107. * Ignore leading './', './/', '././', etc.
  108. */
  109. if (s[0] == '.' && s[1] == '/')
  110. s = pm_slashskip(s + 1);
  111. if (p[0] == '.' && p[1] == '/')
  112. p = pm_slashskip(p + 1);
  113. for (;;) {
  114. switch (*p) {
  115. case '\0':
  116. if (s[0] == '/') {
  117. if (flags & PATHMATCH_NO_ANCHOR_END)
  118. return (1);
  119. /* "dir" == "dir/" == "dir/." */
  120. s = pm_slashskip(s);
  121. }
  122. return (*s == '\0');
  123. break;
  124. case '?':
  125. /* ? always succeds, unless we hit end of 's' */
  126. if (*s == '\0')
  127. return (0);
  128. break;
  129. case '*':
  130. /* "*" == "**" == "***" ... */
  131. while (*p == '*')
  132. ++p;
  133. /* Trailing '*' always succeeds. */
  134. if (*p == '\0')
  135. return (1);
  136. while (*s) {
  137. if (lafe_pathmatch(p, s, flags))
  138. return (1);
  139. ++s;
  140. }
  141. return (0);
  142. break;
  143. case '[':
  144. /*
  145. * Find the end of the [...] character class,
  146. * ignoring \] that might occur within the class.
  147. */
  148. end = p + 1;
  149. while (*end != '\0' && *end != ']') {
  150. if (*end == '\\' && end[1] != '\0')
  151. ++end;
  152. ++end;
  153. }
  154. if (*end == ']') {
  155. /* We found [...], try to match it. */
  156. if (!pm_list(p + 1, end, *s, flags))
  157. return (0);
  158. p = end; /* Jump to trailing ']' char. */
  159. break;
  160. } else
  161. /* No final ']', so just match '['. */
  162. if (*p != *s)
  163. return (0);
  164. break;
  165. case '\\':
  166. /* Trailing '\\' matches itself. */
  167. if (p[1] == '\0') {
  168. if (*s != '\\')
  169. return (0);
  170. } else {
  171. ++p;
  172. if (*p != *s)
  173. return (0);
  174. }
  175. break;
  176. case '/':
  177. if (*s != '/' && *s != '\0')
  178. return (0);
  179. /* Note: pattern "/\./" won't match "/";
  180. * pm_slashskip() correctly stops at backslash. */
  181. p = pm_slashskip(p);
  182. s = pm_slashskip(s);
  183. if (*p == '\0' && (flags & PATHMATCH_NO_ANCHOR_END))
  184. return (1);
  185. --p; /* Counteract the increment below. */
  186. --s;
  187. break;
  188. case '$':
  189. /* '$' is special only at end of pattern and only
  190. * if PATHMATCH_NO_ANCHOR_END is specified. */
  191. if (p[1] == '\0' && (flags & PATHMATCH_NO_ANCHOR_END)){
  192. /* "dir" == "dir/" == "dir/." */
  193. return (*pm_slashskip(s) == '\0');
  194. }
  195. /* Otherwise, '$' is not special. */
  196. /* FALL THROUGH */
  197. default:
  198. if (*p != *s)
  199. return (0);
  200. break;
  201. }
  202. ++p;
  203. ++s;
  204. }
  205. }
  206. /* Main entry point. */
  207. int
  208. lafe_pathmatch(const char *p, const char *s, int flags)
  209. {
  210. /* Empty pattern only matches the empty string. */
  211. if (p == NULL || *p == '\0')
  212. return (s == NULL || *s == '\0');
  213. /* Leading '^' anchors the start of the pattern. */
  214. if (*p == '^') {
  215. ++p;
  216. flags &= ~PATHMATCH_NO_ANCHOR_START;
  217. }
  218. if (*p == '/' && *s != '/')
  219. return (0);
  220. /* Certain patterns and file names anchor implicitly. */
  221. if (*p == '*' || *p == '/' || *p == '/') {
  222. while (*p == '/')
  223. ++p;
  224. while (*s == '/')
  225. ++s;
  226. return (pm(p, s, flags));
  227. }
  228. /* If start is unanchored, try to match start of each path element. */
  229. if (flags & PATHMATCH_NO_ANCHOR_START) {
  230. for ( ; s != NULL; s = strchr(s, '/')) {
  231. if (*s == '/')
  232. s++;
  233. if (pm(p, s, flags))
  234. return (1);
  235. }
  236. return (0);
  237. }
  238. /* Default: Match from beginning. */
  239. return (pm(p, s, flags));
  240. }