fnmatch_old.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, see <http://www.gnu.org/licenses/>. */
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16. #include <errno.h>
  17. #include <fnmatch.h>
  18. #include <ctype.h>
  19. /* Comment out all this code if we are using the GNU C Library, and are not
  20. actually compiling the library itself. This code is part of the GNU C
  21. Library, but also included in many other GNU distributions. Compiling
  22. and linking in this code is a waste when using the GNU C library
  23. (especially if it is a shared library). Rather than having every GNU
  24. program understand `configure --with-gnu-libc' and omit the object files,
  25. it is simpler to just do this in the source for each such file. */
  26. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  27. # if defined (STDC_HEADERS) || !defined (isascii)
  28. # define ISASCII(c) 1
  29. # else
  30. # define ISASCII(c) isascii(c)
  31. # endif
  32. # define ISUPPER(c) (ISASCII (c) && isupper (c))
  33. /* Match STRING against the filename pattern PATTERN, returning zero if
  34. it matches, nonzero if not. */
  35. int fnmatch(const char *pattern, const char *string, int flags)
  36. {
  37. register const char *p = pattern, *n = string;
  38. register char c;
  39. /* Note that this evaluates C many times. */
  40. # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  41. while ((c = *p++) != '\0') {
  42. c = FOLD(c);
  43. switch (c) {
  44. case '?':
  45. if (*n == '\0')
  46. return FNM_NOMATCH;
  47. else if ((flags & FNM_FILE_NAME) && *n == '/')
  48. return FNM_NOMATCH;
  49. else if ((flags & FNM_PERIOD) && *n == '.' &&
  50. (n == string
  51. || ((flags & FNM_FILE_NAME)
  52. && n[-1] == '/'))) return FNM_NOMATCH;
  53. break;
  54. case '\\':
  55. if (!(flags & FNM_NOESCAPE)) {
  56. c = *p++;
  57. if (c == '\0')
  58. /* Trailing \ loses. */
  59. return FNM_NOMATCH;
  60. c = FOLD(c);
  61. }
  62. if (FOLD(*n) != c)
  63. return FNM_NOMATCH;
  64. break;
  65. case '*':
  66. if ((flags & FNM_PERIOD) && *n == '.' &&
  67. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  68. return FNM_NOMATCH;
  69. for (c = *p++; c == '?' || c == '*'; c = *p++) {
  70. if ((flags & FNM_FILE_NAME) && *n == '/')
  71. /* A slash does not match a wildcard under FNM_FILE_NAME. */
  72. return FNM_NOMATCH;
  73. else if (c == '?') {
  74. /* A ? needs to match one character. */
  75. if (*n == '\0')
  76. /* There isn't another character; no match. */
  77. return FNM_NOMATCH;
  78. else
  79. /* One character of the string is consumed in matching
  80. this ? wildcard, so *??? won't match if there are
  81. less than three characters. */
  82. ++n;
  83. }
  84. }
  85. if (c == '\0')
  86. return 0;
  87. {
  88. char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  89. c1 = FOLD(c1);
  90. for (--p; *n != '\0'; ++n)
  91. if ((c == '[' || FOLD(*n) == c1) &&
  92. fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
  93. return 0;
  94. return FNM_NOMATCH;
  95. }
  96. case '[':
  97. {
  98. /* Nonzero if the sense of the character class is inverted. */
  99. register int not;
  100. if (*n == '\0')
  101. return FNM_NOMATCH;
  102. if ((flags & FNM_PERIOD) && *n == '.' &&
  103. (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  104. return FNM_NOMATCH;
  105. not = (*p == '!' || *p == '^');
  106. if (not)
  107. ++p;
  108. c = *p++;
  109. for (;;) {
  110. register char cstart = c, cend = c;
  111. if (!(flags & FNM_NOESCAPE) && c == '\\') {
  112. if (*p == '\0')
  113. return FNM_NOMATCH;
  114. cstart = cend = *p++;
  115. }
  116. cstart = cend = FOLD(cstart);
  117. if (c == '\0')
  118. /* [ (unterminated) loses. */
  119. return FNM_NOMATCH;
  120. c = *p++;
  121. c = FOLD(c);
  122. if ((flags & FNM_FILE_NAME) && c == '/')
  123. /* [/] can never match. */
  124. return FNM_NOMATCH;
  125. if (c == '-' && *p != ']') {
  126. cend = *p++;
  127. if (!(flags & FNM_NOESCAPE) && cend == '\\')
  128. cend = *p++;
  129. if (cend == '\0')
  130. return FNM_NOMATCH;
  131. cend = FOLD(cend);
  132. c = *p++;
  133. }
  134. if (FOLD(*n) >= cstart && FOLD(*n) <= cend)
  135. goto matched;
  136. if (c == ']')
  137. break;
  138. }
  139. if (!not)
  140. return FNM_NOMATCH;
  141. break;
  142. matched:;
  143. /* Skip the rest of the [...] that already matched. */
  144. while (c != ']') {
  145. if (c == '\0')
  146. /* [... (unterminated) loses. */
  147. return FNM_NOMATCH;
  148. c = *p++;
  149. if (!(flags & FNM_NOESCAPE) && c == '\\') {
  150. if (*p == '\0')
  151. return FNM_NOMATCH;
  152. /* XXX 1003.2d11 is unclear if this is right. */
  153. ++p;
  154. }
  155. }
  156. if (not)
  157. return FNM_NOMATCH;
  158. }
  159. break;
  160. default:
  161. if (c != FOLD(*n))
  162. return FNM_NOMATCH;
  163. }
  164. ++n;
  165. }
  166. if (*n == '\0')
  167. return 0;
  168. if ((flags & FNM_LEADING_DIR) && *n == '/')
  169. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  170. return 0;
  171. return FNM_NOMATCH;
  172. # undef FOLD
  173. }
  174. libc_hidden_def(fnmatch)
  175. #endif /* _LIBC or not __GNU_LIBRARY__. */