fnmatch.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public License as
  4. published by the Free Software Foundation; either version 2 of the
  5. License, or (at your option) any later version.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; see the file COPYING.LIB. If
  12. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  13. Cambridge, MA 02139, USA. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. /* Enable GNU extensions in fnmatch.h. */
  18. #ifndef _GNU_SOURCE
  19. # define _GNU_SOURCE 1
  20. #endif
  21. #include <errno.h>
  22. #include <fnmatch.h>
  23. #include <ctype.h>
  24. /* Comment out all this code if we are using the GNU C Library, and are not
  25. actually compiling the library itself. This code is part of the GNU C
  26. Library, but also included in many other GNU distributions. Compiling
  27. and linking in this code is a waste when using the GNU C library
  28. (especially if it is a shared library). Rather than having every GNU
  29. program understand `configure --with-gnu-libc' and omit the object files,
  30. it is simpler to just do this in the source for each such file. */
  31. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  32. # if defined (STDC_HEADERS) || !defined (isascii)
  33. # define ISASCII(c) 1
  34. # else
  35. # define ISASCII(c) isascii(c)
  36. # endif
  37. # define ISUPPER(c) (ISASCII (c) && isupper (c))
  38. /* Match STRING against the filename pattern PATTERN, returning zero if
  39. it matches, nonzero if not. */
  40. int attribute_hidden __fnmatch(const char *pattern, const char *string, int flags)
  41. {
  42. register const char *p = pattern, *n = string;
  43. register char c;
  44. /* Note that this evaluates C many times. */
  45. # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  46. while ((c = *p++) != '\0') {
  47. c = FOLD(c);
  48. switch (c) {
  49. case '?':
  50. if (*n == '\0')
  51. return FNM_NOMATCH;
  52. else if ((flags & FNM_FILE_NAME) && *n == '/')
  53. return FNM_NOMATCH;
  54. else if ((flags & FNM_PERIOD) && *n == '.' &&
  55. (n == string
  56. || ((flags & FNM_FILE_NAME)
  57. && n[-1] == '/'))) return FNM_NOMATCH;
  58. break;
  59. case '\\':
  60. if (!(flags & FNM_NOESCAPE)) {
  61. c = *p++;
  62. if (c == '\0')
  63. /* Trailing \ loses. */
  64. return FNM_NOMATCH;
  65. c = FOLD(c);
  66. }
  67. if (FOLD(*n) != c)
  68. return FNM_NOMATCH;
  69. break;
  70. case '*':
  71. if ((flags & FNM_PERIOD) && *n == '.' &&
  72. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  73. return FNM_NOMATCH;
  74. for (c = *p++; c == '?' || c == '*'; c = *p++) {
  75. if ((flags & FNM_FILE_NAME) && *n == '/')
  76. /* A slash does not match a wildcard under FNM_FILE_NAME. */
  77. return FNM_NOMATCH;
  78. else if (c == '?') {
  79. /* A ? needs to match one character. */
  80. if (*n == '\0')
  81. /* There isn't another character; no match. */
  82. return FNM_NOMATCH;
  83. else
  84. /* One character of the string is consumed in matching
  85. this ? wildcard, so *??? won't match if there are
  86. less than three characters. */
  87. ++n;
  88. }
  89. }
  90. if (c == '\0')
  91. return 0;
  92. {
  93. char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  94. c1 = FOLD(c1);
  95. for (--p; *n != '\0'; ++n)
  96. if ((c == '[' || FOLD(*n) == c1) &&
  97. __fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
  98. return 0;
  99. return FNM_NOMATCH;
  100. }
  101. case '[':
  102. {
  103. /* Nonzero if the sense of the character class is inverted. */
  104. register int not;
  105. if (*n == '\0')
  106. return FNM_NOMATCH;
  107. if ((flags & FNM_PERIOD) && *n == '.' &&
  108. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  109. return FNM_NOMATCH;
  110. not = (*p == '!' || *p == '^');
  111. if (not)
  112. ++p;
  113. c = *p++;
  114. for (;;) {
  115. register char cstart = c, cend = c;
  116. if (!(flags & FNM_NOESCAPE) && c == '\\') {
  117. if (*p == '\0')
  118. return FNM_NOMATCH;
  119. cstart = cend = *p++;
  120. }
  121. cstart = cend = FOLD(cstart);
  122. if (c == '\0')
  123. /* [ (unterminated) loses. */
  124. return FNM_NOMATCH;
  125. c = *p++;
  126. c = FOLD(c);
  127. if ((flags & FNM_FILE_NAME) && c == '/')
  128. /* [/] can never match. */
  129. return FNM_NOMATCH;
  130. if (c == '-' && *p != ']') {
  131. cend = *p++;
  132. if (!(flags & FNM_NOESCAPE) && cend == '\\')
  133. cend = *p++;
  134. if (cend == '\0')
  135. return FNM_NOMATCH;
  136. cend = FOLD(cend);
  137. c = *p++;
  138. }
  139. if (FOLD(*n) >= cstart && FOLD(*n) <= cend)
  140. goto matched;
  141. if (c == ']')
  142. break;
  143. }
  144. if (!not)
  145. return FNM_NOMATCH;
  146. break;
  147. matched:;
  148. /* Skip the rest of the [...] that already matched. */
  149. while (c != ']') {
  150. if (c == '\0')
  151. /* [... (unterminated) loses. */
  152. return FNM_NOMATCH;
  153. c = *p++;
  154. if (!(flags & FNM_NOESCAPE) && c == '\\') {
  155. if (*p == '\0')
  156. return FNM_NOMATCH;
  157. /* XXX 1003.2d11 is unclear if this is right. */
  158. ++p;
  159. }
  160. }
  161. if (not)
  162. return FNM_NOMATCH;
  163. }
  164. break;
  165. default:
  166. if (c != FOLD(*n))
  167. return FNM_NOMATCH;
  168. }
  169. ++n;
  170. }
  171. if (*n == '\0')
  172. return 0;
  173. if ((flags & FNM_LEADING_DIR) && *n == '/')
  174. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  175. return 0;
  176. return FNM_NOMATCH;
  177. # undef FOLD
  178. }
  179. strong_alias(__fnmatch,fnmatch)
  180. #endif /* _LIBC or not __GNU_LIBRARY__. */