fnmatch_old.c 5.4 KB

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