2000-02-18 03:38:33 +08:00
|
|
|
/* regtool.cc
|
|
|
|
|
2009-12-16 22:02:57 +08:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
2015-02-03 23:14:57 +08:00
|
|
|
2009, 2010, 2011, 2015 Red Hat Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-03-03 17:43:35 +08:00
|
|
|
#include <errno.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <ctype.h>
|
2009-12-16 22:02:57 +08:00
|
|
|
#include <wchar.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <getopt.h>
|
2009-12-16 22:02:57 +08:00
|
|
|
#include <locale.h>
|
2006-10-17 03:34:27 +08:00
|
|
|
#define WINVER 0x0502
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <windows.h>
|
2006-03-03 17:43:35 +08:00
|
|
|
#include <sys/cygwin.h>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <cygwin/version.h>
|
2010-08-28 19:22:37 +08:00
|
|
|
#include "loadlib.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-13 23:37:32 +08:00
|
|
|
#define DEFAULT_KEY_SEPARATOR '\\'
|
|
|
|
|
2006-10-22 01:19:29 +08:00
|
|
|
#define REG_AUTO -1
|
|
|
|
|
|
|
|
int value_type = REG_AUTO;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-13 23:37:32 +08:00
|
|
|
char key_sep = DEFAULT_KEY_SEPARATOR;
|
|
|
|
|
2001-01-11 06:34:02 +08:00
|
|
|
#define LIST_KEYS 0x01
|
|
|
|
#define LIST_VALS 0x02
|
|
|
|
#define LIST_ALL (LIST_KEYS | LIST_VALS)
|
|
|
|
|
2002-06-03 10:57:55 +08:00
|
|
|
static char *prog_name;
|
|
|
|
|
|
|
|
static struct option longopts[] =
|
|
|
|
{
|
2006-03-03 17:43:35 +08:00
|
|
|
{"binary", no_argument, NULL, 'b' },
|
2006-10-22 01:19:29 +08:00
|
|
|
{"dword", no_argument, NULL, 'd' },
|
2009-12-16 22:02:57 +08:00
|
|
|
{"dword-be", no_argument, NULL, 'D' },
|
2002-06-03 10:57:55 +08:00
|
|
|
{"expand-string", no_argument, NULL, 'e' },
|
2015-02-03 23:14:57 +08:00
|
|
|
{"force", no_argument, NULL, 'f' },
|
2002-06-03 10:57:55 +08:00
|
|
|
{"help", no_argument, NULL, 'h' },
|
|
|
|
{"integer", no_argument, NULL, 'i' },
|
|
|
|
{"keys", no_argument, NULL, 'k'},
|
|
|
|
{"list", no_argument, NULL, 'l'},
|
|
|
|
{"multi-string", no_argument, NULL, 'm'},
|
2006-10-22 01:19:29 +08:00
|
|
|
{"none", no_argument, NULL, 'n' },
|
2002-06-03 10:57:55 +08:00
|
|
|
{"postfix", no_argument, NULL, 'p'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
2006-10-22 01:19:29 +08:00
|
|
|
{"qword", no_argument, NULL, 'Q' },
|
2002-06-03 10:57:55 +08:00
|
|
|
{"string", no_argument, NULL, 's'},
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
2006-10-17 02:00:06 +08:00
|
|
|
{"wow64", no_argument, NULL, 'w'},
|
2006-10-19 17:08:44 +08:00
|
|
|
{"wow32", no_argument, NULL, 'W'},
|
2006-10-22 01:19:29 +08:00
|
|
|
{"hex", no_argument, NULL, 'x'},
|
2002-08-13 23:37:32 +08:00
|
|
|
{"key-separator", required_argument, NULL, 'K'},
|
2002-06-03 10:57:55 +08:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2015-02-03 23:14:57 +08:00
|
|
|
static char opts[] = "bdDefhiklmnpqQsvVwWxK:";
|
2006-10-22 01:19:29 +08:00
|
|
|
|
|
|
|
const char *types[] =
|
|
|
|
{
|
|
|
|
"REG_NONE",
|
|
|
|
"REG_SZ",
|
|
|
|
"REG_EXPAND_SZ",
|
|
|
|
"REG_BINARY",
|
|
|
|
"REG_DWORD",
|
|
|
|
"REG_DWORD_BIG_ENDIAN",
|
|
|
|
"REG_LINK",
|
|
|
|
"REG_MULTI_SZ",
|
|
|
|
"REG_RESOURCE_LIST",
|
|
|
|
"REG_FULL_RESOURCE_DESCRIPTOR",
|
|
|
|
"REG_RESOURCE_REQUIREMENTS_LIST",
|
|
|
|
"REG_QWORD",
|
|
|
|
};
|
2002-06-03 10:57:55 +08:00
|
|
|
|
2001-01-11 06:34:02 +08:00
|
|
|
int listwhat = 0;
|
|
|
|
int postfix = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
int verbose = 0;
|
|
|
|
int quiet = 0;
|
2006-10-22 01:19:29 +08:00
|
|
|
int hex = 0;
|
2006-10-17 02:00:06 +08:00
|
|
|
DWORD wow64 = 0;
|
2015-02-03 23:14:57 +08:00
|
|
|
DWORD restore_flags = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
char **argv;
|
|
|
|
|
|
|
|
HKEY key;
|
2009-12-16 22:02:57 +08:00
|
|
|
wchar_t *value;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-03 10:57:55 +08:00
|
|
|
static void
|
|
|
|
usage (FILE *where = stderr)
|
|
|
|
{
|
|
|
|
fprintf (where, ""
|
2006-10-22 01:19:29 +08:00
|
|
|
"Usage: %s [OPTION] ACTION KEY [data...]\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2003-04-27 05:52:03 +08:00
|
|
|
"View or edit the Win32 registry\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
"\n", prog_name);
|
2002-06-03 10:57:55 +08:00
|
|
|
if (where == stdout)
|
2003-08-15 16:58:54 +08:00
|
|
|
{
|
2006-10-22 01:19:29 +08:00
|
|
|
fprintf (where, ""
|
|
|
|
"Actions:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" add KEY\\SUBKEY add new SUBKEY\n"
|
|
|
|
" check KEY exit 0 if KEY exists, 1 if not\n"
|
|
|
|
" get KEY\\VALUE prints VALUE to stdout\n"
|
|
|
|
" list KEY list SUBKEYs and VALUEs\n"
|
|
|
|
" remove KEY remove KEY\n"
|
|
|
|
" set KEY\\VALUE [data ...] set VALUE\n"
|
|
|
|
" unset KEY\\VALUE removes VALUE from KEY\n"
|
|
|
|
" load KEY\\SUBKEY PATH load hive from PATH into new SUBKEY\n"
|
|
|
|
" unload KEY\\SUBKEY unload hive and remove SUBKEY\n"
|
2015-02-03 23:14:57 +08:00
|
|
|
" save KEY\\SUBKEY PATH save SUBKEY into new file PATH\n"
|
|
|
|
" restore KEY\\SUBKEY PATH restore SUBKEY from file PATH\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
"\n");
|
|
|
|
fprintf (where, ""
|
2015-02-03 23:14:57 +08:00
|
|
|
"Options for 'list' action:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -k, --keys print only KEYs\n"
|
|
|
|
" -l, --list print only VALUEs\n"
|
|
|
|
" -p, --postfix like ls -p, appends '\\' postfix to KEY names\n"
|
|
|
|
"\n"
|
2015-02-03 23:14:57 +08:00
|
|
|
"Options for 'get' action:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -b, --binary print data as printable hex bytes\n"
|
|
|
|
" -n, --none print data as stream of bytes as stored in registry\n"
|
|
|
|
" -x, --hex print numerical data as hex numbers\n"
|
|
|
|
"\n"
|
2015-02-03 23:14:57 +08:00
|
|
|
"Options for 'set' action:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -b, --binary set type to REG_BINARY (hex args or '-')\n"
|
|
|
|
" -d, --dword set type to REG_DWORD\n"
|
2009-12-16 22:02:57 +08:00
|
|
|
" -D, --dword-be set type to REG_DWORD_BIG_ENDIAN\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -e, --expand-string set type to REG_EXPAND_SZ\n"
|
|
|
|
" -i, --integer set type to REG_DWORD\n"
|
|
|
|
" -m, --multi-string set type to REG_MULTI_SZ\n"
|
|
|
|
" -n, --none set type to REG_NONE\n"
|
|
|
|
" -Q, --qword set type to REG_QWORD\n"
|
|
|
|
" -s, --string set type to REG_SZ\n"
|
|
|
|
"\n"
|
|
|
|
"Options for 'set' and 'unset' Actions:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -K<c>, --key-separator[=]<c> set key-value separator to <c> instead of '\\'\n"
|
|
|
|
"\n"
|
2015-02-03 23:14:57 +08:00
|
|
|
"Options for 'restore' action:\n"
|
|
|
|
"\n"
|
|
|
|
" -f, --force restore even if open handles exist at or beneath the location\n"
|
|
|
|
" in the registry hierarchy to which KEY\\SUBKEY points\n"
|
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
"Other Options:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"\n"
|
2006-10-22 01:19:29 +08:00
|
|
|
" -h, --help output usage information and exit\n"
|
|
|
|
" -q, --quiet no error output, just nonzero return if KEY/VALUE missing\n"
|
|
|
|
" -v, --verbose verbose output, including VALUE contents when applicable\n"
|
|
|
|
" -w, --wow64 access 64 bit registry view (ignored on 32 bit Windows)\n"
|
|
|
|
" -W, --wow32 access 32 bit registry view (ignored on 32 bit Windows)\n"
|
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
"\n");
|
2003-08-15 16:58:54 +08:00
|
|
|
fprintf (where, ""
|
|
|
|
"KEY is in the format [host]\\prefix\\KEY\\KEY\\VALUE, where host is optional\n"
|
|
|
|
"remote host in either \\\\hostname or hostname: format and prefix is any of:\n"
|
|
|
|
" root HKCR HKEY_CLASSES_ROOT (local only)\n"
|
|
|
|
" config HKCC HKEY_CURRENT_CONFIG (local only)\n"
|
|
|
|
" user HKCU HKEY_CURRENT_USER (local only)\n"
|
|
|
|
" machine HKLM HKEY_LOCAL_MACHINE\n"
|
|
|
|
" users HKU HKEY_USERS\n"
|
|
|
|
"\n"
|
|
|
|
"If the keyname starts with a forward slash ('/'), the forward slash is used\n"
|
|
|
|
"as separator and the backslash can be used as escape character.\n");
|
|
|
|
fprintf (where, ""
|
|
|
|
"Example:\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"%s list '/machine/SOFTWARE/Classes/MIME/Database/Content Type/audio\\/wav'\n\n", prog_name);
|
2003-08-15 16:58:54 +08:00
|
|
|
}
|
2002-06-03 10:57:55 +08:00
|
|
|
if (where == stderr)
|
2006-10-22 01:19:29 +08:00
|
|
|
fprintf (where,
|
|
|
|
"ACTION is one of add, check, get, list, remove, set, unset, load, unload, save\n"
|
|
|
|
"\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"Try `%s --help' for more information.\n", prog_name);
|
2002-06-03 10:57:55 +08:00
|
|
|
exit (where == stderr ? 1 : 0);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-03 10:57:55 +08:00
|
|
|
static void
|
|
|
|
print_version ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
printf ("regtool (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"Registry tool\n"
|
|
|
|
"Copyright (C) 2000 - %s Red Hat, Inc.\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-23 17:44:36 +08:00
|
|
|
Fail (unsigned int rv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
if (!quiet)
|
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
|
|
|
| FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
0, rv, 0, (CHAR *) & buf, 0, 0);
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "Error (%d): %s\n", rv, buf);
|
2001-01-11 06:34:02 +08:00
|
|
|
LocalFree (buf);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-01-10 22:20:16 +08:00
|
|
|
exit (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2008-09-13 06:43:10 +08:00
|
|
|
static struct
|
2001-01-10 22:20:16 +08:00
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
const char *string;
|
|
|
|
HKEY key;
|
2001-01-10 22:20:16 +08:00
|
|
|
} wkprefixes[] =
|
|
|
|
{
|
|
|
|
{"root", HKEY_CLASSES_ROOT},
|
|
|
|
{"HKCR", HKEY_CLASSES_ROOT},
|
|
|
|
{"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT},
|
|
|
|
{"config", HKEY_CURRENT_CONFIG},
|
|
|
|
{"HKCC", HKEY_CURRENT_CONFIG},
|
|
|
|
{"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG},
|
|
|
|
{"user", HKEY_CURRENT_USER},
|
|
|
|
{"HKCU", HKEY_CURRENT_USER},
|
|
|
|
{"HKEY_CURRENT_USER", HKEY_CURRENT_USER},
|
|
|
|
{"machine", HKEY_LOCAL_MACHINE},
|
|
|
|
{"HKLM", HKEY_LOCAL_MACHINE},
|
|
|
|
{"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE},
|
|
|
|
{"users", HKEY_USERS},
|
|
|
|
{"HKU", HKEY_USERS},
|
|
|
|
{"HKEY_USERS", HKEY_USERS},
|
|
|
|
{0, 0}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-01-10 22:20:16 +08:00
|
|
|
void
|
|
|
|
translate (char *key)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#define isodigit(c) (strchr("01234567", c))
|
|
|
|
#define tooct(c) ((c)-'0')
|
|
|
|
#define tohex(c) (strchr(_hs,tolower(c))-_hs)
|
|
|
|
static char _hs[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
char *d = key;
|
|
|
|
char *s = key;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '\\')
|
2001-01-10 22:20:16 +08:00
|
|
|
switch (*++s)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
*d++ = '\007';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
*d++ = '\b';
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
*d++ = '\033';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
*d++ = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*d++ = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*d++ = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*d++ = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
*d++ = '\v';
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
c = tooct (*s);
|
|
|
|
if (isodigit (s[1]))
|
|
|
|
{
|
|
|
|
c = (c << 3) | tooct (*++s);
|
|
|
|
if (isodigit (s[1]))
|
|
|
|
c = (c << 3) | tooct (*++s);
|
|
|
|
}
|
|
|
|
*d++ = c;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
if (!isxdigit (s[1]))
|
2001-01-10 23:22:40 +08:00
|
|
|
c = '0';
|
2001-01-10 22:20:16 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
c = tohex (*++s);
|
|
|
|
if (isxdigit (s[1]))
|
|
|
|
c = (c << 4) | tohex (*++s);
|
|
|
|
}
|
|
|
|
*d++ = c;
|
|
|
|
break;
|
|
|
|
default: /* before non-special char: just add the char */
|
|
|
|
*d++ = *s;
|
|
|
|
break;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
else if (*s == '/')
|
2001-01-10 22:20:16 +08:00
|
|
|
*d++ = '\\';
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2001-01-10 22:20:16 +08:00
|
|
|
*d++ = *s;
|
2000-02-18 03:38:33 +08:00
|
|
|
++s;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-03-03 17:43:35 +08:00
|
|
|
find_key (int howmanyparts, REGSAM access, int option = 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-06-07 19:12:16 +08:00
|
|
|
HKEY base;
|
|
|
|
int rv;
|
|
|
|
char *n = argv[0], *e, *h, c;
|
|
|
|
char* host = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
int i;
|
2009-12-16 22:02:57 +08:00
|
|
|
size_t len;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (*n == '/')
|
2001-01-10 22:20:16 +08:00
|
|
|
translate (n);
|
2002-06-07 19:12:16 +08:00
|
|
|
if (*n != '\\')
|
|
|
|
{
|
|
|
|
/* expect host:/key/value format */
|
|
|
|
host = (char*) malloc (strlen (n) + 1);
|
2004-10-25 23:49:36 +08:00
|
|
|
host[0] = host [1] = '\\';
|
2002-06-07 19:12:16 +08:00
|
|
|
for (e = n, h = host + 2; *e && *e != ':'; e++, h++)
|
2004-10-25 23:49:36 +08:00
|
|
|
*h = *e;
|
2002-06-07 19:12:16 +08:00
|
|
|
*h = 0;
|
|
|
|
n = e + 1;
|
|
|
|
if (*n == '/')
|
2004-10-25 23:49:36 +08:00
|
|
|
translate (n);
|
2002-06-07 19:12:16 +08:00
|
|
|
}
|
|
|
|
else if (n[0] == '\\' && n[1] == '\\')
|
|
|
|
{
|
|
|
|
/* expect //host/key/value format */
|
|
|
|
host = (char*) malloc (strlen (n) + 1);
|
2004-10-25 23:49:36 +08:00
|
|
|
host[0] = host[1] = '\\';
|
2002-06-07 19:12:16 +08:00
|
|
|
for (e = n + 2, h = host + 2; *e && *e != '\\'; e++, h++)
|
2004-10-25 23:49:36 +08:00
|
|
|
*h = *e;
|
2002-06-07 19:12:16 +08:00
|
|
|
*h = 0;
|
|
|
|
n = e;
|
|
|
|
}
|
|
|
|
while (*n != '\\')
|
2000-02-18 03:38:33 +08:00
|
|
|
n++;
|
2002-06-07 19:12:16 +08:00
|
|
|
*n++ = 0;
|
2001-06-28 10:46:11 +08:00
|
|
|
for (e = n; *e && *e != '\\'; e++);
|
|
|
|
c = *e;
|
2000-02-18 03:38:33 +08:00
|
|
|
*e = 0;
|
2001-01-10 22:20:16 +08:00
|
|
|
for (i = 0; wkprefixes[i].string; i++)
|
|
|
|
if (strcmp (wkprefixes[i].string, n) == 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
if (!wkprefixes[i].string)
|
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
fprintf (stderr, "Unknown key prefix. Valid prefixes are:\n");
|
|
|
|
for (i = 0; wkprefixes[i].string; i++)
|
|
|
|
fprintf (stderr, "\t%s\n", wkprefixes[i].string);
|
|
|
|
exit (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n = e;
|
|
|
|
*e = c;
|
|
|
|
while (*n && *n == '\\')
|
|
|
|
n++;
|
2001-01-10 22:20:16 +08:00
|
|
|
e = n + strlen (n);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (howmanyparts > 1)
|
|
|
|
{
|
2002-08-13 23:37:32 +08:00
|
|
|
while (n < e && *e != key_sep)
|
2000-02-18 03:38:33 +08:00
|
|
|
e--;
|
2002-08-13 23:37:32 +08:00
|
|
|
if (*e != key_sep)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-07-15 19:27:23 +08:00
|
|
|
key = wkprefixes[i].key;
|
2009-12-16 22:02:57 +08:00
|
|
|
if (value)
|
|
|
|
free (value);
|
|
|
|
len = mbstowcs (NULL, n, 0) + 1;
|
2010-03-25 03:52:00 +08:00
|
|
|
value = (wchar_t *) malloc (len * sizeof (wchar_t));
|
2009-12-16 22:02:57 +08:00
|
|
|
mbstowcs (value, n, len);
|
2001-07-15 19:27:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2004-10-25 23:49:36 +08:00
|
|
|
{
|
2001-07-15 19:27:23 +08:00
|
|
|
*e = 0;
|
2009-12-16 22:02:57 +08:00
|
|
|
if (value)
|
|
|
|
free (value);
|
|
|
|
len = mbstowcs (NULL, e + 1, 0) + 1;
|
2010-03-25 03:52:00 +08:00
|
|
|
value = (wchar_t *) malloc (len * sizeof (wchar_t));
|
2009-12-16 22:02:57 +08:00
|
|
|
mbstowcs (value, e + 1, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
2002-06-07 19:12:16 +08:00
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
rv = RegConnectRegistry (host, wkprefixes[i].key, &base);
|
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
free (host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
base = wkprefixes[i].key;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (n[0] == 0)
|
2002-06-07 19:12:16 +08:00
|
|
|
key = base;
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
len = mbstowcs (NULL, n, 0) + 1;
|
|
|
|
wchar_t name[len];
|
|
|
|
mbstowcs (name, n, len);
|
2006-03-03 17:43:35 +08:00
|
|
|
if (access)
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegOpenKeyExW (base, name, 0, access | wow64, &key);
|
2006-03-03 17:43:35 +08:00
|
|
|
if (option && (rv == ERROR_SUCCESS || rv == ERROR_ACCESS_DENIED))
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
/* reopen with desired option due to missing option support in
|
|
|
|
RegOpenKeyE */
|
|
|
|
/* FIXME: may create the key in rare cases (e.g. access denied
|
|
|
|
in parent) */
|
2006-03-03 17:43:35 +08:00
|
|
|
HKEY key2;
|
2009-12-16 22:02:57 +08:00
|
|
|
if (RegCreateKeyExW (base, name, 0, NULL, option, access | wow64,
|
|
|
|
NULL, &key2, NULL)
|
2006-03-03 17:43:35 +08:00
|
|
|
== ERROR_SUCCESS)
|
2011-12-18 07:39:47 +08:00
|
|
|
{
|
2006-03-03 17:43:35 +08:00
|
|
|
if (rv == ERROR_SUCCESS)
|
|
|
|
RegCloseKey (key);
|
|
|
|
key = key2;
|
|
|
|
rv = ERROR_SUCCESS;
|
2011-12-18 07:39:47 +08:00
|
|
|
}
|
2006-03-03 17:43:35 +08:00
|
|
|
}
|
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
}
|
|
|
|
else if (argv[1])
|
2011-12-18 07:39:47 +08:00
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], NULL, 0);
|
|
|
|
wchar_t win32_path[len];
|
|
|
|
cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], win32_path, len);
|
|
|
|
rv = RegLoadKeyW (base, name, win32_path);
|
2006-03-03 17:43:35 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("key %ls loaded from file %ls\n", name, win32_path);
|
2006-03-03 17:43:35 +08:00
|
|
|
}
|
|
|
|
else
|
2011-12-18 07:39:47 +08:00
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegUnLoadKeyW (base, name);
|
2006-03-03 17:43:35 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("key %ls unloaded\n", name);
|
2006-03-03 17:43:35 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_list ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
DWORD num_subkeys, maxsubkeylen, num_values, maxvalnamelen, maxvaluelen;
|
|
|
|
DWORD maxclasslen;
|
2009-12-16 22:02:57 +08:00
|
|
|
wchar_t *subkey_name, *value_name, *class_name, *vd;
|
|
|
|
unsigned char *value_data;
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD i, j, m, n, t;
|
|
|
|
int v;
|
|
|
|
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (1, KEY_READ);
|
2009-12-16 22:02:57 +08:00
|
|
|
RegQueryInfoKeyW (key, 0, 0, 0, &num_subkeys, &maxsubkeylen, &maxclasslen,
|
|
|
|
&num_values, &maxvalnamelen, &maxvaluelen, 0, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-12-16 22:02:57 +08:00
|
|
|
subkey_name = (wchar_t *) malloc ((maxsubkeylen + 1) * sizeof (wchar_t));
|
|
|
|
class_name = (wchar_t *) malloc ((maxclasslen + 1) * sizeof (wchar_t));
|
|
|
|
value_name = (wchar_t *) malloc ((maxvalnamelen + 1) * sizeof (wchar_t));
|
2001-01-10 22:20:16 +08:00
|
|
|
value_data = (unsigned char *) malloc (maxvaluelen + 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-01-11 06:34:02 +08:00
|
|
|
if (!listwhat)
|
|
|
|
listwhat = LIST_ALL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-01-11 06:34:02 +08:00
|
|
|
if (listwhat & LIST_KEYS)
|
|
|
|
for (i = 0; i < num_subkeys; i++)
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
m = (maxsubkeylen + 1) * sizeof (wchar_t);
|
|
|
|
n = (maxclasslen + 1) * sizeof (wchar_t);
|
|
|
|
RegEnumKeyExW (key, i, subkey_name, &m, 0, class_name, &n, 0);
|
|
|
|
printf ("%ls", subkey_name);
|
2006-10-22 01:19:29 +08:00
|
|
|
if (postfix || verbose)
|
|
|
|
fputc (key_sep, stdout);
|
2001-01-11 06:34:02 +08:00
|
|
|
|
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf (" (%ls)", class_name);
|
2001-01-11 06:34:02 +08:00
|
|
|
|
|
|
|
puts ("");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listwhat & LIST_VALS)
|
|
|
|
for (i = 0; i < num_values; i++)
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
m = (maxvalnamelen + 1) * sizeof (wchar_t);
|
2001-01-11 06:34:02 +08:00
|
|
|
n = maxvaluelen + 1;
|
2009-12-16 22:02:57 +08:00
|
|
|
RegEnumValueW (key, i, value_name, &m, 0, &t, (BYTE *) value_data, &n);
|
2005-09-08 17:24:41 +08:00
|
|
|
value_data[n] = 0;
|
2001-01-11 06:34:02 +08:00
|
|
|
if (!verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("%ls\n", value_name);
|
2001-01-11 06:34:02 +08:00
|
|
|
else
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("%ls (%s) = ", value_name, types[t]);
|
2001-01-11 06:34:02 +08:00
|
|
|
switch (t)
|
|
|
|
{
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_NONE:
|
2001-01-11 06:34:02 +08:00
|
|
|
case REG_BINARY:
|
|
|
|
for (j = 0; j < 8 && j < n; j++)
|
|
|
|
printf ("%02x ", value_data[j]);
|
|
|
|
printf ("\n");
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
2013-04-23 17:44:36 +08:00
|
|
|
printf ("0x%08x (%u)\n", *(unsigned int *) value_data,
|
|
|
|
*(unsigned int *) value_data);
|
2001-01-11 06:34:02 +08:00
|
|
|
break;
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
v = ((value_data[0] << 24)
|
|
|
|
| (value_data[1] << 16)
|
2006-10-22 01:19:29 +08:00
|
|
|
| (value_data[2] << 8)
|
|
|
|
| (value_data[3]));
|
2001-01-11 06:34:02 +08:00
|
|
|
printf ("0x%08x (%d)\n", v, v);
|
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_QWORD:
|
|
|
|
printf ("0x%016llx (%llu)\n",
|
|
|
|
*(unsigned long long *) value_data,
|
|
|
|
*(unsigned long long *) value_data);
|
|
|
|
break;
|
2001-01-11 06:34:02 +08:00
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_SZ:
|
2009-12-16 22:02:57 +08:00
|
|
|
case REG_LINK:
|
|
|
|
printf ("\"%ls\"\n", (wchar_t *) value_data);
|
2001-01-11 06:34:02 +08:00
|
|
|
break;
|
|
|
|
case REG_MULTI_SZ:
|
2009-12-16 22:02:57 +08:00
|
|
|
vd = (wchar_t *) value_data;
|
2001-01-11 06:34:02 +08:00
|
|
|
while (vd && *vd)
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("\"%ls\"", vd);
|
|
|
|
vd = vd + wcslen (vd) + 1;
|
2001-01-11 06:34:02 +08:00
|
|
|
if (*vd)
|
|
|
|
printf (", ");
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
break;
|
|
|
|
default:
|
2006-10-22 01:19:29 +08:00
|
|
|
printf ("?\n");
|
|
|
|
break;
|
2001-01-11 06:34:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_add ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (2, KEY_ALL_ACCESS);
|
2000-02-18 03:38:33 +08:00
|
|
|
HKEY newkey;
|
|
|
|
DWORD newtype;
|
2009-12-16 22:02:57 +08:00
|
|
|
int rv = RegCreateKeyExW (key, value, 0, NULL, REG_OPTION_NON_VOLATILE,
|
|
|
|
KEY_ALL_ACCESS | wow64, 0, &newkey, &newtype);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
if (newtype == REG_OPENED_EXISTING_KEY)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("Key %ls already exists\n", value);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("Key %ls created\n", value);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-17 02:00:06 +08:00
|
|
|
extern "C" {
|
2012-07-06 22:52:33 +08:00
|
|
|
LONG WINAPI (*regDeleteKeyEx)(HKEY, LPCWSTR, REGSAM, DWORD);
|
2006-10-17 02:00:06 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_remove ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-10-17 02:00:06 +08:00
|
|
|
DWORD rv;
|
|
|
|
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (2, KEY_ALL_ACCESS);
|
2006-10-17 02:00:06 +08:00
|
|
|
if (wow64)
|
|
|
|
{
|
|
|
|
HMODULE mod = LoadLibrary ("advapi32.dll");
|
|
|
|
if (mod)
|
2012-07-06 22:52:33 +08:00
|
|
|
regDeleteKeyEx = (LONG WINAPI (*)(HKEY, LPCWSTR, REGSAM, DWORD)) GetProcAddress (mod, "RegDeleteKeyExW");
|
2006-10-17 02:00:06 +08:00
|
|
|
}
|
|
|
|
if (regDeleteKeyEx)
|
|
|
|
rv = (*regDeleteKeyEx) (key, value, wow64, 0);
|
|
|
|
else
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegDeleteKeyW (key, value);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("subkey %ls deleted\n", value);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_check ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (1, KEY_READ);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (verbose)
|
2001-01-10 22:20:16 +08:00
|
|
|
printf ("key %s exists\n", argv[0]);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_set ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
int i, n, max_n;
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD v, rv;
|
2006-10-22 01:19:29 +08:00
|
|
|
unsigned long long llval;
|
2006-03-03 17:43:35 +08:00
|
|
|
char *a = argv[1], *data = 0;
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (2, KEY_ALL_ACCESS);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-10-22 01:19:29 +08:00
|
|
|
if (!a)
|
|
|
|
usage ();
|
|
|
|
if (value_type == REG_AUTO)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *e;
|
2006-10-22 01:19:29 +08:00
|
|
|
llval = strtoull (a, &e, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (a[0] == '%')
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_EXPAND_SZ;
|
2000-02-18 03:38:33 +08:00
|
|
|
else if (a[0] && !*e)
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = llval > 0xffffffffULL ? REG_QWORD : REG_DWORD;
|
2000-02-18 03:38:33 +08:00
|
|
|
else if (argv[2])
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_MULTI_SZ;
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_SZ;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2006-10-22 01:19:29 +08:00
|
|
|
switch (value_type)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_NONE:
|
|
|
|
case REG_BINARY:
|
2006-03-03 17:43:35 +08:00
|
|
|
for (n = 0; argv[n+1]; n++)
|
2011-12-18 07:39:47 +08:00
|
|
|
;
|
2006-03-03 17:43:35 +08:00
|
|
|
if (n == 1 && strcmp (argv[1], "-") == 0)
|
|
|
|
{ /* read from stdin */
|
|
|
|
i = n = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (i <= n)
|
|
|
|
{
|
|
|
|
i = n + BUFSIZ;
|
|
|
|
data = (char *) realloc (data, i);
|
|
|
|
}
|
|
|
|
int r = fread (data+n, 1, i-n, stdin);
|
|
|
|
if (r <= 0)
|
|
|
|
break;
|
|
|
|
n += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (n > 0)
|
|
|
|
{ /* parse hex from argv */
|
|
|
|
data = (char *) malloc (n);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
errno = 0;
|
|
|
|
v = strtoul (argv[i+1], &e, 16);
|
|
|
|
if (errno || v > 0xff || *e)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Invalid hex constant `%s'\n", argv[i+1]);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
data[i] = (char) v;
|
|
|
|
}
|
|
|
|
}
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegSetValueExW (key, value, 0, value_type, (const BYTE *) data, n);
|
2006-03-03 17:43:35 +08:00
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_DWORD:
|
2001-01-10 22:20:16 +08:00
|
|
|
v = strtoul (a, 0, 0);
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegSetValueExW (key, value, 0, REG_DWORD, (const BYTE *) &v,
|
2001-01-10 22:20:16 +08:00
|
|
|
sizeof (v));
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
v = strtoul (a, 0, 0);
|
|
|
|
v = (((v & 0xff) << 24)
|
|
|
|
| ((v & 0xff00) << 8)
|
|
|
|
| ((v & 0xff0000) >> 8)
|
|
|
|
| ((v & 0xff000000) >> 24));
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegSetValueExW (key, value, 0, REG_DWORD_BIG_ENDIAN,
|
2006-10-22 01:19:29 +08:00
|
|
|
(const BYTE *) &v, sizeof (v));
|
|
|
|
break;
|
|
|
|
case REG_QWORD:
|
|
|
|
llval = strtoul (a, 0, 0);
|
2009-12-16 22:02:57 +08:00
|
|
|
rv = RegSetValueExW (key, value, 0, REG_QWORD, (const BYTE *) &llval,
|
2006-10-22 01:19:29 +08:00
|
|
|
sizeof (llval));
|
|
|
|
break;
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
2011-03-30 16:22:59 +08:00
|
|
|
{
|
|
|
|
n = mbstowcs (NULL, a, 0);
|
|
|
|
wchar_t w[n + 1];
|
|
|
|
mbstowcs (w, a, n + 1);
|
|
|
|
rv = RegSetValueExW (key, value, 0, value_type,
|
|
|
|
(const BYTE *) w, (n + 1) * sizeof (wchar_t));
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_MULTI_SZ:
|
2009-12-16 22:02:57 +08:00
|
|
|
for (i = 1, max_n = 1; argv[i]; i++)
|
|
|
|
max_n += mbstowcs (NULL, argv[i], 0) + 1;
|
|
|
|
data = (char *) malloc (max_n * sizeof (wchar_t));
|
2001-01-10 22:20:16 +08:00
|
|
|
for (i = 1, n = 0; argv[i]; i++)
|
2009-12-16 22:02:57 +08:00
|
|
|
n += mbstowcs ((wchar_t *) data + n, argv[i], max_n - n) + 1;
|
|
|
|
((wchar_t *)data)[n] = L'\0';
|
|
|
|
rv = RegSetValueExW (key, value, 0, REG_MULTI_SZ, (const BYTE *) data,
|
2010-10-31 09:41:19 +08:00
|
|
|
(n + 1) * sizeof (wchar_t));
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case REG_AUTO:
|
2001-01-10 22:20:16 +08:00
|
|
|
rv = ERROR_SUCCESS;
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2001-01-10 23:22:40 +08:00
|
|
|
default:
|
|
|
|
rv = ERROR_INVALID_CATEGORY;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2011-12-18 07:39:47 +08:00
|
|
|
|
2006-03-03 17:43:35 +08:00
|
|
|
if (data)
|
|
|
|
free(data);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_unset ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (2, KEY_ALL_ACCESS);
|
2009-12-16 22:02:57 +08:00
|
|
|
DWORD rv = RegDeleteValueW (key, value);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("value %ls deleted\n", value);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
cmd_get ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-01-10 22:20:16 +08:00
|
|
|
find_key (2, KEY_READ);
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD vtype, dsize, rv;
|
2009-12-16 22:02:57 +08:00
|
|
|
PBYTE data;
|
|
|
|
wchar_t *vd;
|
|
|
|
|
|
|
|
rv = RegQueryValueExW (key, value, 0, &vtype, 0, &dsize);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2009-12-16 22:02:57 +08:00
|
|
|
data = (PBYTE) malloc (dsize + 1);
|
|
|
|
rv = RegQueryValueExW (key, value, 0, &vtype, data, &dsize);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
2001-01-10 22:20:16 +08:00
|
|
|
Fail (rv);
|
2006-10-22 01:19:29 +08:00
|
|
|
if (value_type == REG_BINARY)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-10-22 01:19:29 +08:00
|
|
|
for (unsigned i = 0; i < dsize; i++)
|
|
|
|
printf ("%02x%c", (unsigned char)data[i],
|
|
|
|
(i < dsize-1 ? ' ' : '\n'));
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2006-10-22 01:19:29 +08:00
|
|
|
else if (value_type == REG_NONE)
|
|
|
|
fwrite (data, dsize, 1, stdout);
|
|
|
|
else
|
|
|
|
switch (vtype)
|
|
|
|
{
|
|
|
|
case REG_NONE:
|
|
|
|
case REG_BINARY:
|
|
|
|
fwrite (data, dsize, 1, stdout);
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
2013-04-23 17:44:36 +08:00
|
|
|
printf (hex ? "0x%08x\n" : "%u\n", *(unsigned int *) data);
|
2006-10-22 01:19:29 +08:00
|
|
|
break;
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
rv = ((data[0] << 24)
|
|
|
|
| (data[1] << 16)
|
|
|
|
| (data[2] << 8)
|
|
|
|
| (data[3]));
|
2013-04-23 17:44:36 +08:00
|
|
|
printf (hex ? "0x%08x\n" : "%u\n", (unsigned int) rv);
|
2006-10-22 01:19:29 +08:00
|
|
|
break;
|
|
|
|
case REG_QWORD:
|
|
|
|
printf (hex ? "0x%016llx\n" : "%llu\n", *(unsigned long long *) data);
|
|
|
|
break;
|
|
|
|
case REG_SZ:
|
2009-12-16 22:02:57 +08:00
|
|
|
case REG_LINK:
|
|
|
|
printf ("%ls\n", (wchar_t *) data);
|
2006-10-22 01:19:29 +08:00
|
|
|
break;
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
if (value_type == REG_EXPAND_SZ) // hack
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
wchar_t *buf;
|
2006-10-22 01:19:29 +08:00
|
|
|
DWORD bufsize;
|
2009-12-16 22:02:57 +08:00
|
|
|
bufsize = ExpandEnvironmentStringsW ((wchar_t *) data, 0, 0);
|
|
|
|
buf = (wchar_t *) malloc (bufsize + 1);
|
|
|
|
ExpandEnvironmentStringsW ((wchar_t *) data, buf, bufsize + 1);
|
2006-10-22 01:19:29 +08:00
|
|
|
free (data);
|
2009-12-16 22:02:57 +08:00
|
|
|
data = (PBYTE) buf;
|
2006-10-22 01:19:29 +08:00
|
|
|
}
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("%ls\n", (wchar_t *) data);
|
2006-10-22 01:19:29 +08:00
|
|
|
break;
|
|
|
|
case REG_MULTI_SZ:
|
2009-12-16 22:02:57 +08:00
|
|
|
vd = (wchar_t *) data;
|
2006-10-22 01:19:29 +08:00
|
|
|
while (vd && *vd)
|
|
|
|
{
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("%ls\n", vd);
|
|
|
|
vd = vd + wcslen (vd) + 1;
|
2006-10-22 01:19:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-03 17:43:35 +08:00
|
|
|
int
|
|
|
|
cmd_load ()
|
|
|
|
{
|
|
|
|
if (!argv[1])
|
|
|
|
{
|
|
|
|
usage ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
find_key (1, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_unload ()
|
|
|
|
{
|
|
|
|
if (argv[1])
|
|
|
|
{
|
|
|
|
usage ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
find_key (1, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_save ()
|
|
|
|
{
|
|
|
|
if (!argv[1])
|
|
|
|
{
|
|
|
|
usage ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* REG_OPTION_BACKUP_RESTORE is necessary to save /HKLM/SECURITY */
|
|
|
|
find_key (1, KEY_QUERY_VALUE, REG_OPTION_BACKUP_RESTORE);
|
2009-12-16 22:02:57 +08:00
|
|
|
ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], NULL, 0);
|
|
|
|
wchar_t win32_path[len];
|
|
|
|
cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], win32_path, len);
|
|
|
|
DWORD rv = RegSaveKeyW (key, win32_path, NULL);
|
2006-03-03 17:43:35 +08:00
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
if (verbose)
|
2009-12-16 22:02:57 +08:00
|
|
|
printf ("key saved to %ls\n", win32_path);
|
2006-03-03 17:43:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-03 23:14:57 +08:00
|
|
|
int
|
|
|
|
cmd_restore ()
|
|
|
|
{
|
|
|
|
if (!argv[1])
|
|
|
|
{
|
|
|
|
usage ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* REG_OPTION_BACKUP_RESTORE is necessary to restore /HKLM/SECURITY */
|
|
|
|
find_key (1, KEY_ALL_ACCESS, REG_OPTION_BACKUP_RESTORE);
|
|
|
|
ssize_t len = cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], NULL, 0);
|
|
|
|
wchar_t win32_path[len];
|
|
|
|
cygwin_conv_path (CCP_POSIX_TO_WIN_W, argv[1], win32_path, len);
|
|
|
|
DWORD rv = RegRestoreKeyW (key, win32_path, restore_flags);
|
|
|
|
if (rv != ERROR_SUCCESS)
|
|
|
|
Fail (rv);
|
|
|
|
if (verbose)
|
|
|
|
printf ("key saved to %ls\n", win32_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-13 06:43:10 +08:00
|
|
|
static struct
|
2001-01-10 22:20:16 +08:00
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
const char *name;
|
2001-01-10 22:20:16 +08:00
|
|
|
int (*func) ();
|
|
|
|
} commands[] =
|
|
|
|
{
|
|
|
|
{"list", cmd_list},
|
|
|
|
{"add", cmd_add},
|
|
|
|
{"remove", cmd_remove},
|
|
|
|
{"check", cmd_check},
|
|
|
|
{"set", cmd_set},
|
|
|
|
{"unset", cmd_unset},
|
|
|
|
{"get", cmd_get},
|
2006-03-03 17:43:35 +08:00
|
|
|
{"load", cmd_load},
|
|
|
|
{"unload", cmd_unload},
|
|
|
|
{"save", cmd_save},
|
2015-02-03 23:14:57 +08:00
|
|
|
{"restore", cmd_restore},
|
2001-01-10 22:20:16 +08:00
|
|
|
{0, 0}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2001-01-10 22:20:16 +08:00
|
|
|
main (int argc, char **_argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-06-03 10:57:55 +08:00
|
|
|
int g;
|
|
|
|
|
2009-12-16 22:02:57 +08:00
|
|
|
setlocale (LC_ALL, "");
|
2011-10-10 22:57:48 +08:00
|
|
|
|
|
|
|
prog_name = program_invocation_short_name;
|
2002-06-03 10:57:55 +08:00
|
|
|
|
|
|
|
while ((g = getopt_long (argc, _argv, opts, longopts, NULL)) != EOF)
|
|
|
|
switch (g)
|
2001-01-10 22:20:16 +08:00
|
|
|
{
|
2006-03-03 17:43:35 +08:00
|
|
|
case 'b':
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_BINARY;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
value_type = REG_DWORD;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
value_type = REG_DWORD_BIG_ENDIAN;
|
2006-03-03 17:43:35 +08:00
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
case 'e':
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_EXPAND_SZ;
|
2001-01-11 06:34:02 +08:00
|
|
|
break;
|
2015-02-03 23:14:57 +08:00
|
|
|
case 'f':
|
|
|
|
restore_flags = REG_FORCE_RESTORE;
|
|
|
|
break;
|
2001-01-11 06:34:02 +08:00
|
|
|
case 'k':
|
|
|
|
listwhat |= LIST_KEYS;
|
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
case 'i':
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_DWORD;
|
2002-06-03 10:57:55 +08:00
|
|
|
break;
|
2001-01-11 06:34:02 +08:00
|
|
|
case 'l':
|
|
|
|
listwhat |= LIST_VALS;
|
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
case 'm':
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_MULTI_SZ;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
value_type = REG_NONE;
|
2002-06-03 10:57:55 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
postfix++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet++;
|
2001-01-10 22:20:16 +08:00
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case 'Q':
|
|
|
|
value_type = REG_QWORD;
|
|
|
|
break;
|
2001-01-10 22:20:16 +08:00
|
|
|
case 's':
|
2006-10-22 01:19:29 +08:00
|
|
|
value_type = REG_SZ;
|
2001-01-10 22:20:16 +08:00
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
2001-01-10 22:20:16 +08:00
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
case 'V':
|
|
|
|
print_version ();
|
|
|
|
exit (0);
|
2006-10-17 02:00:06 +08:00
|
|
|
case 'w':
|
|
|
|
wow64 = KEY_WOW64_64KEY;
|
|
|
|
break;
|
2006-10-19 17:08:44 +08:00
|
|
|
case 'W':
|
|
|
|
wow64 = KEY_WOW64_32KEY;
|
|
|
|
break;
|
2006-10-22 01:19:29 +08:00
|
|
|
case 'x':
|
|
|
|
hex++;
|
|
|
|
break;
|
2002-08-13 23:37:32 +08:00
|
|
|
case 'K':
|
2005-02-28 01:55:54 +08:00
|
|
|
key_sep = *optarg;
|
2002-08-13 23:37:32 +08:00
|
|
|
break;
|
2002-06-03 10:57:55 +08:00
|
|
|
default :
|
2011-10-10 22:57:48 +08:00
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n",
|
|
|
|
prog_name);
|
|
|
|
return 1;
|
2001-01-10 22:20:16 +08:00
|
|
|
}
|
2002-06-03 10:57:55 +08:00
|
|
|
|
|
|
|
if ((_argv[optind] == NULL) || (_argv[optind+1] == NULL))
|
2001-01-10 22:20:16 +08:00
|
|
|
usage ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-01-10 22:20:16 +08:00
|
|
|
argv = _argv + optind;
|
2000-02-18 03:38:33 +08:00
|
|
|
int i;
|
2001-01-10 22:20:16 +08:00
|
|
|
for (i = 0; commands[i].name; i++)
|
|
|
|
if (strcmp (commands[i].name, argv[0]) == 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
argv++;
|
2001-01-10 22:20:16 +08:00
|
|
|
return commands[i].func ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-01-10 22:20:16 +08:00
|
|
|
usage ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|