fnmatch.c 5.8 KB

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