ntdll/tests: Add some tests for registry path names.
[wine.git] / programs / reg / reg.c
blobe7729a3c3f601046ca957b77e3e99fdd9e5036cc
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 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
25 static const WCHAR short_hklm[] = {'H','K','L','M',0};
26 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
27 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
28 static const WCHAR short_hku[] = {'H','K','U',0};
29 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
30 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
31 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
32 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
33 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
34 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
36 static const struct
38 HKEY key;
39 const WCHAR *short_name;
40 const WCHAR *long_name;
42 root_rels[] =
44 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
45 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
46 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
47 {HKEY_USERS, short_hku, long_hku},
48 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
51 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
52 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
53 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
54 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
55 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
56 static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
57 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
58 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
60 static const struct
62 DWORD type;
63 const WCHAR *name;
65 type_rels[] =
67 {REG_NONE, type_none},
68 {REG_SZ, type_sz},
69 {REG_EXPAND_SZ, type_expand_sz},
70 {REG_BINARY, type_binary},
71 {REG_DWORD, type_dword},
72 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
73 {REG_DWORD_BIG_ENDIAN, type_dword_be},
74 {REG_MULTI_SZ, type_multi_sz},
77 static int reg_printfW(const WCHAR *msg, ...)
79 va_list va_args;
80 int wlen;
81 DWORD count, ret;
82 WCHAR msg_buffer[8192];
84 va_start(va_args, msg);
85 vsnprintfW(msg_buffer, 8192, msg, va_args);
86 va_end(va_args);
88 wlen = lstrlenW(msg_buffer);
89 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
90 if (!ret)
92 DWORD len;
93 char *msgA;
95 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
96 * back to WriteFile(), assuming the console encoding is still the right
97 * one in that case.
99 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
100 NULL, 0, NULL, NULL);
101 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
102 if (!msgA)
103 return 0;
105 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
106 NULL, NULL);
107 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
108 HeapFree(GetProcessHeap(), 0, msgA);
111 return count;
114 static int reg_message(int msg)
116 static const WCHAR formatW[] = {'%','s',0};
117 WCHAR msg_buffer[8192];
119 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
120 sizeof(msg_buffer)/sizeof(WCHAR));
121 return reg_printfW(formatW, msg_buffer);
124 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
126 DWORD length = strlenW(rootkey_name);
128 return (!strncmpiW(input_path, rootkey_name, length) &&
129 (input_path[length] == 0 || input_path[length] == '\\'));
132 static HKEY path_get_rootkey(const WCHAR *path)
134 DWORD i;
136 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
138 if (path_rootname_cmp(path, root_rels[i].short_name) ||
139 path_rootname_cmp(path, root_rels[i].long_name))
140 return root_rels[i].key;
143 return NULL;
146 static DWORD wchar_get_type(const WCHAR *type_name)
148 DWORD i;
150 if (!type_name)
151 return REG_SZ;
153 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
155 if (!strcmpiW(type_rels[i].name, type_name))
156 return type_rels[i].type;
159 return ~0u;
162 /* hexchar_to_byte from programs/regedit/hexedit.c */
163 static inline BYTE hexchar_to_byte(WCHAR ch)
165 if (ch >= '0' && ch <= '9')
166 return ch - '0';
167 else if (ch >= 'a' && ch <= 'f')
168 return ch - 'a' + 10;
169 else if (ch >= 'A' && ch <= 'F')
170 return ch - 'A' + 10;
171 else
172 return -1;
175 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
177 LPBYTE out_data = NULL;
178 *reg_count = 0;
180 switch (reg_type)
182 case REG_SZ:
184 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
185 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
186 lstrcpyW((LPWSTR)out_data,data);
187 break;
189 case REG_DWORD:
191 LPWSTR rest;
192 DWORD val;
193 val = strtolW(data, &rest, 0);
194 if (rest == data) {
195 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};
196 reg_printfW(nonnumber);
197 break;
199 *reg_count = sizeof(DWORD);
200 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
201 ((LPDWORD)out_data)[0] = val;
202 break;
204 case REG_BINARY:
206 static const WCHAR nohex[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','h','e','x',' ','d','a','t','a','.','\n',0};
207 BYTE hex0, hex1;
208 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
209 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
210 out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
211 if(datalen % 2)
213 hex1 = hexchar_to_byte(data[i++]);
214 if(hex1 == 0xFF)
215 goto no_hex_data;
216 out_data[destByteIndex++] = hex1;
218 for(;i + 1 < datalen;i += 2)
220 hex0 = hexchar_to_byte(data[i]);
221 hex1 = hexchar_to_byte(data[i + 1]);
222 if(hex0 == 0xFF || hex1 == 0xFF)
223 goto no_hex_data;
224 out_data[destByteIndex++] = (hex0 << 4) | hex1;
226 break;
227 no_hex_data:
228 /* cleanup, print error */
229 HeapFree(GetProcessHeap(), 0, out_data);
230 reg_printfW(nohex);
231 out_data = NULL;
232 break;
234 default:
236 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};
237 reg_printfW(unhandled, reg_type,data);
241 return out_data;
244 static BOOL sane_path(const WCHAR *key)
246 unsigned int i = strlenW(key);
248 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
250 reg_message(STRING_INVALID_KEY);
251 return FALSE;
254 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
256 reg_message(STRING_NO_REMOTE);
257 return FALSE;
260 return TRUE;
263 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
264 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
266 LPWSTR p;
267 HKEY root,subkey;
269 if (!sane_path(key_name))
270 return 1;
272 p = strchrW(key_name,'\\');
273 if (!p)
275 reg_message(STRING_INVALID_KEY);
276 return 1;
278 p++;
280 root = path_get_rootkey(key_name);
281 if (!root)
283 reg_message(STRING_INVALID_KEY);
284 return 1;
287 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
289 reg_message(STRING_INVALID_KEY);
290 return 1;
293 if (value_name || data)
295 DWORD reg_type;
296 DWORD reg_count = 0;
297 BYTE* reg_data = NULL;
299 if (!force)
301 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
303 /* FIXME: Prompt for overwrite */
307 reg_type = wchar_get_type(type);
308 if (reg_type == ~0u)
310 RegCloseKey(subkey);
311 reg_message(STRING_UNSUPPORTED_TYPE);
312 return 1;
315 if (data)
316 reg_data = get_regdata(data,reg_type,separator,&reg_count);
318 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
319 HeapFree(GetProcessHeap(),0,reg_data);
322 RegCloseKey(subkey);
323 reg_message(STRING_SUCCESS);
325 return 0;
328 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
329 BOOL value_all, BOOL force)
331 LPWSTR p;
332 HKEY root,subkey;
334 if (!sane_path(key_name))
335 return 1;
337 p = strchrW(key_name,'\\');
338 if (!p)
340 reg_message(STRING_INVALID_KEY);
341 return 1;
343 p++;
345 root = path_get_rootkey(key_name);
346 if (!root)
348 reg_message(STRING_INVALID_KEY);
349 return 1;
352 if (value_name && value_empty)
354 reg_message(STRING_INVALID_CMDLINE);
355 return 1;
358 if (value_empty && value_all)
360 reg_message(STRING_INVALID_CMDLINE);
361 return 1;
364 if (!force)
366 /* FIXME: Prompt for delete */
369 /* Delete subtree only if no /v* option is given */
370 if (!value_name && !value_empty && !value_all)
372 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
374 reg_message(STRING_CANNOT_FIND);
375 return 1;
377 reg_message(STRING_SUCCESS);
378 return 0;
381 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
383 reg_message(STRING_CANNOT_FIND);
384 return 1;
387 if (value_all)
389 LPWSTR szValue;
390 DWORD maxValue;
391 DWORD count;
392 LONG rc;
394 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
395 &maxValue, NULL, NULL, NULL);
396 if (rc != ERROR_SUCCESS)
398 /* FIXME: failure */
399 RegCloseKey(subkey);
400 return 1;
402 maxValue++;
403 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
405 while (1)
407 count = maxValue;
408 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
409 if (rc == ERROR_SUCCESS)
411 rc = RegDeleteValueW(subkey, szValue);
412 if (rc != ERROR_SUCCESS)
413 break;
415 else break;
417 if (rc != ERROR_SUCCESS)
419 /* FIXME delete failed */
422 else if (value_name)
424 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
426 RegCloseKey(subkey);
427 reg_message(STRING_CANNOT_FIND);
428 return 1;
431 else if (value_empty)
433 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
436 RegCloseKey(subkey);
437 reg_message(STRING_SUCCESS);
438 return 0;
441 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
442 BOOL subkey)
444 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
445 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
446 reg_printfW(stubW, key_name, value_name, value_empty, subkey);
448 return 1;
451 int wmain(int argc, WCHAR *argvW[])
453 int i;
455 static const WCHAR addW[] = {'a','d','d',0};
456 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
457 static const WCHAR queryW[] = {'q','u','e','r','y',0};
458 static const WCHAR slashDW[] = {'/','d',0};
459 static const WCHAR slashFW[] = {'/','f',0};
460 static const WCHAR slashHW[] = {'/','h',0};
461 static const WCHAR slashSW[] = {'/','s',0};
462 static const WCHAR slashTW[] = {'/','t',0};
463 static const WCHAR slashVW[] = {'/','v',0};
464 static const WCHAR slashVAW[] = {'/','v','a',0};
465 static const WCHAR slashVEW[] = {'/','v','e',0};
466 static const WCHAR slashHelpW[] = {'/','?',0};
468 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
469 || !lstrcmpiW(argvW[1], slashHW))
471 reg_message(STRING_USAGE);
472 return 0;
475 if (!lstrcmpiW(argvW[1], addW))
477 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
478 BOOL value_empty = FALSE, force = FALSE;
480 if (argc < 3)
482 reg_message(STRING_INVALID_CMDLINE);
483 return 1;
485 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
486 !lstrcmpiW(argvW[2], slashHW)))
488 reg_message(STRING_ADD_USAGE);
489 return 0;
492 key_name = argvW[2];
493 for (i = 1; i < argc; i++)
495 if (!lstrcmpiW(argvW[i], slashVW))
496 value_name = argvW[++i];
497 else if (!lstrcmpiW(argvW[i], slashVEW))
498 value_empty = TRUE;
499 else if (!lstrcmpiW(argvW[i], slashTW))
500 type = argvW[++i];
501 else if (!lstrcmpiW(argvW[i], slashSW))
502 separator = argvW[++i][0];
503 else if (!lstrcmpiW(argvW[i], slashDW))
504 data = argvW[++i];
505 else if (!lstrcmpiW(argvW[i], slashFW))
506 force = TRUE;
508 return reg_add(key_name, value_name, value_empty, type, separator,
509 data, force);
511 else if (!lstrcmpiW(argvW[1], deleteW))
513 WCHAR *key_name, *value_name = NULL;
514 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
516 if (argc < 3)
518 reg_message(STRING_INVALID_CMDLINE);
519 return 1;
521 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
522 !lstrcmpiW(argvW[2], slashHW)))
524 reg_message(STRING_DELETE_USAGE);
525 return 0;
528 key_name = argvW[2];
529 for (i = 1; i < argc; i++)
531 if (!lstrcmpiW(argvW[i], slashVW))
532 value_name = argvW[++i];
533 else if (!lstrcmpiW(argvW[i], slashVEW))
534 value_empty = TRUE;
535 else if (!lstrcmpiW(argvW[i], slashVAW))
536 value_all = TRUE;
537 else if (!lstrcmpiW(argvW[i], slashFW))
538 force = TRUE;
540 return reg_delete(key_name, value_name, value_empty, value_all, force);
542 else if (!lstrcmpiW(argvW[1], queryW))
544 WCHAR *key_name, *value_name = NULL;
545 BOOL value_empty = FALSE, subkey = FALSE;
547 if (argc < 3)
549 reg_message(STRING_INVALID_CMDLINE);
550 return 1;
552 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
553 !lstrcmpiW(argvW[2], slashHW)))
555 reg_message(STRING_QUERY_USAGE);
556 return 0;
559 key_name = argvW[2];
560 for (i = 1; i < argc; i++)
562 if (!lstrcmpiW(argvW[i], slashVW))
563 value_name = argvW[++i];
564 else if (!lstrcmpiW(argvW[i], slashVEW))
565 value_empty = TRUE;
566 else if (!lstrcmpiW(argvW[i], slashSW))
567 subkey = TRUE;
569 return reg_query(key_name, value_name, value_empty, subkey);
571 else
573 reg_message(STRING_INVALID_CMDLINE);
574 return 1;