diff --git a/common/t-w32-reg.c b/common/t-w32-reg.c index 01816db54..9665003ea 100644 --- a/common/t-w32-reg.c +++ b/common/t-w32-reg.c @@ -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; } diff --git a/common/w32-reg.c b/common/w32-reg.c index d8d94b90e..f36c66453 100644 --- a/common/w32-reg.c +++ b/common/w32-reg.c @@ -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*/ diff --git a/common/w32help.h b/common/w32help.h index edb51b8b7..a79081f8e 100644 --- a/common/w32help.h +++ b/common/w32help.h @@ -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