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.

237 lines
4.2 KiB

  1. /*
  2. * util.c - Miscellaneous utility functions module.
  3. */
  4. /* Headers
  5. **********/
  6. #include "project.h"
  7. #pragma hdrstop
  8. /****************************** Public Functions *****************************/
  9. /*
  10. ** IsLocalDrivePath()
  11. **
  12. **
  13. **
  14. ** Arguments:
  15. **
  16. ** Returns:
  17. **
  18. ** Side Effects: none
  19. */
  20. PUBLIC_CODE BOOL IsLocalDrivePath(LPCTSTR pcszFullPath)
  21. {
  22. BOOL bResult;
  23. ASSERT(IsFullPath(pcszFullPath));
  24. bResult = IsDrivePath(pcszFullPath);
  25. if (bResult)
  26. {
  27. TCHAR rgchRootPath[DRIVE_ROOT_PATH_LEN];
  28. ASSERT(IsCharAlpha(*pcszFullPath));
  29. lstrcpy(rgchRootPath, TEXT("A:\\"));
  30. rgchRootPath[0] = *pcszFullPath;
  31. bResult = (GetDriveType(rgchRootPath) != DRIVE_REMOTE);
  32. }
  33. return(bResult);
  34. }
  35. /*
  36. ** IsUNCPath()
  37. **
  38. ** Determines whether or not a path is in "\\server\share" UNC form.
  39. **
  40. ** Arguments: pcszPath - path to examine
  41. **
  42. ** Returns: TRUE if path is a UNC path. FALSE if not.
  43. **
  44. ** Side Effects: none
  45. **
  46. ** A UNC path is a string of the form two slashes, one or more non-slashes, one
  47. ** slash, one or more non-slashes
  48. */
  49. PUBLIC_CODE BOOL IsUNCPath(LPCTSTR pcszFullPath)
  50. {
  51. BOOL bResult = FALSE;
  52. ASSERT(IsFullPath(pcszFullPath));
  53. if (lstrlen(pcszFullPath) >= 5 &&
  54. IS_SLASH(pcszFullPath[0]) &&
  55. IS_SLASH(pcszFullPath[1]) &&
  56. ! IS_SLASH(pcszFullPath[2]))
  57. {
  58. LPCTSTR pcsz;
  59. for (pcsz = &(pcszFullPath[2]); *pcsz; pcsz = CharNext(pcsz))
  60. {
  61. if (IS_SLASH(*pcsz))
  62. {
  63. bResult = (*(pcsz + 1) &&
  64. ! IS_SLASH(*(pcsz + 1)));
  65. break;
  66. }
  67. }
  68. }
  69. return(bResult);
  70. }
  71. /*
  72. ** DeleteLastDrivePathElement()
  73. **
  74. ** Deletes the last path element from a drive path.
  75. **
  76. ** Arguments: pszDrivePath - drive path whose last element is to be deleted
  77. **
  78. ** Returns: TRUE if path element deleted. FALSE if not, i.e., given path
  79. ** is root path.
  80. **
  81. ** Side Effects: none
  82. **
  83. ** Examples:
  84. **
  85. ** input path output path
  86. ** ---------- -----------
  87. ** c:\ c:\
  88. ** c:\foo c:\
  89. ** c:\foo\bar c:\foo
  90. ** c:\foo\bar\ c:\foo\bar
  91. **
  92. ** N.b., this function does not perform any validity tests on the format of the
  93. ** input path string.
  94. */
  95. PUBLIC_CODE BOOL DeleteLastDrivePathElement(LPTSTR pszDrivePath)
  96. {
  97. BOOL bHackIt;
  98. LPTSTR pszEndOfDriveSpec;
  99. ASSERT(IsDrivePath(pszDrivePath));
  100. pszEndOfDriveSpec = pszDrivePath + 3;
  101. /* Is this a a root path? */
  102. bHackIt = *pszEndOfDriveSpec;
  103. if (bHackIt)
  104. DeleteLastPathElement(pszEndOfDriveSpec);
  105. ASSERT(IsDrivePath(pszDrivePath));
  106. return(bHackIt);
  107. }
  108. #if defined(DEBUG) || defined(VSTF)
  109. /*
  110. ** IsContained()
  111. **
  112. **
  113. **
  114. ** Arguments:
  115. **
  116. ** Returns:
  117. **
  118. ** Side Effects: none
  119. */
  120. PUBLIC_CODE BOOL IsContained(PCVOID pcvJar, UINT ucbJarLen, PCVOID pcvJelly,
  121. UINT ucbJellyLen)
  122. {
  123. BOOL bResult = FALSE;
  124. ASSERT(IS_VALID_READ_BUFFER_PTR(pcvJar, CVOID, ucbJarLen));
  125. ASSERT(IS_VALID_READ_BUFFER_PTR(pcvJelly, CVOID, ucbJellyLen));
  126. if (EVAL(pcvJelly >= pcvJar))
  127. {
  128. UINT ucbJellyOffset;
  129. ucbJellyOffset = (UINT)((PCBYTE)pcvJelly - (PCBYTE)pcvJar);
  130. if (EVAL(ucbJellyOffset < ucbJarLen) &&
  131. EVAL(ucbJellyLen < ucbJarLen - ucbJellyOffset))
  132. bResult = TRUE;
  133. }
  134. return(bResult);
  135. }
  136. /*
  137. ** IsValidCNRName()
  138. **
  139. **
  140. **
  141. ** Arguments:
  142. **
  143. ** Returns:
  144. **
  145. ** Side Effects: none
  146. */
  147. PUBLIC_CODE BOOL IsValidCNRName(LPCTSTR pcszCNRName)
  148. {
  149. BOOL bResult;
  150. /* Any valid string < MAX_PATH_LEN bytes long is a valid CNR name. */
  151. bResult = (IS_VALID_STRING_PTR(pcszCNRName, CSTR) &&
  152. EVAL(lstrlen(pcszCNRName) < MAX_PATH_LEN));
  153. #ifdef DEBUG
  154. /*
  155. * RIP if a CNR name ends in a slash
  156. */
  157. if (bResult)
  158. {
  159. if (IsUNCPath(pcszCNRName))
  160. {
  161. ASSERT(! IS_SLASH(*(CharPrev(pcszCNRName, pcszCNRName + lstrlen(pcszCNRName)))));
  162. }
  163. }
  164. #endif
  165. return(bResult);
  166. }
  167. #endif
  168. #ifdef DEBUG
  169. /*
  170. ** IsDriveRootPath()
  171. **
  172. **
  173. **
  174. ** Arguments:
  175. **
  176. ** Returns:
  177. **
  178. ** Side Effects: none
  179. */
  180. PUBLIC_CODE BOOL IsDriveRootPath(LPCTSTR pcszPath)
  181. {
  182. return(IsDrivePath(pcszPath) &&
  183. lstrlen(pcszPath) == 3);
  184. }
  185. #endif