common,w32: New function read_w32_reg_string.

* common/w32-reg.c (read_w32_reg_string): New.

* common/t-w32-reg.c (test_read_registry): Add another test.
This commit is contained in:
Werner Koch 2021-11-18 21:45:02 +01:00
parent 6ee01c1d26
commit 6c6c404883
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 74 additions and 18 deletions

View File

@ -44,25 +44,28 @@
static void
test_read_registry (void)
{
char *string;
char *string1, *string2;
#ifdef HAVE_W32CE_SYSTEM
string = read_w32_registry_string ("HKEY_CLASSES_ROOT",
"BOOTSTRAP\\CLSID", NULL);
if (!string)
fail (0);
fprintf (stderr, "Bootstrap clsid: %s\n", string);
xfree (string);
#endif
string = read_w32_registry_string
string1 = read_w32_registry_string
("HKEY_CURRENT_USER",
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"User Agent");
if (!string)
if (!string1)
fail (0);
fprintf (stderr, "User agent: %s\n", string);
xfree (string);
fprintf (stderr, "User agent: %s\n", string1);
string2 = read_w32_reg_string
("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion"
"\\Internet Settings:User Agent");
if (!string2)
fail (1);
fprintf (stderr, "User agent: %s\n", string2);
if (strcmp (string1, string2))
fail (2);
xfree (string1);
xfree (string2);
}
@ -71,10 +74,14 @@ test_read_registry (void)
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
test_read_registry ();
if (argc > 1)
{
char *string = read_w32_reg_string (argv[1]);
printf ("%s -> %s\n", argv[1], string? string : "(null)");
xfree (string);
}
else
test_read_registry ();
return 0;
}

View File

@ -226,5 +226,53 @@ read_w32_registry_string (const char *root, const char *dir, const char *name)
#endif /*!HAVE_W32CE_SYSTEM*/
}
/* Compact version of read_w32_registry_string. This version expects
* a single string as key described here using an example:
*
* HKCU\Software\GNU\GnuPG:HomeDir
*
* HKCU := the class, other supported classes are HKLM, HKCR, HKU, and
* HKCC. If no class is given and the string thus starts with
* a backslash HKCU with a fallback to HKLM is used.
* Software\GNU\GnuPG := The actual key.
* HomeDir := the name of the item. The name is optional to use the default
* value.
*
* Note that the first backslash and the first colon act as delimiters.
*
* Returns a malloced string or NULL if not found.
*/
char *
read_w32_reg_string (const char *key_arg)
{
char *key;
char *p1, *p2;
char *result;
if (!key_arg)
return NULL;
key = xtrystrdup (key_arg);
if (!key)
{
log_info ("warning: malloc failed while reading registry key\n");
return NULL;
}
p1 = strchr (key, '\\');
if (!p1)
{
xfree (key);
return NULL;
}
*p1++ = 0;
p2 = strchr (p1, ':');
if (p2)
*p2++ = 0;
result = read_w32_registry_string (*key? key : NULL, p1, p2);
xfree (key);
return result;
}
#endif /*HAVE_W32_SYSTEM*/

View File

@ -44,6 +44,7 @@ char **w32_parse_commandline (char *cmdline, int globing, int *r_argv,
/*-- w32-reg.c --*/
char *read_w32_registry_string (const char *root,
const char *dir, const char *name );
char *read_w32_reg_string (const char *key);
/* Other stuff. */
#ifdef HAVE_W32CE_SYSTEM