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.

174 lines
5.0 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /******************************************************************************
  3. *
  4. * UMATCH.C
  5. *
  6. * The unix_match() function, performing unix style wild-card matching on
  7. * a given file name.
  8. *
  9. *
  10. ******************************************************************************/
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #define TRUE 1
  15. #define FALSE 0
  16. /******************************************************************************
  17. *
  18. * unix_match()
  19. *
  20. * Check specified filename (found_file) to see if it matches
  21. * the filename with global characters (with globals).
  22. * Uses unix style wild-card matching.
  23. *
  24. * EXIT:
  25. * TRUE -- the specified filename matched the filename with wildcards
  26. * FALSE -- the specified filename did not match
  27. *
  28. ******************************************************************************/
  29. int
  30. unix_match(
  31. WCHAR *with_globals, /* the file with possible global characters */
  32. WCHAR *found_file ) /* file name returned - no globals in it */
  33. {
  34. WCHAR *c1, *c2, *start_c1, *start_c2, *sav_c1, *sav_c2;
  35. WCHAR ch, ch2;
  36. int i, j, k, char_ok, match, want_match;
  37. /*
  38. * Play with filename so that blanks are removed.
  39. */
  40. j = k = 0;
  41. for (i=0; found_file[i]!=L'\0'; ++i) {
  42. if (found_file[i] == L' ') {
  43. if (j == 0) {
  44. j = i;
  45. } else {
  46. found_file[i] = L'\0';
  47. }
  48. } else if (found_file[i] == L'.') {
  49. k = i;
  50. }
  51. }
  52. if (j && k) {
  53. wcscpy(&found_file[j], &found_file[k]);
  54. }
  55. /*
  56. * If Search name is just "*", simply return success now.
  57. */
  58. if (with_globals[0]==L'*' && with_globals[1]==L'\0') {
  59. return TRUE;
  60. }
  61. #ifdef DEBUG
  62. wprintf("unix_match: search=%s: found=%s:\n", with_globals, found_file);
  63. #endif
  64. /*
  65. * Now compare the 2 filenames to see if we have a match.
  66. */
  67. c1 = with_globals,
  68. c2 = found_file;
  69. start_c1 = sav_c1 = NULL;
  70. while (*c2!=L'\0') {
  71. char_ok = FALSE;
  72. switch (*c1) {
  73. case L'\0':
  74. break;
  75. case '*':
  76. while (*++c1 == L'*') ; /* skip consecutive '*'s */
  77. if (*c1 == L'\0') { /* if we reached the end, we match */
  78. return TRUE;
  79. }
  80. start_c1 = c1; /* remember where '*' was and where */
  81. start_c2 = c2; /* we were in filename string */
  82. sav_c1 = NULL;
  83. char_ok = TRUE;
  84. break;
  85. case L'?':
  86. ++c1; ++c2;
  87. char_ok = TRUE;
  88. break;
  89. case L'[':
  90. if (!sav_c1) {
  91. sav_c1 = c1;
  92. sav_c2 = c2;
  93. }
  94. match = FALSE;
  95. want_match = TRUE;
  96. if (*++c1 == L'!') {
  97. ++c1;
  98. want_match = FALSE;
  99. }
  100. while ((ch=*c1) && ch != L']') { /* BJP */
  101. if (c1[1] == L'-') {
  102. ch2 = *c2;
  103. if (ch<=ch2 && c1[2]>=ch2) {
  104. match = TRUE;
  105. break;
  106. }
  107. ++c1; ++c1; /* skip '-' and following char */
  108. } else if (ch == *c2) {
  109. match = TRUE;
  110. break;
  111. }
  112. ++c1;
  113. }
  114. if (want_match) {
  115. if (match) {
  116. while ((ch=*c1++) && ch != L']') ; /* BJP */
  117. ++c2;
  118. char_ok = TRUE;
  119. } else if (!start_c1) {
  120. return FALSE;
  121. }
  122. } else /*!want_match*/ {
  123. if (match) {
  124. return FALSE;
  125. } else if (start_c1) {
  126. if (sav_c1 != start_c1) {
  127. while ((ch=*c1++) && ch != L']') ; /* BJP */
  128. ++c2;
  129. sav_c1 = NULL;
  130. char_ok = TRUE;
  131. } else if (c2[1] == L'\0') {
  132. while ((ch=*c1++) && ch != L']') ; /* BJP */
  133. c2 = sav_c2;
  134. sav_c1 = NULL;
  135. char_ok = TRUE;
  136. }
  137. } else {
  138. while ((ch=*c1++) && ch != L']') ; /* BJP */
  139. ++c2;
  140. char_ok = TRUE;
  141. }
  142. }
  143. break;
  144. default:
  145. if (*c1 == *c2) { /* See if this char matches exactly */
  146. ++c1; ++c2;
  147. char_ok = TRUE;
  148. }
  149. }
  150. if (!char_ok) { /* No match found */
  151. if (start_c1) { /* If there was a '*', start over after*/
  152. c1 = start_c1; /* the '*', and one char further into */
  153. c2 = ++start_c2; /* the filename string than before */
  154. } else {
  155. return FALSE;
  156. }
  157. }
  158. }
  159. while (*c1==L'*') ++c1;
  160. if (*c1==L'\0' && *c2==L'\0')
  161. return TRUE;
  162. else
  163. return FALSE;
  164. }