reg: Use malloc(), realloc() and free() from stdlib.h instead of wine/heap.h.
[wine.git] / programs / reg / delete.c
blob6ead3fc691964a1b7769a528f6d486f2b3d06560
1 /*
2 * Copyright 2016-2017, 2021 Hugh McMaster
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.
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.
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
19 #include "reg.h"
21 int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
22 BOOL value_empty, BOOL value_all, BOOL force)
24 HKEY key;
26 if (!force)
28 BOOL ret;
30 if (value_name || value_empty)
31 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
32 else if (value_all)
33 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
34 else
35 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
37 if (!ret)
39 output_message(STRING_CANCELLED);
40 return 0;
44 /* Delete subtree only if no /v* option is given */
45 if (!value_name && !value_empty && !value_all)
47 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
49 output_message(STRING_CANNOT_FIND);
50 return 1;
52 output_message(STRING_SUCCESS);
53 return 0;
56 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
58 output_message(STRING_CANNOT_FIND);
59 return 1;
62 if (value_all)
64 DWORD max_value_len = 256, value_len;
65 WCHAR *value_name;
66 LONG rc;
68 value_name = malloc(max_value_len * sizeof(WCHAR));
70 while (1)
72 value_len = max_value_len;
73 rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
74 if (rc == ERROR_SUCCESS)
76 rc = RegDeleteValueW(key, value_name);
77 if (rc != ERROR_SUCCESS)
79 free(value_name);
80 RegCloseKey(key);
81 output_message(STRING_VALUEALL_FAILED, key_name);
82 return 1;
85 else if (rc == ERROR_MORE_DATA)
87 max_value_len *= 2;
88 value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
90 else break;
92 free(value_name);
94 else if (value_name || value_empty)
96 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
98 RegCloseKey(key);
99 output_message(STRING_CANNOT_FIND);
100 return 1;
104 RegCloseKey(key);
105 output_message(STRING_SUCCESS);
106 return 0;