16fc87692Swinesync /*
26fc87692Swinesync * Copyright 2016-2017, 2021 Hugh McMaster
36fc87692Swinesync *
46fc87692Swinesync * This library is free software; you can redistribute it and/or
56fc87692Swinesync * modify it under the terms of the GNU Lesser General Public
66fc87692Swinesync * License as published by the Free Software Foundation; either
76fc87692Swinesync * version 2.1 of the License, or (at your option) any later version.
86fc87692Swinesync *
96fc87692Swinesync * This library is distributed in the hope that it will be useful,
106fc87692Swinesync * but WITHOUT ANY WARRANTY; without even the implied warranty of
116fc87692Swinesync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
126fc87692Swinesync * Lesser General Public License for more details.
136fc87692Swinesync *
146fc87692Swinesync * You should have received a copy of the GNU Lesser General Public
156fc87692Swinesync * License along with this library; if not, write to the Free Software
166fc87692Swinesync * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
176fc87692Swinesync */
186fc87692Swinesync
196fc87692Swinesync #include "reg.h"
206fc87692Swinesync
216fa073aaSwinesync static BOOL op_delete_key = TRUE;
226fa073aaSwinesync
output_error(LONG rc)236fa073aaSwinesync static void output_error(LONG rc)
246fa073aaSwinesync {
256fa073aaSwinesync if (rc == ERROR_FILE_NOT_FOUND)
266fa073aaSwinesync {
276fa073aaSwinesync if (op_delete_key)
286fa073aaSwinesync output_message(STRING_KEY_NONEXIST);
296fa073aaSwinesync else
306fa073aaSwinesync output_message(STRING_VALUE_NONEXIST);
316fa073aaSwinesync }
326fa073aaSwinesync else
336fa073aaSwinesync {
346fa073aaSwinesync output_message(STRING_ACCESS_DENIED);
356fa073aaSwinesync }
366fa073aaSwinesync }
376fa073aaSwinesync
run_delete(HKEY root,WCHAR * path,REGSAM sam,WCHAR * key_name,WCHAR * value_name,BOOL value_empty,BOOL value_all,BOOL force)38fde082ffSwinesync static int run_delete(HKEY root, WCHAR *path, REGSAM sam, WCHAR *key_name, WCHAR *value_name,
396fc87692Swinesync BOOL value_empty, BOOL value_all, BOOL force)
406fc87692Swinesync {
416fa073aaSwinesync LONG rc;
42f0c76942Swinesync HKEY hkey;
436fc87692Swinesync
446fc87692Swinesync if (!force)
456fc87692Swinesync {
466fc87692Swinesync BOOL ret;
476fc87692Swinesync
486fc87692Swinesync if (value_name || value_empty)
496fc87692Swinesync ret = ask_confirm(STRING_DELETE_VALUE, value_name);
506fc87692Swinesync else if (value_all)
516fc87692Swinesync ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
526fc87692Swinesync else
536fc87692Swinesync ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
546fc87692Swinesync
556fc87692Swinesync if (!ret)
566fc87692Swinesync {
576fc87692Swinesync output_message(STRING_CANCELLED);
586fc87692Swinesync return 0;
596fc87692Swinesync }
606fc87692Swinesync }
616fc87692Swinesync
62fde082ffSwinesync if ((rc = RegOpenKeyExW(root, path, 0, KEY_READ|KEY_SET_VALUE|sam, &hkey)))
636fc87692Swinesync {
646fa073aaSwinesync output_error(rc);
656fc87692Swinesync return 1;
666fc87692Swinesync }
676fc87692Swinesync
68129ed8f5Swinesync /* Delete registry key if no /v* option is given */
69129ed8f5Swinesync if (!value_name && !value_empty && !value_all)
70129ed8f5Swinesync {
71129ed8f5Swinesync if ((rc = RegDeleteTreeW(hkey, NULL)))
72129ed8f5Swinesync {
73129ed8f5Swinesync RegCloseKey(hkey);
74129ed8f5Swinesync output_error(rc);
75129ed8f5Swinesync return 1;
76129ed8f5Swinesync }
77129ed8f5Swinesync
78129ed8f5Swinesync RegDeleteKeyW(hkey, L"");
79129ed8f5Swinesync RegCloseKey(hkey);
80129ed8f5Swinesync
81129ed8f5Swinesync output_message(STRING_SUCCESS);
82129ed8f5Swinesync return 0;
83129ed8f5Swinesync }
84129ed8f5Swinesync
856fa073aaSwinesync op_delete_key = FALSE;
866fa073aaSwinesync
876fc87692Swinesync if (value_all)
886fc87692Swinesync {
896fc87692Swinesync DWORD max_value_len = 256, value_len;
906fc87692Swinesync WCHAR *value_name;
916fc87692Swinesync
9268d5548fSwinesync value_name = malloc(max_value_len * sizeof(WCHAR));
936fc87692Swinesync
946fc87692Swinesync while (1)
956fc87692Swinesync {
966fc87692Swinesync value_len = max_value_len;
97f0c76942Swinesync rc = RegEnumValueW(hkey, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
986fc87692Swinesync if (rc == ERROR_SUCCESS)
996fc87692Swinesync {
100f0c76942Swinesync rc = RegDeleteValueW(hkey, value_name);
1016fc87692Swinesync if (rc != ERROR_SUCCESS)
1026fc87692Swinesync {
10368d5548fSwinesync free(value_name);
104f0c76942Swinesync RegCloseKey(hkey);
1056fc87692Swinesync output_message(STRING_VALUEALL_FAILED, key_name);
1066fa073aaSwinesync output_error(rc);
1076fc87692Swinesync return 1;
1086fc87692Swinesync }
1096fc87692Swinesync }
1106fc87692Swinesync else if (rc == ERROR_MORE_DATA)
1116fc87692Swinesync {
1126fc87692Swinesync max_value_len *= 2;
11368d5548fSwinesync value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
1146fc87692Swinesync }
1156fc87692Swinesync else break;
1166fc87692Swinesync }
11768d5548fSwinesync free(value_name);
1186fc87692Swinesync }
1196fc87692Swinesync else if (value_name || value_empty)
1206fc87692Swinesync {
1216fa073aaSwinesync if ((rc = RegDeleteValueW(hkey, value_name)))
1226fc87692Swinesync {
123f0c76942Swinesync RegCloseKey(hkey);
1246fa073aaSwinesync output_error(rc);
1256fc87692Swinesync return 1;
1266fc87692Swinesync }
1276fc87692Swinesync }
1286fc87692Swinesync
129f0c76942Swinesync RegCloseKey(hkey);
1306fc87692Swinesync output_message(STRING_SUCCESS);
1316fc87692Swinesync return 0;
1326fc87692Swinesync }
133409d7663Swinesync
reg_delete(int argc,WCHAR * argvW[])134409d7663Swinesync int reg_delete(int argc, WCHAR *argvW[])
135409d7663Swinesync {
136409d7663Swinesync HKEY root;
137409d7663Swinesync WCHAR *path, *key_name, *value_name = NULL;
138409d7663Swinesync BOOL value_all = FALSE, value_empty = FALSE, force = FALSE;
139fde082ffSwinesync REGSAM sam = 0;
140409d7663Swinesync int i;
141409d7663Swinesync
142a1c74056Swinesync if (!parse_registry_key(argvW[2], &root, &path))
143409d7663Swinesync return 1;
144409d7663Swinesync
145409d7663Swinesync for (i = 3; i < argc; i++)
146409d7663Swinesync {
147fa6d473eSwinesync WCHAR *str;
148fa6d473eSwinesync
149fa6d473eSwinesync if (argvW[i][0] != '/' && argvW[i][0] != '-')
150fa6d473eSwinesync goto invalid;
151fa6d473eSwinesync
152fa6d473eSwinesync str = &argvW[i][1];
153409d7663Swinesync
154409d7663Swinesync if (!lstrcmpiW(str, L"va"))
155409d7663Swinesync {
156409d7663Swinesync if (value_all) goto invalid;
157409d7663Swinesync value_all = TRUE;
158409d7663Swinesync continue;
159409d7663Swinesync }
160409d7663Swinesync else if (!lstrcmpiW(str, L"ve"))
161409d7663Swinesync {
162409d7663Swinesync if (value_empty) goto invalid;
163409d7663Swinesync value_empty = TRUE;
164409d7663Swinesync continue;
165409d7663Swinesync }
166fde082ffSwinesync else if (!lstrcmpiW(str, L"reg:32"))
167fde082ffSwinesync {
168fde082ffSwinesync if (sam & KEY_WOW64_32KEY) goto invalid;
169fde082ffSwinesync sam |= KEY_WOW64_32KEY;
170b6bc8e13Swinesync continue;
171fde082ffSwinesync }
172fde082ffSwinesync else if (!lstrcmpiW(str, L"reg:64"))
173fde082ffSwinesync {
174fde082ffSwinesync if (sam & KEY_WOW64_64KEY) goto invalid;
175fde082ffSwinesync sam |= KEY_WOW64_64KEY;
176fde082ffSwinesync continue;
177fde082ffSwinesync }
178409d7663Swinesync else if (!str[0] || str[1])
179409d7663Swinesync goto invalid;
180409d7663Swinesync
181409d7663Swinesync switch (towlower(*str))
182409d7663Swinesync {
183409d7663Swinesync case 'v':
184409d7663Swinesync if (value_name || !(value_name = argvW[++i]))
185409d7663Swinesync goto invalid;
186409d7663Swinesync break;
187409d7663Swinesync case 'f':
188409d7663Swinesync if (force) goto invalid;
189409d7663Swinesync force = TRUE;
190409d7663Swinesync break;
191409d7663Swinesync default:
192409d7663Swinesync goto invalid;
193409d7663Swinesync }
194409d7663Swinesync }
195409d7663Swinesync
196409d7663Swinesync if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
197409d7663Swinesync goto invalid;
198409d7663Swinesync
199fde082ffSwinesync if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
200fde082ffSwinesync goto invalid;
201fde082ffSwinesync
202a1c74056Swinesync key_name = get_long_key(root, path);
203a1c74056Swinesync
204fde082ffSwinesync return run_delete(root, path, sam, key_name, value_name, value_empty, value_all, force);
205409d7663Swinesync
206409d7663Swinesync invalid:
2079cf114d7Swinesync output_message(STRING_INVALID_SYNTAX);
208*1a6f523eSThomas Csovcsity output_message(STRING_FUNC_HELP, _wcsupr(argvW[1]));
209409d7663Swinesync return 1;
210409d7663Swinesync }
211