fnmatch_old.c 5.5 KB

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