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
21 static BOOL op_delete_key
= TRUE
;
23 static void output_error(LONG rc
)
25 if (rc
== ERROR_FILE_NOT_FOUND
)
28 output_message(STRING_KEY_NONEXIST
);
30 output_message(STRING_VALUE_NONEXIST
);
34 output_message(STRING_ACCESS_DENIED
);
38 static int run_delete(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
39 BOOL value_empty
, BOOL value_all
, BOOL force
)
48 if (value_name
|| value_empty
)
49 ret
= ask_confirm(STRING_DELETE_VALUE
, value_name
);
51 ret
= ask_confirm(STRING_DELETE_VALUEALL
, key_name
);
53 ret
= ask_confirm(STRING_DELETE_SUBKEY
, key_name
);
57 output_message(STRING_CANCELLED
);
62 /* Delete registry key if no /v* option is given */
63 if (!value_name
&& !value_empty
&& !value_all
)
65 if ((rc
= RegDeleteTreeW(root
, path
)))
71 output_message(STRING_SUCCESS
);
75 if ((rc
= RegOpenKeyExW(root
, path
, 0, KEY_READ
|KEY_SET_VALUE
, &hkey
)))
81 op_delete_key
= FALSE
;
85 DWORD max_value_len
= 256, value_len
;
88 value_name
= malloc(max_value_len
* sizeof(WCHAR
));
92 value_len
= max_value_len
;
93 rc
= RegEnumValueW(hkey
, 0, value_name
, &value_len
, NULL
, NULL
, NULL
, NULL
);
94 if (rc
== ERROR_SUCCESS
)
96 rc
= RegDeleteValueW(hkey
, value_name
);
97 if (rc
!= ERROR_SUCCESS
)
101 output_message(STRING_VALUEALL_FAILED
, key_name
);
106 else if (rc
== ERROR_MORE_DATA
)
109 value_name
= realloc(value_name
, max_value_len
* sizeof(WCHAR
));
115 else if (value_name
|| value_empty
)
117 if ((rc
= RegDeleteValueW(hkey
, value_name
)))
126 output_message(STRING_SUCCESS
);
130 int reg_delete(int argc
, WCHAR
*argvW
[])
133 WCHAR
*path
, *key_name
, *value_name
= NULL
;
134 BOOL value_all
= FALSE
, value_empty
= FALSE
, force
= FALSE
;
137 if (!parse_registry_key(argvW
[2], &root
, &path
))
140 for (i
= 3; i
< argc
; i
++)
144 if (argvW
[i
][0] != '/' && argvW
[i
][0] != '-')
149 if (!lstrcmpiW(str
, L
"va"))
151 if (value_all
) goto invalid
;
155 else if (!lstrcmpiW(str
, L
"ve"))
157 if (value_empty
) goto invalid
;
161 else if (!lstrcmpiW(str
, L
"reg:32") || !lstrcmpiW(str
, L
"reg:64"))
163 else if (!str
[0] || str
[1])
166 switch (towlower(*str
))
169 if (value_name
|| !(value_name
= argvW
[++i
]))
173 if (force
) goto invalid
;
181 if ((value_name
&& value_empty
) || (value_name
&& value_all
) || (value_empty
&& value_all
))
184 key_name
= get_long_key(root
, path
);
186 return run_delete(root
, path
, key_name
, value_name
, value_empty
, value_all
, force
);
189 output_message(STRING_INVALID_SYNTAX
);
190 output_message(STRING_FUNC_HELP
, wcsupr(argvW
[1]));