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.

216 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1989-91 Microsoft Corporation
  3. Module Name:
  4. pathcmp.c
  5. Abstract:
  6. Net path compare routines:
  7. NetpwPathCompare
  8. Author:
  9. Richard L Firth (rfirth) 06-Jan-1992
  10. Revision History:
  11. --*/
  12. #include "nticanon.h"
  13. //
  14. // The size of the buffer needed to hold a canonicalized pathname
  15. //
  16. #define CANON_PATH_SIZE (MAX_PATH * sizeof(TCHAR))
  17. //
  18. // routines
  19. //
  20. LONG
  21. NetpwPathCompare(
  22. IN LPTSTR PathName1,
  23. IN LPTSTR PathName2,
  24. IN DWORD PathType,
  25. IN DWORD Flags
  26. )
  27. /*++
  28. Routine Description:
  29. NetpPathCompare compares the two pathnames to see if they match.
  30. If the supplied names are not canonicalized this function will
  31. do the canonicalization of the pathnames.
  32. It is strongly recommended that this function be called with
  33. canonicalized pathnames ONLY, because the canonicalization
  34. operation is very expensive. If uncanonicalized pathnames are
  35. passed in, the caller should be aware that a non-zero result
  36. could be due to an error which occurred during canonicalization.
  37. Arguments:
  38. PathName1 - The first pathname to compare.
  39. PathName2 - The second pathname to compare.
  40. PathType - The type of the pathnames as determined by NetpPathType.
  41. If non-zero, the function assumes that NetpPathType has
  42. been called on both of the pathnames and that their
  43. types are equal to this value. If zero, the function
  44. ignores this value.
  45. Flags - Flags to determine operation. Currently defined
  46. values are:
  47. rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrc
  48. where
  49. r = Reserved. MBZ.
  50. c = should be set if both of the paths have already been
  51. canonicalized (using NetpPathCanonicalize).
  52. Manifest values for these flags are defined in NET\H\ICANON.H.
  53. Return Value:
  54. 0 if the two paths match.
  55. Non-zero if they don't match, or if an error occurred in the
  56. functions operation.
  57. --*/
  58. {
  59. LONG RetVal = 0;
  60. DWORD PathType1;
  61. DWORD PathType2;
  62. LPTSTR CanonPath1;
  63. LPTSTR CanonPath2;
  64. BOOL HaveHeap = FALSE;
  65. #ifdef CANONDBG
  66. DbgPrint("NetpwPathCompare\n");
  67. #endif
  68. //
  69. // Parameter validation
  70. //
  71. if (Flags & INPC_FLAGS_RESERVED) {
  72. return ERROR_INVALID_PARAMETER;
  73. }
  74. //
  75. // If the paths aren't canonicalized, we curse at the caller under
  76. // our breath and canonicalize them ourselves.
  77. //
  78. if (!(Flags & INPC_FLAGS_PATHS_CANONICALIZED)) {
  79. //
  80. // We can save time and space if we determine and compare
  81. // their types first.
  82. //
  83. if (RetVal = NetpwPathType(PathName1, &PathType1, 0L)) {
  84. return RetVal;
  85. }
  86. if (RetVal = NetpwPathType(PathName2, &PathType2, 0L)) {
  87. return RetVal;
  88. }
  89. //
  90. // Now compare the types and return non-zero if they don't match
  91. //
  92. if (PathType1 != PathType2) {
  93. return 1;
  94. }
  95. //
  96. // If the types match, we have to do the canonicalization
  97. //
  98. if ((CanonPath1 = (LPTSTR)NetpMemoryAllocate(CANON_PATH_SIZE)) == NULL) {
  99. return ERROR_NOT_ENOUGH_MEMORY;
  100. }
  101. if ((CanonPath2 = (LPTSTR)NetpMemoryAllocate(CANON_PATH_SIZE)) == NULL) {
  102. NetpMemoryFree((PVOID)CanonPath1);
  103. return ERROR_NOT_ENOUGH_MEMORY;
  104. }
  105. HaveHeap = TRUE;
  106. //
  107. // Call NetpPathCanonicalize on each pathname
  108. //
  109. RetVal = NetpwPathCanonicalize(
  110. PathName1,
  111. CanonPath1,
  112. CANON_PATH_SIZE,
  113. NULL,
  114. &PathType1,
  115. 0L
  116. );
  117. if (RetVal) {
  118. NetpMemoryFree((PVOID)CanonPath1);
  119. NetpMemoryFree((PVOID)CanonPath2);
  120. return RetVal;
  121. }
  122. RetVal = NetpwPathCanonicalize(
  123. PathName2,
  124. CanonPath2,
  125. CANON_PATH_SIZE,
  126. NULL,
  127. &PathType2,
  128. 0L
  129. );
  130. if (RetVal) {
  131. NetpMemoryFree((PVOID)CanonPath1);
  132. NetpMemoryFree((PVOID)CanonPath2);
  133. return RetVal;
  134. }
  135. } else {
  136. //
  137. // The paths are already canonicalized, just set the pointers
  138. //
  139. CanonPath1 = PathName1;
  140. CanonPath2 = PathName2;
  141. }
  142. //
  143. // Now do the comparison
  144. //
  145. // Paths are no longer case mapped during canonicalization, so we use
  146. // case-insensitive comparison
  147. //
  148. RetVal = STRICMP(CanonPath1, CanonPath2);
  149. //
  150. // if we had to allocate space to store the canonicalized paths then free
  151. // that memory (why don't C have local procedures?)
  152. //
  153. if (HaveHeap) {
  154. NetpMemoryFree((PVOID)CanonPath1);
  155. NetpMemoryFree((PVOID)CanonPath2);
  156. }
  157. return RetVal;
  158. }