setupapi: Fix return behaviour when the device key doesn't exist.
[wine/multimedia.git] / programs / reg / reg.c
blob0f8aa151b797bb0eb5b2f9b094967e04e3baaf16
1 /*
2 * Copyright 2008 Andrew Riedi
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 <windows.h>
20 #include <wine/unicode.h>
21 #include "reg.h"
23 static int reg_printfW(const WCHAR *msg, ...)
25 va_list va_args;
26 int wlen;
27 DWORD count, ret;
28 WCHAR msg_buffer[8192];
30 va_start(va_args, msg);
31 vsprintfW(msg_buffer, msg, va_args);
32 va_end(va_args);
34 wlen = lstrlenW(msg_buffer);
35 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
36 if (!ret)
38 DWORD len;
39 char *msgA;
41 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
42 NULL, 0, NULL, NULL);
43 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
44 if (!msgA)
45 return 0;
47 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
48 NULL, NULL);
49 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
50 HeapFree(GetProcessHeap(), 0, msgA);
53 return count;
56 static int reg_message(int msg)
58 static const WCHAR formatW[] = {'%','s',0};
59 WCHAR msg_buffer[8192];
61 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
62 sizeof(msg_buffer)/sizeof(WCHAR));
63 return reg_printfW(formatW, msg_buffer);
66 static HKEY get_rootkey(LPWSTR key)
68 static const WCHAR szHKLM[] = {'H','K','L','M',0};
69 static const WCHAR szHKEY_LOCAL_MACHINE[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
70 static const WCHAR szHKCU[] = {'H','K','C','U',0};
71 static const WCHAR szHKEY_CURRENT_USER[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
72 static const WCHAR szHKCR[] = {'H','K','C','R',0};
73 static const WCHAR szHKEY_CLASSES_ROOT[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
74 static const WCHAR szHKU[] = {'H','K','U',0};
75 static const WCHAR szHKEY_USERS[] = {'H','K','E','Y','_','U','S','E','R','S',0};
76 static const WCHAR szHKCC[] = {'H','K','C','C',0};
77 static const WCHAR szHKEY_CURRENT_CONFIG[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
79 if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKLM,4)==2 ||
80 CompareStringW(CP_ACP,NORM_IGNORECASE,key,18,szHKEY_LOCAL_MACHINE,18)==2)
81 return HKEY_LOCAL_MACHINE;
82 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCU,4)==2 ||
83 CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CURRENT_USER,17)==2)
84 return HKEY_CURRENT_USER;
85 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCR,4)==2 ||
86 CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CLASSES_ROOT,17)==2)
87 return HKEY_CLASSES_ROOT;
88 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,3,szHKU,3)==2 ||
89 CompareStringW(CP_ACP,NORM_IGNORECASE,key,10,szHKEY_USERS,10)==2)
90 return HKEY_USERS;
91 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCC,4)==2 ||
92 CompareStringW(CP_ACP,NORM_IGNORECASE,key,19,szHKEY_CURRENT_CONFIG,19)==2)
93 return HKEY_CURRENT_CONFIG;
94 else return NULL;
97 static DWORD get_regtype(LPWSTR type)
99 static const WCHAR szREG_SZ[] = {'R','E','G','_','S','Z',0};
100 static const WCHAR szREG_MULTI_SZ[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
101 static const WCHAR szREG_DWORD_BIG_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
102 static const WCHAR szREG_DWORD[] = {'R','E','G','_','D','W','O','R','D',0};
103 static const WCHAR szREG_BINARY[] = {'R','E','G','_','B','I','N','A','R','Y',0};
104 static const WCHAR szREG_DWORD_LITTLE_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
105 static const WCHAR szREG_NONE[] = {'R','E','G','_','N','O','N','E',0};
106 static const WCHAR szREG_EXPAND_SZ[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
108 if (!type)
109 return REG_SZ;
111 if (lstrcmpiW(type,szREG_SZ)==0) return REG_SZ;
112 if (lstrcmpiW(type,szREG_DWORD)==0) return REG_DWORD;
113 if (lstrcmpiW(type,szREG_MULTI_SZ)==0) return REG_MULTI_SZ;
114 if (lstrcmpiW(type,szREG_EXPAND_SZ)==0) return REG_EXPAND_SZ;
115 if (lstrcmpiW(type,szREG_DWORD_BIG_ENDIAN)==0) return REG_DWORD_BIG_ENDIAN;
116 if (lstrcmpiW(type,szREG_DWORD_LITTLE_ENDIAN)==0) return REG_DWORD_LITTLE_ENDIAN;
117 if (lstrcmpiW(type,szREG_BINARY)==0) return REG_BINARY;
118 if (lstrcmpiW(type,szREG_NONE)==0) return REG_NONE;
120 return -1;
123 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
125 LPBYTE out_data = NULL;
126 *reg_count = 0;
128 switch (reg_type)
130 case REG_SZ:
132 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
133 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
134 lstrcpyW((LPWSTR)out_data,data);
135 break;
137 case REG_DWORD:
139 LPWSTR rest;
140 DWORD val;
141 val = strtolW(data, &rest, 0);
142 if (rest == data) {
143 static const WCHAR nonnumber[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','n','u','m','b','e','r','.','\n',0};
144 reg_printfW(nonnumber);
145 break;
147 *reg_count = sizeof(DWORD);
148 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
149 ((LPDWORD)out_data)[0] = val;
150 break;
152 default:
154 static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
155 reg_printfW(unhandled, reg_type,data);
159 return out_data;
162 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
163 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
165 static const WCHAR stubW[] = {'A','D','D',' ','-',' ','%','s',
166 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
167 LPWSTR p;
168 HKEY root,subkey;
170 reg_printfW(stubW, key_name, value_name, value_empty, type, data, force);
172 if (key_name[0]=='\\' && key_name[1]=='\\')
174 reg_message(STRING_NO_REMOTE);
175 return 1;
178 p = strchrW(key_name,'\\');
179 if (!p)
181 reg_message(STRING_INVALID_KEY);
182 return 1;
184 p++;
186 root = get_rootkey(key_name);
187 if (!root)
189 reg_message(STRING_INVALID_KEY);
190 return 1;
193 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
195 reg_message(STRING_INVALID_KEY);
196 return 1;
199 if (value_name || data)
201 DWORD reg_type;
202 DWORD reg_count = 0;
203 BYTE* reg_data = NULL;
205 if (!force)
207 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
209 /* FIXME: Prompt for overwrite */
213 reg_type = get_regtype(type);
214 if (reg_type == -1)
216 RegCloseKey(subkey);
217 reg_message(STRING_INVALID_CMDLINE);
218 return 1;
221 if (data)
222 reg_data = get_regdata(data,reg_type,separator,&reg_count);
224 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
225 HeapFree(GetProcessHeap(),0,reg_data);
228 RegCloseKey(subkey);
229 reg_message(STRING_SUCCESS);
231 return 0;
234 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
235 BOOL value_all, BOOL force)
237 LPWSTR p;
238 HKEY root,subkey;
240 static const WCHAR stubW[] = {'D','E','L','E','T','E',
241 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
242 ,0};
243 reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
245 if (key_name[0]=='\\' && key_name[1]=='\\')
247 reg_message(STRING_NO_REMOTE);
248 return 1;
251 p = strchrW(key_name,'\\');
252 if (!p)
254 reg_message(STRING_INVALID_KEY);
255 return 1;
257 p++;
259 root = get_rootkey(key_name);
260 if (!root)
262 reg_message(STRING_INVALID_KEY);
263 return 1;
266 if (value_name && value_empty)
268 reg_message(STRING_INVALID_CMDLINE);
269 return 1;
272 if (value_empty && value_all)
274 reg_message(STRING_INVALID_CMDLINE);
275 return 1;
278 if (!force)
280 /* FIXME: Prompt for delete */
283 /* Delete subtree only if no /v* option is given */
284 if (!value_name && !value_empty && !value_all)
286 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
288 reg_message(STRING_CANNOT_FIND);
289 return 1;
291 reg_message(STRING_SUCCESS);
292 return 0;
295 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
297 reg_message(STRING_CANNOT_FIND);
298 return 1;
301 if (value_all)
303 LPWSTR szValue;
304 DWORD maxValue;
305 DWORD count;
306 LONG rc;
308 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
309 &maxValue, NULL, NULL, NULL);
310 if (rc != ERROR_SUCCESS)
312 /* FIXME: failure */
313 RegCloseKey(subkey);
314 return 1;
316 maxValue++;
317 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
319 while (1)
321 count = maxValue;
322 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
323 if (rc == ERROR_SUCCESS)
325 rc = RegDeleteValueW(subkey, szValue);
326 if (rc != ERROR_SUCCESS)
327 break;
329 else break;
331 if (rc != ERROR_SUCCESS)
333 /* FIXME delete failed */
336 else if (value_name)
338 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
340 RegCloseKey(subkey);
341 reg_message(STRING_CANNOT_FIND);
342 return 1;
345 else if (value_empty)
347 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
350 RegCloseKey(subkey);
351 reg_message(STRING_SUCCESS);
352 return 0;
355 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
356 BOOL subkey)
358 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
359 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
360 reg_printfW(stubW, key_name, value_name, value_empty, subkey);
362 return 1;
365 int wmain(int argc, WCHAR *argvW[])
367 int i;
369 static const WCHAR addW[] = {'a','d','d',0};
370 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
371 static const WCHAR queryW[] = {'q','u','e','r','y',0};
372 static const WCHAR slashDW[] = {'/','d',0};
373 static const WCHAR slashFW[] = {'/','f',0};
374 static const WCHAR slashHW[] = {'/','h',0};
375 static const WCHAR slashSW[] = {'/','s',0};
376 static const WCHAR slashTW[] = {'/','t',0};
377 static const WCHAR slashVW[] = {'/','v',0};
378 static const WCHAR slashVAW[] = {'/','v','a',0};
379 static const WCHAR slashVEW[] = {'/','v','e',0};
380 static const WCHAR slashHelpW[] = {'/','?',0};
382 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
383 || !lstrcmpiW(argvW[1], slashHW))
385 reg_message(STRING_USAGE);
386 return 0;
389 if (!lstrcmpiW(argvW[1], addW))
391 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
392 BOOL value_empty = FALSE, force = FALSE;
394 if (argc < 3)
396 reg_message(STRING_INVALID_CMDLINE);
397 return 1;
399 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
400 !lstrcmpiW(argvW[2], slashHW)))
402 reg_message(STRING_ADD_USAGE);
403 return 0;
406 key_name = argvW[2];
407 for (i = 1; i < argc; i++)
409 if (!lstrcmpiW(argvW[i], slashVW))
410 value_name = argvW[++i];
411 else if (!lstrcmpiW(argvW[i], slashVEW))
412 value_empty = TRUE;
413 else if (!lstrcmpiW(argvW[i], slashTW))
414 type = argvW[++i];
415 else if (!lstrcmpiW(argvW[i], slashSW))
416 separator = argvW[++i][0];
417 else if (!lstrcmpiW(argvW[i], slashDW))
418 data = argvW[++i];
419 else if (!lstrcmpiW(argvW[i], slashFW))
420 force = TRUE;
422 return reg_add(key_name, value_name, value_empty, type, separator,
423 data, force);
425 else if (!lstrcmpiW(argvW[1], deleteW))
427 WCHAR *key_name, *value_name = NULL;
428 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
430 if (argc < 3)
432 reg_message(STRING_INVALID_CMDLINE);
433 return 1;
435 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
436 !lstrcmpiW(argvW[2], slashHW)))
438 reg_message(STRING_DELETE_USAGE);
439 return 0;
442 key_name = argvW[2];
443 for (i = 1; i < argc; i++)
445 if (!lstrcmpiW(argvW[i], slashVW))
446 value_name = argvW[++i];
447 else if (!lstrcmpiW(argvW[i], slashVEW))
448 value_empty = TRUE;
449 else if (!lstrcmpiW(argvW[i], slashVAW))
450 value_all = TRUE;
451 else if (!lstrcmpiW(argvW[i], slashFW))
452 force = TRUE;
454 return reg_delete(key_name, value_name, value_empty, value_all, force);
456 else if (!lstrcmpiW(argvW[1], queryW))
458 WCHAR *key_name, *value_name = NULL;
459 BOOL value_empty = FALSE, subkey = FALSE;
461 if (argc < 3)
463 reg_message(STRING_INVALID_CMDLINE);
464 return 1;
466 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
467 !lstrcmpiW(argvW[2], slashHW)))
469 reg_message(STRING_QUERY_USAGE);
470 return 0;
473 key_name = argvW[2];
474 for (i = 1; i < argc; i++)
476 if (!lstrcmpiW(argvW[i], slashVW))
477 value_name = argvW[++i];
478 else if (!lstrcmpiW(argvW[i], slashVEW))
479 value_empty = TRUE;
480 else if (!lstrcmpiW(argvW[i], slashSW))
481 subkey = TRUE;
483 return reg_query(key_name, value_name, value_empty, subkey);
485 else
487 reg_message(STRING_INVALID_CMDLINE);
488 return 1;