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.

183 lines
4.3 KiB

  1. #include "main.h"
  2. #define PCOMMON_IMPL
  3. #include "pcommon.h"
  4. //*************************************************************
  5. //
  6. // CheckSlash()
  7. //
  8. // Purpose: Checks for an ending slash and adds one if
  9. // it is missing.
  10. //
  11. // Parameters: lpDir - directory
  12. //
  13. // Return: Pointer to the end of the string
  14. //
  15. // Comments:
  16. //
  17. // History: Date Author Comment
  18. // 6/19/95 ericflo Created
  19. //
  20. //*************************************************************
  21. LPTSTR CheckSlash (LPTSTR lpDir)
  22. {
  23. DWORD dwStrLen;
  24. LPTSTR lpEnd;
  25. lpEnd = lpDir + lstrlen(lpDir);
  26. if (*(lpEnd - 1) != TEXT('\\')) {
  27. *lpEnd = TEXT('\\');
  28. lpEnd++;
  29. *lpEnd = TEXT('\0');
  30. }
  31. return lpEnd;
  32. }
  33. //*************************************************************
  34. //
  35. // RegCleanUpValue()
  36. //
  37. // Purpose: Removes the target value and if no more values / keys
  38. // are present, removes the key. This function then
  39. // works up the parent tree removing keys if they are
  40. // also empty. If any parent key has a value / subkey,
  41. // it won't be removed.
  42. //
  43. // Parameters: hKeyRoot - Root key
  44. // lpSubKey - SubKey
  45. // lpValueName - Value to remove
  46. //
  47. //
  48. // Return: TRUE if successful
  49. // FALSE if an error occurs
  50. //
  51. //*************************************************************
  52. BOOL RegCleanUpValue (HKEY hKeyRoot, LPTSTR lpSubKey, LPTSTR lpValueName)
  53. {
  54. TCHAR szDelKey[2 * MAX_PATH];
  55. LPTSTR lpEnd;
  56. DWORD dwKeys, dwValues;
  57. LONG lResult;
  58. HKEY hKey;
  59. //
  60. // Make a copy of the subkey so we can write to it.
  61. //
  62. lstrcpy (szDelKey, lpSubKey);
  63. //
  64. // First delete the value
  65. //
  66. lResult = RegOpenKeyEx (hKeyRoot, szDelKey, 0, KEY_WRITE, &hKey);
  67. if (lResult == ERROR_SUCCESS)
  68. {
  69. lResult = RegDeleteValue (hKey, lpValueName);
  70. RegCloseKey (hKey);
  71. if (lResult != ERROR_SUCCESS)
  72. {
  73. if (lResult != ERROR_FILE_NOT_FOUND)
  74. {
  75. DebugMsg((DM_WARNING, TEXT("RegCleanUpKey: Failed to delete value <%s> with %d."), lpValueName, lResult));
  76. return FALSE;
  77. }
  78. }
  79. }
  80. //
  81. // Now loop through each of the parents. If the parent is empty
  82. // eg: no values and no other subkeys, then remove the parent and
  83. // keep working up.
  84. //
  85. lpEnd = szDelKey + lstrlen(szDelKey) - 1;
  86. while (lpEnd >= szDelKey)
  87. {
  88. //
  89. // Find the parent key
  90. //
  91. while ((lpEnd > szDelKey) && (*lpEnd != TEXT('\\')))
  92. lpEnd--;
  93. //
  94. // Open the key
  95. //
  96. lResult = RegOpenKeyEx (hKeyRoot, szDelKey, 0, KEY_READ, &hKey);
  97. if (lResult != ERROR_SUCCESS)
  98. {
  99. if (lResult == ERROR_FILE_NOT_FOUND)
  100. {
  101. goto LoopAgain;
  102. }
  103. else
  104. {
  105. DebugMsg((DM_WARNING, TEXT("RegCleanUpKey: Failed to open key <%s> with %d."), szDelKey, lResult));
  106. return FALSE;
  107. }
  108. }
  109. //
  110. // See if there any any values / keys
  111. //
  112. lResult = RegQueryInfoKey (hKey, NULL, NULL, NULL, &dwKeys, NULL, NULL,
  113. &dwValues, NULL, NULL, NULL, NULL);
  114. RegCloseKey (hKey);
  115. if (lResult != ERROR_SUCCESS)
  116. {
  117. DebugMsg((DM_WARNING, TEXT("RegCleanUpKey: Failed to query key <%s> with %d."), szDelKey, lResult));
  118. return FALSE;
  119. }
  120. //
  121. // Exit now if this key has values or keys
  122. //
  123. if ((dwKeys != 0) || (dwValues != 0))
  124. {
  125. return TRUE;
  126. }
  127. RegDeleteKey (hKeyRoot, szDelKey);
  128. LoopAgain:
  129. //
  130. // If we are at the beginning of the subkey, we can leave now.
  131. //
  132. if (lpEnd == szDelKey)
  133. {
  134. return TRUE;
  135. }
  136. //
  137. // There is a parent key. Remove the slash and loop again.
  138. //
  139. if (*lpEnd == TEXT('\\'))
  140. {
  141. *lpEnd = TEXT('\0');
  142. }
  143. }
  144. return TRUE;
  145. }