fnmatch.c 5.8 KB

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