Leaked source code of windows server 2003
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.

173 lines
5.2 KiB

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