2005-10-07 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / w32reg.c
bloba85ac734852279307f7c8c6251f6de4db6ae59bf
1 /* w32reg.c - MS-Windows Registry access
2 * Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <config.h>
22 #ifdef HAVE_W32_SYSTEM
23 /* This module is only used in this environment */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <windows.h>
31 #include "util.h"
32 #include "sysutils.h"
34 static HKEY
35 get_root_key(const char *root)
37 HKEY root_key;
39 if( !root )
40 root_key = HKEY_CURRENT_USER;
41 else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
42 root_key = HKEY_CLASSES_ROOT;
43 else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
44 root_key = HKEY_CURRENT_USER;
45 else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
46 root_key = HKEY_LOCAL_MACHINE;
47 else if( !strcmp( root, "HKEY_USERS" ) )
48 root_key = HKEY_USERS;
49 else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
50 root_key = HKEY_PERFORMANCE_DATA;
51 else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
52 root_key = HKEY_CURRENT_CONFIG;
53 else
54 return NULL;
56 return root_key;
60 /****************
61 * Return a string from the Win32 Registry or NULL in case of
62 * error. Caller must release the return value. A NULL for root
63 * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
64 * NOTE: The value is allocated with a plain malloc() - use free() and not
65 * the usual m_free()!!!
67 char *
68 read_w32_registry_string( const char *root, const char *dir, const char *name )
70 HKEY root_key, key_handle;
71 DWORD n1, nbytes, type;
72 char *result = NULL;
74 if ( !(root_key = get_root_key(root) ) )
75 return NULL;
77 if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
79 if (root)
80 return NULL; /* no need for a RegClose, so return direct */
81 /* It seems to be common practise to fall back to HLM. */
82 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
83 return NULL; /* still no need for a RegClose, so return direct */
86 nbytes = 1;
87 if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
88 goto leave;
89 result = malloc( (n1=nbytes+1) );
90 if( !result )
91 goto leave;
92 if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
93 free(result); result = NULL;
94 goto leave;
96 result[nbytes] = 0; /* make sure it is really a string */
97 if (type == REG_EXPAND_SZ && strchr (result, '%')) {
98 char *tmp;
100 n1 += 1000;
101 tmp = malloc (n1+1);
102 if (!tmp)
103 goto leave;
104 nbytes = ExpandEnvironmentStrings (result, tmp, n1);
105 if (nbytes && nbytes > n1) {
106 free (tmp);
107 n1 = nbytes;
108 tmp = malloc (n1 + 1);
109 if (!tmp)
110 goto leave;
111 nbytes = ExpandEnvironmentStrings (result, tmp, n1);
112 if (nbytes && nbytes > n1) {
113 free (tmp); /* oops - truncated, better don't expand at all */
114 goto leave;
116 tmp[nbytes] = 0;
117 free (result);
118 result = tmp;
120 else if (nbytes) { /* okay, reduce the length */
121 tmp[nbytes] = 0;
122 free (result);
123 result = malloc (strlen (tmp)+1);
124 if (!result)
125 result = tmp;
126 else {
127 strcpy (result, tmp);
128 free (tmp);
131 else { /* error - don't expand */
132 free (tmp);
136 leave:
137 RegCloseKey( key_handle );
138 return result;
143 write_w32_registry_string(const char *root, const char *dir,
144 const char *name, const char *value)
146 HKEY root_key, reg_key;
148 if ( !(root_key = get_root_key(root) ) )
149 return -1;
151 if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key )
152 != ERROR_SUCCESS )
153 return -1;
155 if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
156 strlen( value ) ) != ERROR_SUCCESS ) {
157 if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
158 RegCloseKey(reg_key);
159 return -1;
161 if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
162 strlen( value ) ) != ERROR_SUCCESS ) {
163 RegCloseKey(reg_key);
164 return -1;
168 RegCloseKey( reg_key );
170 return 0;
173 #endif /*HAVE_W32_SYSTEM*/