1 /* init.c - Initialize the GnuPG error library.
2 Copyright (C) 2005 g10 Code GmbH
4 This file is part of libgpg-error.
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include <gpg-error.h>
35 /* Locale directory support. */
38 /* The implementation follows below. */
39 static char *get_locale_dir (void);
40 static void drop_locale_dir (char *locale_dir);
42 #define get_locale_dir() LOCALEDIR
43 #define drop_locale_dir(dir)
47 /* Initialize the library. This function should be run early. */
54 /* We only have to bind our locale directory to our text domain. */
56 locale_dir = get_locale_dir ();
59 bindtextdomain (PACKAGE, locale_dir);
60 drop_locale_dir (locale_dir);
69 #ifdef HAVE_W32_SYSTEM
74 get_root_key(const char *root)
79 root_key = HKEY_CURRENT_USER;
80 else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
81 root_key = HKEY_CLASSES_ROOT;
82 else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
83 root_key = HKEY_CURRENT_USER;
84 else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
85 root_key = HKEY_LOCAL_MACHINE;
86 else if( !strcmp( root, "HKEY_USERS" ) )
87 root_key = HKEY_USERS;
88 else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
89 root_key = HKEY_PERFORMANCE_DATA;
90 else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
91 root_key = HKEY_CURRENT_CONFIG;
98 * Return a string from the Win32 Registry or NULL in case of
99 * error. Caller must release the return value. A NULL for root
100 * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
101 * NOTE: The value is allocated with a plain malloc() - use free() and not
102 * the usual xfree()!!!
105 read_w32_registry_string( const char *root, const char *dir, const char *name )
107 HKEY root_key, key_handle;
108 DWORD n1, nbytes, type;
111 if ( !(root_key = get_root_key(root) ) )
114 if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
117 return NULL; /* no need for a RegClose, so return direct */
118 /* It seems to be common practise to fall back to HKLM. */
119 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
120 return NULL; /* still no need for a RegClose, so return direct */
124 if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) ) {
127 /* Try to fallback to HKLM also vor a missing value. */
128 RegCloseKey (key_handle);
129 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
130 return NULL; /* Nope. */
131 if (RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes))
134 result = malloc( (n1=nbytes+1) );
137 if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
138 free(result); result = NULL;
141 result[nbytes] = 0; /* make sure it is really a string */
142 if (type == REG_EXPAND_SZ && strchr (result, '%')) {
149 nbytes = ExpandEnvironmentStrings (result, tmp, n1);
150 if (nbytes && nbytes > n1) {
153 tmp = malloc (n1 + 1);
156 nbytes = ExpandEnvironmentStrings (result, tmp, n1);
157 if (nbytes && nbytes > n1) {
158 free (tmp); /* oops - truncated, better don't expand at all */
165 else if (nbytes) { /* okay, reduce the length */
168 result = malloc (strlen (tmp)+1);
172 strcpy (result, tmp);
176 else { /* error - don't expand */
182 RegCloseKey( key_handle );
187 #define REGKEY "Software\\GNU\\GnuPG"
190 get_locale_dir (void)
196 instdir = read_w32_registry_string ("HKEY_LOCAL_MACHINE", REGKEY,
197 "Install Directory");
201 /* Build the key: "<instdir>/share/locale". */
202 #define SLDIR "\\share\\locale"
203 dname = malloc (strlen (instdir) + strlen (SLDIR) + 1);
211 p += strlen (instdir);
221 drop_locale_dir (char *locale_dir)
226 #endif /* HAVE_W32_SYSTEM */