Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.1 KiB

  1. /* $Source: /u/mark/src/pax/RCS/wildmat.c,v $
  2. *
  3. * $Revision: 1.2 $
  4. *
  5. * wildmat.c - simple regular expression pattern matching routines
  6. *
  7. * DESCRIPTION
  8. *
  9. * These routines provide simple UNIX style regular expression matching.
  10. * They were originally written by Rich Salz, the comp.sources.unix
  11. * moderator for inclusion in some of his software. These routines
  12. * were released into the public domain and used by John Gilmore in
  13. * USTAR.
  14. *
  15. * AUTHORS
  16. *
  17. * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  18. * John Gilmore (gnu@hoptoad)
  19. * Rich Salz (rs@uunet.uu.net)
  20. *
  21. *
  22. * Sponsored by The USENIX Association for public distribution.
  23. *
  24. * Copyright (c) 1989 Mark H. Colburn.
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms are permitted
  28. * provided that the above copyright notice is duplicated in all such
  29. * forms and that any documentation, advertising materials, and other
  30. * materials related to such distribution and use acknowledge that the
  31. * software was developed * by Mark H. Colburn and sponsored by The
  32. * USENIX Association.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  35. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  36. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  37. *
  38. * $Log: wildmat.c,v $
  39. * Revision 1.2 89/02/12 10:06:20 mark
  40. * 1.2 release fixes
  41. *
  42. * Revision 1.1 88/12/23 18:02:41 mark
  43. * Initial revision
  44. *
  45. */
  46. #ifndef lint
  47. static char *ident = "$Id: wildmat.c,v 1.2 89/02/12 10:06:20 mark Exp $";
  48. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  49. #endif /* ! lint */
  50. /* Headers */
  51. #include "pax.h"
  52. /* Function Prototypes */
  53. #ifdef __STDC__
  54. static int star(char *, char *);
  55. #else /* !__STDC__ */
  56. static int star();
  57. #endif /* __STDC__ */
  58. /*
  59. * star - handle trailing * in a regular expression
  60. *
  61. * DESCRIPTION
  62. *
  63. * Star is used to match filename expansions containing a trailing
  64. * asterisk ('*'). Star call wildmat() to determine if the substring
  65. * passed to it is matches the regular expression.
  66. *
  67. * PARAMETERS
  68. *
  69. * char *source - The source string which is to be compared to the
  70. * regular expression pattern.
  71. * char *pattern - The regular expression which we are supposed to
  72. * match to.
  73. *
  74. * RETURNS
  75. *
  76. * Returns non-zero if the entire source string is completely matched by
  77. * the regular expression pattern, returns 0 otherwise. This is used to
  78. * see if *'s in a pattern matched the entire source string.
  79. *
  80. */
  81. #ifdef __STDC__
  82. static int star(char *source, char *pattern)
  83. #else
  84. static int star(source, pattern)
  85. char *source; /* source operand */
  86. char *pattern; /* regular expression to match */
  87. #endif
  88. {
  89. #ifdef DF_TRACE_DEBUG
  90. printf("DF_TRACE_DEBUG: static int star() in wildmat.c\n");
  91. #endif
  92. while (!wildmat(pattern, source)) {
  93. if (*++source == '\0') {
  94. return (0);
  95. }
  96. }
  97. return (1);
  98. }
  99. /*
  100. * wildmat - match a regular expression
  101. *
  102. * DESCRIPTION
  103. *
  104. * Wildmat attempts to match the string pointed to by source to the
  105. * regular expression pointed to by pattern. The subset of regular
  106. * expression syntax which is supported is defined by POSIX P1003.2
  107. * FILENAME EXPANSION rules.
  108. *
  109. * PARAMETERS
  110. *
  111. * char *pattern - The regular expression which we are supposed to
  112. * match to.
  113. * char *source - The source string which is to be compared to the
  114. * regular expression pattern.
  115. *
  116. * RETURNS
  117. *
  118. * Returns non-zero if the source string matches the regular expression
  119. * pattern specified, returns 0 otherwise.
  120. *
  121. */
  122. #ifdef __STDC__
  123. int wildmat(char *pattern, char *source)
  124. #else
  125. int wildmat(pattern, source)
  126. char *pattern; /* regular expression to match */
  127. char *source; /* source operand */
  128. #endif
  129. {
  130. int last; /* last character matched */
  131. int matched; /* !0 if a match occurred */
  132. int reverse; /* !0 if sense of match is reversed */
  133. #ifdef DF_TRACE_DEBUG
  134. printf("DF_TRACE_DEBUG: int wildmat() in wildmat.c\n");
  135. #endif
  136. for (; *pattern; source++, pattern++) {
  137. switch (*pattern) {
  138. case '\\':
  139. /* Literal match with following character */
  140. pattern++;
  141. /* FALLTHRU */
  142. default:
  143. if (*source != *pattern) {
  144. return (0);
  145. }
  146. continue;
  147. case '?':
  148. /* Match anything. */
  149. if (*source == '\0') {
  150. return (0);
  151. }
  152. continue;
  153. case '*':
  154. /* Trailing star matches everything. */
  155. return (*++pattern ? star(source, pattern) : 1);
  156. case '[':
  157. /* [^....] means inverse character class. */
  158. if (reverse = pattern[1] == '^') {
  159. pattern++;
  160. }
  161. for (last = 0400, matched = 0;
  162. *++pattern && *pattern != ']'; last = *pattern) {
  163. /* This next line requires a good C compiler. */
  164. if (*pattern == '-'
  165. ? *source <= *++pattern && *source >= last
  166. : *source == *pattern) {
  167. matched = 1;
  168. }
  169. }
  170. if (matched == reverse) {
  171. return (0);
  172. }
  173. continue;
  174. }
  175. }
  176. /*
  177. * For "tar" use, matches that end at a slash also work. --hoptoad!gnu
  178. */
  179. return (*source == '\0' || *source == '/');
  180. }