delete.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2016-2017, 2021 Hugh McMaster
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #include "reg.h"
  19. static BOOL op_delete_key = TRUE;
  20. static void output_error(LONG rc)
  21. {
  22. if (rc == ERROR_FILE_NOT_FOUND)
  23. {
  24. if (op_delete_key)
  25. output_message(STRING_KEY_NONEXIST);
  26. else
  27. output_message(STRING_VALUE_NONEXIST);
  28. }
  29. else
  30. {
  31. output_message(STRING_ACCESS_DENIED);
  32. }
  33. }
  34. static int run_delete(HKEY root, WCHAR *path, REGSAM sam, WCHAR *key_name, WCHAR *value_name,
  35. BOOL value_empty, BOOL value_all, BOOL force)
  36. {
  37. LONG rc;
  38. HKEY hkey;
  39. if (!force)
  40. {
  41. BOOL ret;
  42. if (value_name || value_empty)
  43. ret = ask_confirm(STRING_DELETE_VALUE, value_name);
  44. else if (value_all)
  45. ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
  46. else
  47. ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
  48. if (!ret)
  49. {
  50. output_message(STRING_CANCELLED);
  51. return 0;
  52. }
  53. }
  54. if ((rc = RegOpenKeyExW(root, path, 0, KEY_READ|KEY_SET_VALUE|sam, &hkey)))
  55. {
  56. output_error(rc);
  57. return 1;
  58. }
  59. /* Delete registry key if no /v* option is given */
  60. if (!value_name && !value_empty && !value_all)
  61. {
  62. if ((rc = RegDeleteTreeW(hkey, NULL)))
  63. {
  64. RegCloseKey(hkey);
  65. output_error(rc);
  66. return 1;
  67. }
  68. RegDeleteKeyW(hkey, L"");
  69. RegCloseKey(hkey);
  70. output_message(STRING_SUCCESS);
  71. return 0;
  72. }
  73. op_delete_key = FALSE;
  74. if (value_all)
  75. {
  76. DWORD max_value_len = 256, value_len;
  77. WCHAR *value_name;
  78. value_name = malloc(max_value_len * sizeof(WCHAR));
  79. while (1)
  80. {
  81. value_len = max_value_len;
  82. rc = RegEnumValueW(hkey, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
  83. if (rc == ERROR_SUCCESS)
  84. {
  85. rc = RegDeleteValueW(hkey, value_name);
  86. if (rc != ERROR_SUCCESS)
  87. {
  88. free(value_name);
  89. RegCloseKey(hkey);
  90. output_message(STRING_VALUEALL_FAILED, key_name);
  91. output_error(rc);
  92. return 1;
  93. }
  94. }
  95. else if (rc == ERROR_MORE_DATA)
  96. {
  97. max_value_len *= 2;
  98. value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
  99. }
  100. else break;
  101. }
  102. free(value_name);
  103. }
  104. else if (value_name || value_empty)
  105. {
  106. if ((rc = RegDeleteValueW(hkey, value_name)))
  107. {
  108. RegCloseKey(hkey);
  109. output_error(rc);
  110. return 1;
  111. }
  112. }
  113. RegCloseKey(hkey);
  114. output_message(STRING_SUCCESS);
  115. return 0;
  116. }
  117. int reg_delete(int argc, WCHAR *argvW[])
  118. {
  119. HKEY root;
  120. WCHAR *path, *key_name, *value_name = NULL;
  121. BOOL value_all = FALSE, value_empty = FALSE, force = FALSE;
  122. REGSAM sam = 0;
  123. int i;
  124. if (!parse_registry_key(argvW[2], &root, &path))
  125. return 1;
  126. for (i = 3; i < argc; i++)
  127. {
  128. WCHAR *str;
  129. if (argvW[i][0] != '/' && argvW[i][0] != '-')
  130. goto invalid;
  131. str = &argvW[i][1];
  132. if (!lstrcmpiW(str, L"va"))
  133. {
  134. if (value_all) goto invalid;
  135. value_all = TRUE;
  136. continue;
  137. }
  138. else if (!lstrcmpiW(str, L"ve"))
  139. {
  140. if (value_empty) goto invalid;
  141. value_empty = TRUE;
  142. continue;
  143. }
  144. else if (!lstrcmpiW(str, L"reg:32"))
  145. {
  146. if (sam & KEY_WOW64_32KEY) goto invalid;
  147. sam |= KEY_WOW64_32KEY;
  148. continue;
  149. }
  150. else if (!lstrcmpiW(str, L"reg:64"))
  151. {
  152. if (sam & KEY_WOW64_64KEY) goto invalid;
  153. sam |= KEY_WOW64_64KEY;
  154. continue;
  155. }
  156. else if (!str[0] || str[1])
  157. goto invalid;
  158. switch (towlower(*str))
  159. {
  160. case 'v':
  161. if (value_name || !(value_name = argvW[++i]))
  162. goto invalid;
  163. break;
  164. case 'f':
  165. if (force) goto invalid;
  166. force = TRUE;
  167. break;
  168. default:
  169. goto invalid;
  170. }
  171. }
  172. if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
  173. goto invalid;
  174. if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
  175. goto invalid;
  176. key_name = get_long_key(root, path);
  177. return run_delete(root, path, sam, key_name, value_name, value_empty, value_all, force);
  178. invalid:
  179. output_message(STRING_INVALID_SYNTAX);
  180. output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
  181. return 1;
  182. }