lib_match.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /****************************************************************************
  2. * libs/libc/misc/lib_match.c - simple shell-style filename matcher
  3. *
  4. * Simple shell-style filename pattern matcher written by Jef Poskanzer
  5. * This pattern matcher only handles '?', '*' and '**', and multiple
  6. * patterns separated by '|'.
  7. *
  8. * Copyright © 1995, 2000 by Jef Poskanzer <jef@mail.acme.com>.
  9. * All rights reserved.
  10. *
  11. * With extensions by Ken Pettit.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <string.h>
  40. #include <nuttx/lib/regex.h>
  41. /****************************************************************************
  42. * Private Functions
  43. ****************************************************************************/
  44. /****************************************************************************
  45. * Name: match_one
  46. *
  47. * Description:
  48. * Does all of the work for one '|' delimited pattern
  49. *
  50. * Returned Value:
  51. * Returns 1 (match) or 0 (no-match).
  52. *
  53. ****************************************************************************/
  54. static int match_one(FAR const char *pattern, int patlen,
  55. FAR const char *string)
  56. {
  57. const char *p;
  58. char first;
  59. int pl;
  60. int i;
  61. for (p = pattern; p - pattern < patlen; p++, string++)
  62. {
  63. if (*p == '?' && *string != '\0')
  64. {
  65. continue;
  66. }
  67. /* Match single character from a set: "[a-zA-Z]" for instance */
  68. if (*p == '[' && *string != '\0')
  69. {
  70. i = 0;
  71. while (*p != ']' && *p != '\0')
  72. {
  73. p++;
  74. if (*string == *p)
  75. {
  76. /* Match found. Advance to the ']' */
  77. i = 1;
  78. while (*p != ']' && *p != '\0')
  79. {
  80. p++;
  81. }
  82. break;
  83. }
  84. /* Prepare to test for range */
  85. if (*p != '\0')
  86. {
  87. first = *p++;
  88. if (*p == '-')
  89. {
  90. p++;
  91. if (*string >= first && *string <= *p)
  92. {
  93. /* Match found. Advance to the ']' */
  94. i = 1;
  95. while (*p != ']' && *p != '\0')
  96. {
  97. p++;
  98. }
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. /* We reuse 'i' above to indicate match found */
  105. if (i)
  106. {
  107. continue;
  108. }
  109. return 0;
  110. }
  111. if (*p == '*')
  112. {
  113. p++;
  114. if (*p == '*')
  115. {
  116. /* Double-wildcard matches anything. */
  117. p++;
  118. i = strlen(string);
  119. }
  120. else
  121. {
  122. /* Single-wildcard matches anything but slash. */
  123. i = strcspn(string, "/");
  124. }
  125. pl = patlen - (p - pattern);
  126. for (; i >= 0; i--)
  127. {
  128. if (match_one(p, pl, &(string[i])))
  129. {
  130. return 1;
  131. }
  132. }
  133. return 0;
  134. }
  135. if (*p != *string)
  136. {
  137. return 0;
  138. }
  139. }
  140. if (*string == '\0')
  141. {
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. /****************************************************************************
  147. * Public Functions
  148. ****************************************************************************/
  149. /****************************************************************************
  150. * Name: match
  151. *
  152. * Description:
  153. * Simple shell-style filename pattern matcher originally written by
  154. * Jef Poskanzer and extended by Ken Pettit. This pattern matcher handles
  155. * '?', '*', '**', sets like [a-zA-z], and multiple patterns separated
  156. * by '|'.
  157. *
  158. * Returned Value:
  159. * Returns 1 (match) or 0 (no-match).
  160. *
  161. ****************************************************************************/
  162. int match(FAR const char *pattern, FAR const char *string)
  163. {
  164. const char *or;
  165. for (; ; )
  166. {
  167. or = strchr(pattern, '|');
  168. if (or == (char *)0)
  169. {
  170. return match_one(pattern, strlen(pattern), string);
  171. }
  172. if (match_one(pattern, or - pattern, string))
  173. {
  174. return 1;
  175. }
  176. pattern = or + 1;
  177. }
  178. }