fnmatch_old.c 5.3 KB

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