reg: Avoid using RegQueryInfoKey() to enumerate subkeys and values in the query opera...
[wine.git] / programs / reg / reg.c
bloba6927d0e7e367ebb78ead68b79d6fd2166638c47
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 <wine/debug.h>
22 #include <errno.h>
23 #include "reg.h"
25 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
27 WINE_DEFAULT_DEBUG_CHANNEL(reg);
29 static const WCHAR short_hklm[] = {'H','K','L','M',0};
30 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
31 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
32 static const WCHAR short_hku[] = {'H','K','U',0};
33 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
34 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
35 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
36 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
37 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
38 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
40 static const struct
42 HKEY key;
43 const WCHAR *short_name;
44 const WCHAR *long_name;
46 root_rels[] =
48 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
49 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
50 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
51 {HKEY_USERS, short_hku, long_hku},
52 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
55 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
56 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
57 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
58 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
59 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
60 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};
61 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
62 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
64 static const struct
66 DWORD type;
67 const WCHAR *name;
69 type_rels[] =
71 {REG_NONE, type_none},
72 {REG_SZ, type_sz},
73 {REG_EXPAND_SZ, type_expand_sz},
74 {REG_BINARY, type_binary},
75 {REG_DWORD, type_dword},
76 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
77 {REG_DWORD_BIG_ENDIAN, type_dword_be},
78 {REG_MULTI_SZ, type_multi_sz},
81 static void output_writeconsole(const WCHAR *str, DWORD wlen)
83 DWORD count, ret;
85 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
86 if (!ret)
88 DWORD len;
89 char *msgA;
91 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
92 * back to WriteFile(), assuming the console encoding is still the right
93 * one in that case.
95 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
96 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
97 if (!msgA) return;
99 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
100 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
101 HeapFree(GetProcessHeap(), 0, msgA);
105 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
107 WCHAR *str;
108 DWORD len;
110 SetLastError(NO_ERROR);
111 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
112 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
113 if (len == 0 && GetLastError() != NO_ERROR)
115 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
116 return;
118 output_writeconsole(str, len);
119 LocalFree(str);
122 static void __cdecl output_message(unsigned int id, ...)
124 WCHAR fmt[1024];
125 __ms_va_list va_args;
127 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
129 WINE_FIXME("LoadString failed with %d\n", GetLastError());
130 return;
132 __ms_va_start(va_args, id);
133 output_formatstring(fmt, va_args);
134 __ms_va_end(va_args);
137 static void __cdecl output_string(const WCHAR *fmt, ...)
139 __ms_va_list va_args;
141 __ms_va_start(va_args, fmt);
142 output_formatstring(fmt, va_args);
143 __ms_va_end(va_args);
146 /* ask_confirm() adapted from programs/cmd/builtins.c */
147 static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
149 HMODULE hmod;
150 WCHAR Ybuffer[4];
151 WCHAR Nbuffer[4];
152 WCHAR defval[32];
153 WCHAR answer[MAX_PATH];
154 WCHAR *str;
155 DWORD count;
157 hmod = GetModuleHandleW(NULL);
158 LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
159 LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
160 LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
162 str = (reg_info && *reg_info) ? reg_info : defval;
164 while (1)
166 output_message(msgid, str);
167 output_message(STRING_YESNO);
168 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
169 answer[0] = toupperW(answer[0]);
170 if (answer[0] == Ybuffer[0])
171 return TRUE;
172 if (answer[0] == Nbuffer[0])
173 return FALSE;
177 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
179 DWORD length = strlenW(rootkey_name);
181 return (!strncmpiW(input_path, rootkey_name, length) &&
182 (input_path[length] == 0 || input_path[length] == '\\'));
185 static HKEY path_get_rootkey(const WCHAR *path)
187 DWORD i;
189 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
191 if (path_rootname_cmp(path, root_rels[i].short_name) ||
192 path_rootname_cmp(path, root_rels[i].long_name))
193 return root_rels[i].key;
196 return NULL;
199 static DWORD wchar_get_type(const WCHAR *type_name)
201 DWORD i;
203 if (!type_name)
204 return REG_SZ;
206 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
208 if (!strcmpiW(type_rels[i].name, type_name))
209 return type_rels[i].type;
212 return ~0u;
215 /* hexchar_to_byte from programs/regedit/hexedit.c */
216 static inline BYTE hexchar_to_byte(WCHAR ch)
218 if (ch >= '0' && ch <= '9')
219 return ch - '0';
220 else if (ch >= 'a' && ch <= 'f')
221 return ch - 'a' + 10;
222 else if (ch >= 'A' && ch <= 'F')
223 return ch - 'A' + 10;
224 else
225 return -1;
228 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
230 static const WCHAR empty;
231 LPBYTE out_data = NULL;
232 *reg_count = 0;
234 if (!data) data = &empty;
236 switch (reg_type)
238 case REG_NONE:
239 case REG_SZ:
240 case REG_EXPAND_SZ:
242 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
243 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
244 lstrcpyW((LPWSTR)out_data,data);
245 break;
247 case REG_DWORD:
248 /* case REG_DWORD_LITTLE_ENDIAN: */
249 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
251 LPWSTR rest;
252 unsigned long val;
253 val = strtoulW(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
254 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE) || val > ~0u) {
255 output_message(STRING_MISSING_INTEGER);
256 break;
258 *reg_count = sizeof(DWORD);
259 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
260 ((LPDWORD)out_data)[0] = val;
261 break;
263 case REG_BINARY:
265 BYTE hex0, hex1;
266 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
267 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
268 out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
269 if(datalen % 2)
271 hex1 = hexchar_to_byte(data[i++]);
272 if(hex1 == 0xFF)
273 goto no_hex_data;
274 out_data[destByteIndex++] = hex1;
276 for(;i + 1 < datalen;i += 2)
278 hex0 = hexchar_to_byte(data[i]);
279 hex1 = hexchar_to_byte(data[i + 1]);
280 if(hex0 == 0xFF || hex1 == 0xFF)
281 goto no_hex_data;
282 out_data[destByteIndex++] = (hex0 << 4) | hex1;
284 break;
285 no_hex_data:
286 /* cleanup, print error */
287 HeapFree(GetProcessHeap(), 0, out_data);
288 output_message(STRING_MISSING_HEXDATA);
289 out_data = NULL;
290 break;
292 case REG_MULTI_SZ:
294 int i, destindex, len = strlenW(data);
295 WCHAR *buffer = HeapAlloc(GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
297 for (i = 0, destindex = 0; i < len; i++, destindex++)
299 if (!separator && data[i] == '\\' && data[i + 1] == '0')
301 buffer[destindex] = 0;
302 i++;
304 else if (data[i] == separator)
305 buffer[destindex] = 0;
306 else
307 buffer[destindex] = data[i];
309 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
311 HeapFree(GetProcessHeap(), 0, buffer);
312 output_message(STRING_INVALID_STRING);
313 return NULL;
316 buffer[destindex] = 0;
317 if (destindex && buffer[destindex - 1])
318 buffer[++destindex] = 0;
319 *reg_count = (destindex + 1) * sizeof(WCHAR);
320 return (BYTE *)buffer;
322 default:
323 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
326 return out_data;
329 static BOOL sane_path(const WCHAR *key)
331 unsigned int i = strlenW(key);
333 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
335 output_message(STRING_INVALID_KEY);
336 return FALSE;
339 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
341 output_message(STRING_NO_REMOTE);
342 return FALSE;
345 return TRUE;
348 static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
349 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
351 HKEY key;
353 if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
355 output_message(STRING_INVALID_KEY);
356 return 1;
359 if (value_name || value_empty || data)
361 DWORD reg_type;
362 DWORD reg_count = 0;
363 BYTE* reg_data = NULL;
365 if (!force)
367 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
369 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
371 RegCloseKey(key);
372 output_message(STRING_CANCELLED);
373 return 0;
378 reg_type = wchar_get_type(type);
379 if (reg_type == ~0u)
381 RegCloseKey(key);
382 output_message(STRING_UNSUPPORTED_TYPE, type);
383 return 1;
385 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
387 RegCloseKey(key);
388 output_message(STRING_INVALID_CMDLINE);
389 return 1;
392 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
394 RegCloseKey(key);
395 return 1;
398 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
399 HeapFree(GetProcessHeap(),0,reg_data);
402 RegCloseKey(key);
403 output_message(STRING_SUCCESS);
405 return 0;
408 static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
409 BOOL value_empty, BOOL value_all, BOOL force)
411 HKEY key;
413 if (!force)
415 BOOL ret;
417 if (value_name || value_empty)
418 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
419 else if (value_all)
420 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
421 else
422 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
424 if (!ret)
426 output_message(STRING_CANCELLED);
427 return 0;
431 /* Delete subtree only if no /v* option is given */
432 if (!value_name && !value_empty && !value_all)
434 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
436 output_message(STRING_CANNOT_FIND);
437 return 1;
439 output_message(STRING_SUCCESS);
440 return 0;
443 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
445 output_message(STRING_CANNOT_FIND);
446 return 1;
449 if (value_all)
451 LPWSTR szValue;
452 DWORD maxValue;
453 DWORD count;
454 LONG rc;
456 rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
457 NULL, &maxValue, NULL, NULL, NULL);
458 if (rc != ERROR_SUCCESS)
460 RegCloseKey(key);
461 output_message(STRING_GENERAL_FAILURE);
462 return 1;
464 maxValue++;
465 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
467 while (1)
469 count = maxValue;
470 rc = RegEnumValueW(key, 0, szValue, &count, NULL, NULL, NULL, NULL);
471 if (rc == ERROR_SUCCESS)
473 rc = RegDeleteValueW(key, szValue);
474 if (rc != ERROR_SUCCESS)
476 HeapFree(GetProcessHeap(), 0, szValue);
477 RegCloseKey(key);
478 output_message(STRING_VALUEALL_FAILED, key_name);
479 return 1;
482 else break;
484 HeapFree(GetProcessHeap(), 0, szValue);
486 else if (value_name || value_empty)
488 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
490 RegCloseKey(key);
491 output_message(STRING_CANNOT_FIND);
492 return 1;
496 RegCloseKey(key);
497 output_message(STRING_SUCCESS);
498 return 0;
501 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
503 WCHAR *buffer = NULL;
504 int i;
506 switch (type)
508 case REG_SZ:
509 case REG_EXPAND_SZ:
510 buffer = HeapAlloc(GetProcessHeap(), 0, size_bytes);
511 strcpyW(buffer, (WCHAR *)src);
512 break;
513 case REG_NONE:
514 case REG_BINARY:
516 WCHAR *ptr;
517 WCHAR fmt[] = {'%','0','2','X',0};
519 buffer = HeapAlloc(GetProcessHeap(), 0, (size_bytes * 2 + 1) * sizeof(WCHAR));
520 ptr = buffer;
521 for (i = 0; i < size_bytes; i++)
522 ptr += sprintfW(ptr, fmt, src[i]);
523 break;
525 case REG_DWORD:
526 /* case REG_DWORD_LITTLE_ENDIAN: */
527 case REG_DWORD_BIG_ENDIAN:
529 const int zero_x_dword = 10;
530 WCHAR fmt[] = {'0','x','%','x',0};
532 buffer = HeapAlloc(GetProcessHeap(), 0, (zero_x_dword + 1) * sizeof(WCHAR));
533 sprintfW(buffer, fmt, *(DWORD *)src);
534 break;
536 case REG_MULTI_SZ:
538 const int two_wchars = 2 * sizeof(WCHAR);
539 DWORD tmp_size;
540 const WCHAR *tmp = (const WCHAR *)src;
541 int len, destindex;
543 if (size_bytes <= two_wchars)
545 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
546 *buffer = 0;
547 return buffer;
550 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
551 buffer = HeapAlloc(GetProcessHeap(), 0, tmp_size * 2 + sizeof(WCHAR));
552 len = tmp_size / sizeof(WCHAR);
554 for (i = 0, destindex = 0; i < len; i++, destindex++)
556 if (tmp[i])
557 buffer[destindex] = tmp[i];
558 else
560 buffer[destindex++] = '\\';
561 buffer[destindex] = '0';
564 buffer[destindex] = 0;
565 break;
568 return buffer;
571 static const WCHAR *reg_type_to_wchar(DWORD type)
573 int i, array_size = ARRAY_SIZE(type_rels);
575 for (i = 0; i < array_size; i++)
577 if (type == type_rels[i].type)
578 return type_rels[i].name;
580 return NULL;
583 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
585 WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
586 WCHAR defval[32];
587 WCHAR *reg_data;
588 WCHAR newlineW[] = {'\n',0};
590 if (value_name && value_name[0])
591 output_string(fmt, value_name);
592 else
594 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
595 output_string(fmt, defval);
597 output_string(fmt, reg_type_to_wchar(type));
599 if (data)
601 reg_data = reg_data_to_wchar(type, data, data_size);
602 output_string(fmt, reg_data);
603 HeapFree(GetProcessHeap(), 0, reg_data);
605 else
607 LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
608 output_string(fmt, defval);
610 output_string(newlineW);
613 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
615 WCHAR *subkey_path;
616 WCHAR fmt[] = {'%','s','\\','%','s',0};
618 subkey_path = HeapAlloc(GetProcessHeap(), 0, (path_len + subkey_len + 2) * sizeof(WCHAR));
619 if (!subkey_path)
621 ERR("Failed to allocate memory for subkey_path\n");
622 return NULL;
624 sprintfW(subkey_path, fmt, path, subkey_name);
625 return subkey_path;
628 static unsigned int num_values_found = 0;
630 #define MAX_SUBKEY_LEN 257
632 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
634 LONG rc;
635 DWORD max_data_bytes = 2048, data_size;
636 DWORD subkey_len;
637 DWORD type, path_len, i;
638 BYTE *data;
639 WCHAR fmt[] = {'%','1','\n',0};
640 WCHAR newlineW[] = {'\n',0};
641 WCHAR *subkey_name, *subkey_path;
642 HKEY subkey;
644 data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
645 if (!data)
647 ERR("Failed to allocate memory for data\n");
648 return 1;
651 for (;;)
653 data_size = max_data_bytes;
654 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
655 if (rc == ERROR_MORE_DATA)
657 max_data_bytes = data_size;
658 data = HeapReAlloc(GetProcessHeap(), 0, data, max_data_bytes);
660 else break;
663 if (rc == ERROR_SUCCESS)
665 output_string(fmt, path);
666 output_value(value_name, type, data, data_size);
667 output_string(newlineW);
668 num_values_found++;
671 HeapFree(GetProcessHeap(), 0, data);
673 if (!recurse)
675 if (rc == ERROR_FILE_NOT_FOUND)
677 if (value_name && *value_name)
679 output_message(STRING_CANNOT_FIND);
680 return 1;
682 output_string(fmt, path);
683 output_value(NULL, REG_SZ, NULL, 0);
685 return 0;
688 subkey_name = HeapAlloc(GetProcessHeap(), 0, MAX_SUBKEY_LEN * sizeof(WCHAR));
689 if (!subkey_name)
691 ERR("Failed to allocate memory for subkey_name\n");
692 return 1;
695 path_len = strlenW(path);
697 i = 0;
698 for (;;)
700 subkey_len = MAX_SUBKEY_LEN;
701 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
702 if (rc == ERROR_SUCCESS)
704 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
705 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
707 query_value(subkey, value_name, subkey_path, recurse);
708 RegCloseKey(subkey);
710 HeapFree(GetProcessHeap(), 0, subkey_path);
711 i++;
713 else break;
716 HeapFree(GetProcessHeap(), 0, subkey_name);
717 return 0;
720 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
722 LONG rc;
723 DWORD max_value_len = 256, value_len;
724 DWORD max_data_bytes = 2048, data_size;
725 DWORD subkey_len;
726 DWORD i, type, path_len;
727 WCHAR fmt[] = {'%','1','\n',0};
728 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
729 WCHAR *value_name, *subkey_name, *subkey_path;
730 WCHAR newlineW[] = {'\n',0};
731 BYTE *data;
732 HKEY subkey;
734 output_string(fmt, path);
736 value_name = HeapAlloc(GetProcessHeap(), 0, max_value_len * sizeof(WCHAR));
737 if (!value_name)
739 ERR("Failed to allocate memory for value_name\n");
740 return 1;
743 data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
744 if (!data)
746 HeapFree(GetProcessHeap(), 0, value_name);
747 ERR("Failed to allocate memory for data\n");
748 return 1;
751 i = 0;
752 for (;;)
754 value_len = max_value_len;
755 data_size = max_data_bytes;
756 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
757 if (rc == ERROR_SUCCESS)
759 output_value(value_name, type, data, data_size);
760 i++;
762 else if (rc == ERROR_MORE_DATA)
764 if (data_size > max_data_bytes)
766 max_data_bytes = data_size;
767 data = HeapReAlloc(GetProcessHeap(), 0, data, max_data_bytes);
769 else
771 max_value_len *= 2;
772 value_name = HeapReAlloc(GetProcessHeap(), 0, value_name, max_value_len);
775 else break;
778 HeapFree(GetProcessHeap(), 0, data);
779 HeapFree(GetProcessHeap(), 0, value_name);
781 if (i || recurse)
782 output_string(newlineW);
784 subkey_name = HeapAlloc(GetProcessHeap(), 0, MAX_SUBKEY_LEN * sizeof(WCHAR));
785 if (!subkey_name)
787 ERR("Failed to allocate memory for subkey_name\n");
788 return 1;
791 path_len = strlenW(path);
793 i = 0;
794 for (;;)
796 subkey_len = MAX_SUBKEY_LEN;
797 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
798 if (rc == ERROR_SUCCESS)
800 if (recurse)
802 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
803 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
805 query_all(subkey, subkey_path, recurse);
806 RegCloseKey(subkey);
808 HeapFree(GetProcessHeap(), 0, subkey_path);
810 else output_string(fmt_path, path, subkey_name);
811 i++;
813 else break;
816 HeapFree(GetProcessHeap(), 0, subkey_name);
818 if (i && !recurse)
819 output_string(newlineW);
821 return 0;
824 static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
825 BOOL value_empty, BOOL recurse)
827 HKEY key;
828 WCHAR newlineW[] = {'\n',0};
829 int ret;
831 if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
833 output_message(STRING_CANNOT_FIND);
834 return 1;
837 output_string(newlineW);
839 if (value_name || value_empty)
841 ret = query_value(key, value_name, key_name, recurse);
842 if (recurse)
843 output_message(STRING_MATCHES_FOUND, num_values_found);
845 else
846 ret = query_all(key, key_name, recurse);
848 RegCloseKey(key);
850 return ret;
853 static WCHAR *get_long_key(HKEY root, WCHAR *path)
855 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
856 WCHAR *long_key;
857 WCHAR fmt[] = {'%','s','\\','%','s',0};
859 for (i = 0; i < array_size; i++)
861 if (root == root_rels[i].key)
862 break;
865 len = strlenW(root_rels[i].long_name);
867 if (!path)
869 long_key = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
870 strcpyW(long_key, root_rels[i].long_name);
871 return long_key;
874 len += strlenW(path) + 1; /* add one for the backslash */
875 long_key = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
876 sprintfW(long_key, fmt, root_rels[i].long_name, path);
877 return long_key;
880 static BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
882 if (!sane_path(key))
883 return FALSE;
885 *root = path_get_rootkey(key);
886 if (!*root)
888 output_message(STRING_INVALID_KEY);
889 return FALSE;
892 *path = strchrW(key, '\\');
893 if (*path) (*path)++;
895 *long_key = get_long_key(*root, *path);
897 return TRUE;
900 static BOOL is_help_switch(const WCHAR *s)
902 if (strlenW(s) > 2)
903 return FALSE;
905 if ((s[0] == '/' || s[0] == '-') && (s[1] == 'h' || s[1] == '?'))
906 return TRUE;
908 return FALSE;
911 enum operations {
912 REG_ADD,
913 REG_DELETE,
914 REG_QUERY,
915 REG_INVALID
918 static const WCHAR addW[] = {'a','d','d',0};
919 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
920 static const WCHAR queryW[] = {'q','u','e','r','y',0};
922 static enum operations get_operation(const WCHAR *str, int *op_help)
924 if (!lstrcmpiW(str, addW))
926 *op_help = STRING_ADD_USAGE;
927 return REG_ADD;
930 if (!lstrcmpiW(str, deleteW))
932 *op_help = STRING_DELETE_USAGE;
933 return REG_DELETE;
936 if (!lstrcmpiW(str, queryW))
938 *op_help = STRING_QUERY_USAGE;
939 return REG_QUERY;
942 return REG_INVALID;
945 int wmain(int argc, WCHAR *argvW[])
947 int i, op, op_help, ret;
948 BOOL show_op_help = FALSE;
949 static const WCHAR switchVAW[] = {'v','a',0};
950 static const WCHAR switchVEW[] = {'v','e',0};
951 WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
952 BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
953 HKEY root;
955 if (argc == 1)
957 output_message(STRING_INVALID_SYNTAX);
958 output_message(STRING_REG_HELP);
959 return 1;
962 if (is_help_switch(argvW[1]))
964 output_message(STRING_USAGE);
965 return 0;
968 op = get_operation(argvW[1], &op_help);
970 if (op == REG_INVALID)
972 output_message(STRING_INVALID_OPTION, argvW[1]);
973 output_message(STRING_REG_HELP);
974 return 1;
977 if (argc > 2)
978 show_op_help = is_help_switch(argvW[2]);
980 if (argc == 2 || (show_op_help && argc > 3))
982 output_message(STRING_INVALID_SYNTAX);
983 output_message(STRING_FUNC_HELP, struprW(argvW[1]));
984 return 1;
986 else if (show_op_help)
988 output_message(op_help);
989 return 0;
992 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
993 return 1;
995 for (i = 3; i < argc; i++)
997 if (argvW[i][0] == '/' || argvW[i][0] == '-')
999 WCHAR *ptr = &argvW[i][1];
1001 if (!lstrcmpiW(ptr, switchVEW))
1003 value_empty = TRUE;
1004 continue;
1006 else if (!lstrcmpiW(ptr, switchVAW))
1008 value_all = TRUE;
1009 continue;
1011 else if (!ptr[0] || ptr[1])
1013 output_message(STRING_INVALID_CMDLINE);
1014 return 1;
1017 switch(tolowerW(argvW[i][1]))
1019 case 'v':
1020 if (value_name || !(value_name = argvW[++i]))
1022 output_message(STRING_INVALID_CMDLINE);
1023 return 1;
1025 break;
1026 case 't':
1027 if (type || !(type = argvW[++i]))
1029 output_message(STRING_INVALID_CMDLINE);
1030 return 1;
1032 break;
1033 case 'd':
1034 if (data || !(data = argvW[++i]))
1036 output_message(STRING_INVALID_CMDLINE);
1037 return 1;
1039 break;
1040 case 's':
1041 if (op == REG_QUERY)
1043 recurse = TRUE;
1044 break;
1047 ptr = argvW[++i];
1048 if (!ptr || strlenW(ptr) != 1)
1050 output_message(STRING_INVALID_CMDLINE);
1051 return 1;
1053 separator = ptr[0];
1054 break;
1055 case 'f':
1056 force = TRUE;
1057 break;
1058 default:
1059 output_message(STRING_INVALID_CMDLINE);
1060 return 1;
1065 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
1067 output_message(STRING_INVALID_CMDLINE);
1068 return 1;
1071 if (op == REG_ADD)
1072 ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
1073 else if (op == REG_DELETE)
1074 ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
1075 else if (op == REG_QUERY)
1076 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
1077 return ret;