3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
21 #include <sys/cygwin.h>
23 #define DEFAULT_KEY_SEPARATOR '\\'
27 int value_type = REG_AUTO;
29 char key_sep = DEFAULT_KEY_SEPARATOR;
31 #define LIST_KEYS 0x01
32 #define LIST_VALS 0x02
33 #define LIST_ALL (LIST_KEYS | LIST_VALS)
35 static const char version[] = "$Revision$";
36 static char *prog_name;
38 static struct option longopts[] =
40 {"binary", no_argument, NULL, 'b' },
41 {"dword", no_argument, NULL, 'd' },
42 {"dword-be", no_argument, NULL, 'D' },
43 {"expand-string", no_argument, NULL, 'e' },
44 {"help", no_argument, NULL, 'h' },
45 {"integer", no_argument, NULL, 'i' },
46 {"keys", no_argument, NULL, 'k'},
47 {"list", no_argument, NULL, 'l'},
48 {"multi-string", no_argument, NULL, 'm'},
49 {"none", no_argument, NULL, 'n' },
50 {"postfix", no_argument, NULL, 'p'},
51 {"quiet", no_argument, NULL, 'q'},
52 {"qword", no_argument, NULL, 'Q' },
53 {"string", no_argument, NULL, 's'},
54 {"verbose", no_argument, NULL, 'v'},
55 {"version", no_argument, NULL, 'V'},
56 {"wow64", no_argument, NULL, 'w'},
57 {"wow32", no_argument, NULL, 'W'},
58 {"hex", no_argument, NULL, 'x'},
59 {"key-separator", required_argument, NULL, 'K'},
63 static char opts[] = "bdDehiklmnpqQsvVwWxK:";
72 "REG_DWORD_BIG_ENDIAN",
76 "REG_FULL_RESOURCE_DESCRIPTOR",
77 "REG_RESOURCE_REQUIREMENTS_LIST",
93 usage (FILE *where = stderr)
96 "Usage: %s [OPTION] ACTION KEY [data...]\n"
97 "View or edit the Win32 registry\n"
103 " add KEY\\SUBKEY add new SUBKEY\n"
104 " check KEY exit 0 if KEY exists, 1 if not\n"
105 " get KEY\\VALUE prints VALUE to stdout\n"
106 " list KEY list SUBKEYs and VALUEs\n"
107 " remove KEY remove KEY\n"
108 " set KEY\\VALUE [data ...] set VALUE\n"
109 " unset KEY\\VALUE removes VALUE from KEY\n"
110 " load KEY\\SUBKEY PATH load hive from PATH into new SUBKEY\n"
111 " unload KEY\\SUBKEY unload hive and remove SUBKEY\n"
112 " save KEY\\SUBKEY PATH save SUBKEY into new hive PATH\n"
115 "Options for 'list' Action:\n"
116 " -k, --keys print only KEYs\n"
117 " -l, --list print only VALUEs\n"
118 " -p, --postfix like ls -p, appends '\\' postfix to KEY names\n"
120 "Options for 'get' Action:\n"
121 " -b, --binary print data as printable hex bytes\n"
122 " -n, --none print data as stream of bytes as stored in registry\n"
123 " -x, --hex print numerical data as hex numbers\n"
125 "Options for 'set' Action:\n"
126 " -b, --binary set type to REG_BINARY (hex args or '-')\n"
127 " -d, --dword set type to REG_DWORD\n"
128 " -D, --dword-be set type to REG_DWORD_BIG_ENDIAN\n"
129 " -e, --expand-string set type to REG_EXPAND_SZ\n"
130 " -i, --integer set type to REG_DWORD\n"
131 " -m, --multi-string set type to REG_MULTI_SZ\n"
132 " -n, --none set type to REG_NONE\n"
133 " -Q, --qword set type to REG_QWORD\n"
134 " -s, --string set type to REG_SZ\n"
136 "Options for 'set' and 'unset' Actions:\n"
137 " -K<c>, --key-separator[=]<c> set key-value separator to <c> instead of '\\'\n"
140 " -h, --help output usage information and exit\n"
141 " -q, --quiet no error output, just nonzero return if KEY/VALUE missing\n"
142 " -v, --verbose verbose output, including VALUE contents when applicable\n"
143 " -w, --wow64 access 64 bit registry view (ignored on 32 bit Windows)\n"
144 " -W, --wow32 access 32 bit registry view (ignored on 32 bit Windows)\n"
145 " -V, --version output version information and exit\n"
148 "KEY is in the format [host]\\prefix\\KEY\\KEY\\VALUE, where host is optional\n"
149 "remote host in either \\\\hostname or hostname: format and prefix is any of:\n"
150 " root HKCR HKEY_CLASSES_ROOT (local only)\n"
151 " config HKCC HKEY_CURRENT_CONFIG (local only)\n"
152 " user HKCU HKEY_CURRENT_USER (local only)\n"
153 " machine HKLM HKEY_LOCAL_MACHINE\n"
154 " users HKU HKEY_USERS\n"
156 "If the keyname starts with a forward slash ('/'), the forward slash is used\n"
157 "as separator and the backslash can be used as escape character.\n");
160 "%s list '/machine/SOFTWARE/Classes/MIME/Database/Content Type/audio\\/wav'\n", prog_name);
164 "ACTION is one of add, check, get, list, remove, set, unset, load, unload, save\n"
166 "Try '%s --help' for more information.\n", prog_name);
167 exit (where == stderr ? 1 : 0);
173 const char *v = strchr (version, ':');
183 len = strchr (v, ' ') - v;
188 Copyright 2000-2009 Red Hat, Inc.\n\
190 ", prog_name, len, v, __DATE__);
199 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
200 | FORMAT_MESSAGE_FROM_SYSTEM,
201 0, rv, 0, (CHAR *) & buf, 0, 0);
202 fprintf (stderr, "Error (%ld): %s\n", rv, buf);
214 {"root", HKEY_CLASSES_ROOT},
215 {"HKCR", HKEY_CLASSES_ROOT},
216 {"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT},
217 {"config", HKEY_CURRENT_CONFIG},
218 {"HKCC", HKEY_CURRENT_CONFIG},
219 {"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG},
220 {"user", HKEY_CURRENT_USER},
221 {"HKCU", HKEY_CURRENT_USER},
222 {"HKEY_CURRENT_USER", HKEY_CURRENT_USER},
223 {"machine", HKEY_LOCAL_MACHINE},
224 {"HKLM", HKEY_LOCAL_MACHINE},
225 {"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE},
226 {"users", HKEY_USERS},
228 {"HKEY_USERS", HKEY_USERS},
233 translate (char *key)
235 #define isodigit(c) (strchr("01234567", c))
236 #define tooct(c) ((c)-'0')
237 #define tohex(c) (strchr(_hs,tolower(c))-_hs)
238 static char _hs[] = "0123456789abcdef";
284 c = (c << 3) | tooct (*++s);
286 c = (c << 3) | tooct (*++s);
291 if (!isxdigit (s[1]))
297 c = (c << 4) | tohex (*++s);
301 default: /* before non-special char: just add the char */
315 find_key (int howmanyparts, REGSAM access, int option = 0)
319 char *n = argv[0], *e, *h, c;
328 /* expect host:/key/value format */
329 host = (char*) malloc (strlen (n) + 1);
330 host[0] = host [1] = '\\';
331 for (e = n, h = host + 2; *e && *e != ':'; e++, h++)
338 else if (n[0] == '\\' && n[1] == '\\')
340 /* expect //host/key/value format */
341 host = (char*) malloc (strlen (n) + 1);
342 host[0] = host[1] = '\\';
343 for (e = n + 2, h = host + 2; *e && *e != '\\'; e++, h++)
351 for (e = n; *e && *e != '\\'; e++);
354 for (i = 0; wkprefixes[i].string; i++)
355 if (strcmp (wkprefixes[i].string, n) == 0)
357 if (!wkprefixes[i].string)
359 fprintf (stderr, "Unknown key prefix. Valid prefixes are:\n");
360 for (i = 0; wkprefixes[i].string; i++)
361 fprintf (stderr, "\t%s\n", wkprefixes[i].string);
367 while (*n && *n == '\\')
370 if (howmanyparts > 1)
372 while (n < e && *e != key_sep)
376 key = wkprefixes[i].key;
379 len = mbstowcs (NULL, n, 0) + 1;
380 value = (wchar_t *) malloc (len);
381 mbstowcs (value, n, len);
389 len = mbstowcs (NULL, e + 1, 0) + 1;
390 value = (wchar_t *) malloc (len);
391 mbstowcs (value, e + 1, len);
396 rv = RegConnectRegistry (host, wkprefixes[i].key, &base);
397 if (rv != ERROR_SUCCESS)
402 base = wkprefixes[i].key;
408 len = mbstowcs (NULL, n, 0) + 1;
410 mbstowcs (name, n, len);
413 rv = RegOpenKeyExW (base, name, 0, access | wow64, &key);
414 if (option && (rv == ERROR_SUCCESS || rv == ERROR_ACCESS_DENIED))
416 /* reopen with desired option due to missing option support in
418 /* FIXME: may create the key in rare cases (e.g. access denied
421 if (RegCreateKeyExW (base, name, 0, NULL, option, access | wow64,
425 if (rv == ERROR_SUCCESS)
431 if (rv != ERROR_SUCCESS)
436 ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], NULL, 0);
437 wchar_t win32_path[len];
438 cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], win32_path, len);
439 rv = RegLoadKeyW (base, name, win32_path);
440 if (rv != ERROR_SUCCESS)
443 printf ("key %ls loaded from file %ls\n", name, win32_path);
447 rv = RegUnLoadKeyW (base, name);
448 if (rv != ERROR_SUCCESS)
451 printf ("key %ls unloaded\n", name);
460 DWORD num_subkeys, maxsubkeylen, num_values, maxvalnamelen, maxvaluelen;
462 wchar_t *subkey_name, *value_name, *class_name, *vd;
463 unsigned char *value_data;
467 find_key (1, KEY_READ);
468 RegQueryInfoKeyW (key, 0, 0, 0, &num_subkeys, &maxsubkeylen, &maxclasslen,
469 &num_values, &maxvalnamelen, &maxvaluelen, 0, 0);
471 subkey_name = (wchar_t *) malloc ((maxsubkeylen + 1) * sizeof (wchar_t));
472 class_name = (wchar_t *) malloc ((maxclasslen + 1) * sizeof (wchar_t));
473 value_name = (wchar_t *) malloc ((maxvalnamelen + 1) * sizeof (wchar_t));
474 value_data = (unsigned char *) malloc (maxvaluelen + 1);
479 if (listwhat & LIST_KEYS)
480 for (i = 0; i < num_subkeys; i++)
482 m = (maxsubkeylen + 1) * sizeof (wchar_t);
483 n = (maxclasslen + 1) * sizeof (wchar_t);
484 RegEnumKeyExW (key, i, subkey_name, &m, 0, class_name, &n, 0);
485 printf ("%ls", subkey_name);
486 if (postfix || verbose)
487 fputc (key_sep, stdout);
490 printf (" (%ls)", class_name);
495 if (listwhat & LIST_VALS)
496 for (i = 0; i < num_values; i++)
498 m = (maxvalnamelen + 1) * sizeof (wchar_t);
500 RegEnumValueW (key, i, value_name, &m, 0, &t, (BYTE *) value_data, &n);
503 printf ("%ls\n", value_name);
506 printf ("%ls (%s) = ", value_name, types[t]);
511 for (j = 0; j < 8 && j < n; j++)
512 printf ("%02x ", value_data[j]);
516 printf ("0x%08lx (%lu)\n", *(DWORD *) value_data,
517 *(DWORD *) value_data);
519 case REG_DWORD_BIG_ENDIAN:
520 v = ((value_data[0] << 24)
521 | (value_data[1] << 16)
522 | (value_data[2] << 8)
524 printf ("0x%08x (%d)\n", v, v);
527 printf ("0x%016llx (%llu)\n",
528 *(unsigned long long *) value_data,
529 *(unsigned long long *) value_data);
534 printf ("\"%ls\"\n", (wchar_t *) value_data);
537 vd = (wchar_t *) value_data;
540 printf ("\"%ls\"", vd);
541 vd = vd + wcslen (vd) + 1;
559 find_key (2, KEY_ALL_ACCESS);
562 int rv = RegCreateKeyExW (key, value, 0, NULL, REG_OPTION_NON_VOLATILE,
563 KEY_ALL_ACCESS | wow64, 0, &newkey, &newtype);
564 if (rv != ERROR_SUCCESS)
569 if (newtype == REG_OPENED_EXISTING_KEY)
570 printf ("Key %ls already exists\n", value);
572 printf ("Key %ls created\n", value);
578 WINADVAPI LONG WINAPI (*regDeleteKeyEx)(HKEY, LPCWSTR, REGSAM, DWORD);
586 find_key (2, KEY_ALL_ACCESS);
589 HMODULE mod = LoadLibrary ("advapi32.dll");
591 regDeleteKeyEx = (WINADVAPI LONG WINAPI (*)(HKEY, LPCWSTR, REGSAM, DWORD)) GetProcAddress (mod, "RegDeleteKeyExW");
594 rv = (*regDeleteKeyEx) (key, value, wow64, 0);
596 rv = RegDeleteKeyW (key, value);
597 if (rv != ERROR_SUCCESS)
600 printf ("subkey %ls deleted\n", value);
607 find_key (1, KEY_READ);
609 printf ("key %s exists\n", argv[0]);
618 unsigned long long llval;
619 char *a = argv[1], *data = 0;
620 find_key (2, KEY_ALL_ACCESS);
624 if (value_type == REG_AUTO)
627 llval = strtoull (a, &e, 0);
629 value_type = REG_EXPAND_SZ;
630 else if (a[0] && !*e)
631 value_type = llval > 0xffffffffULL ? REG_QWORD : REG_DWORD;
633 value_type = REG_MULTI_SZ;
642 for (n = 0; argv[n+1]; n++)
644 if (n == 1 && strcmp (argv[1], "-") == 0)
645 { /* read from stdin */
652 data = (char *) realloc (data, i);
654 int r = fread (data+n, 1, i-n, stdin);
661 { /* parse hex from argv */
662 data = (char *) malloc (n);
663 for (i = 0; i < n; i++)
667 v = strtoul (argv[i+1], &e, 16);
668 if (errno || v > 0xff || *e)
670 fprintf (stderr, "Invalid hex constant `%s'\n", argv[i+1]);
676 rv = RegSetValueExW (key, value, 0, value_type, (const BYTE *) data, n);
679 v = strtoul (a, 0, 0);
680 rv = RegSetValueExW (key, value, 0, REG_DWORD, (const BYTE *) &v,
683 case REG_DWORD_BIG_ENDIAN:
684 v = strtoul (a, 0, 0);
685 v = (((v & 0xff) << 24)
686 | ((v & 0xff00) << 8)
687 | ((v & 0xff0000) >> 8)
688 | ((v & 0xff000000) >> 24));
689 rv = RegSetValueExW (key, value, 0, REG_DWORD_BIG_ENDIAN,
690 (const BYTE *) &v, sizeof (v));
693 llval = strtoul (a, 0, 0);
694 rv = RegSetValueExW (key, value, 0, REG_QWORD, (const BYTE *) &llval,
699 n = mbstowcs (NULL, a, 0);
701 mbstowcs (w, a, n + 1);
702 rv = RegSetValueExW (key, value, 0, value_type,
703 (const BYTE *) w, (n + 1) * sizeof (wchar_t));
706 for (i = 1, max_n = 1; argv[i]; i++)
707 max_n += mbstowcs (NULL, argv[i], 0) + 1;
708 data = (char *) malloc (max_n * sizeof (wchar_t));
709 for (i = 1, n = 0; argv[i]; i++)
710 n += mbstowcs ((wchar_t *) data + n, argv[i], max_n - n) + 1;
711 ((wchar_t *)data)[n] = L'\0';
712 rv = RegSetValueExW (key, value, 0, REG_MULTI_SZ, (const BYTE *) data,
713 (max_n + 1) * sizeof (wchar_t));
719 rv = ERROR_INVALID_CATEGORY;
726 if (rv != ERROR_SUCCESS)
735 find_key (2, KEY_ALL_ACCESS);
736 DWORD rv = RegDeleteValueW (key, value);
737 if (rv != ERROR_SUCCESS)
740 printf ("value %ls deleted\n", value);
747 find_key (2, KEY_READ);
748 DWORD vtype, dsize, rv;
752 rv = RegQueryValueExW (key, value, 0, &vtype, 0, &dsize);
753 if (rv != ERROR_SUCCESS)
755 data = (PBYTE) malloc (dsize + 1);
756 rv = RegQueryValueExW (key, value, 0, &vtype, data, &dsize);
757 if (rv != ERROR_SUCCESS)
759 if (value_type == REG_BINARY)
761 for (unsigned i = 0; i < dsize; i++)
762 printf ("%02x%c", (unsigned char)data[i],
763 (i < dsize-1 ? ' ' : '\n'));
765 else if (value_type == REG_NONE)
766 fwrite (data, dsize, 1, stdout);
772 fwrite (data, dsize, 1, stdout);
775 printf (hex ? "0x%08lx\n" : "%lu\n", *(DWORD *) data);
777 case REG_DWORD_BIG_ENDIAN:
778 rv = ((data[0] << 24)
782 printf (hex ? "0x%08lx\n" : "%lu\n", rv);
785 printf (hex ? "0x%016llx\n" : "%llu\n", *(unsigned long long *) data);
789 printf ("%ls\n", (wchar_t *) data);
792 if (value_type == REG_EXPAND_SZ) // hack
796 bufsize = ExpandEnvironmentStringsW ((wchar_t *) data, 0, 0);
797 buf = (wchar_t *) malloc (bufsize + 1);
798 ExpandEnvironmentStringsW ((wchar_t *) data, buf, bufsize + 1);
802 printf ("%ls\n", (wchar_t *) data);
805 vd = (wchar_t *) data;
808 printf ("%ls\n", vd);
809 vd = vd + wcslen (vd) + 1;
841 set_privilege (const char *name)
844 if (!LookupPrivilegeValue (NULL, name, &tp.Privileges[0].Luid))
845 return GetLastError ();
846 tp.PrivilegeCount = 1;
847 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
849 /* OpenProcessToken does not work here, because main thread has its own
850 impersonation token */
851 if (!OpenThreadToken (GetCurrentThread (), TOKEN_ADJUST_PRIVILEGES, FALSE, &t))
852 return GetLastError ();
853 AdjustTokenPrivileges (t, FALSE, &tp, 0, NULL, NULL);
854 DWORD rv = GetLastError ();
867 /* try to set SeBackupPrivilege, let RegSaveKey report the error */
868 set_privilege (SE_BACKUP_NAME);
869 /* REG_OPTION_BACKUP_RESTORE is necessary to save /HKLM/SECURITY */
870 find_key (1, KEY_QUERY_VALUE, REG_OPTION_BACKUP_RESTORE);
871 ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], NULL, 0);
872 wchar_t win32_path[len];
873 cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], win32_path, len);
874 DWORD rv = RegSaveKeyW (key, win32_path, NULL);
875 if (rv != ERROR_SUCCESS)
878 printf ("key saved to %ls\n", win32_path);
890 {"remove", cmd_remove},
891 {"check", cmd_check},
893 {"unset", cmd_unset},
896 {"unload", cmd_unload},
902 main (int argc, char **_argv)
906 setlocale (LC_ALL, "");
907 prog_name = strrchr (_argv[0], '/');
908 if (prog_name == NULL)
909 prog_name = strrchr (_argv[0], '\\');
910 if (prog_name == NULL)
911 prog_name = _argv[0];
915 while ((g = getopt_long (argc, _argv, opts, longopts, NULL)) != EOF)
919 value_type = REG_BINARY;
922 value_type = REG_DWORD;
925 value_type = REG_DWORD_BIG_ENDIAN;
928 value_type = REG_EXPAND_SZ;
931 listwhat |= LIST_KEYS;
936 value_type = REG_DWORD;
939 listwhat |= LIST_VALS;
942 value_type = REG_MULTI_SZ;
945 value_type = REG_NONE;
954 value_type = REG_QWORD;
966 wow64 = KEY_WOW64_64KEY;
969 wow64 = KEY_WOW64_32KEY;
981 if ((_argv[optind] == NULL) || (_argv[optind+1] == NULL))
984 argv = _argv + optind;
986 for (i = 0; commands[i].name; i++)
987 if (strcmp (commands[i].name, argv[0]) == 0)
990 return commands[i].func ();