/[winpt]/trunk/MyGPGME/w32-util.c
ViewVC logotype

Contents of /trunk/MyGPGME/w32-util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File MIME type: text/plain
File size: 4972 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 /* w32-util.c - Utility functions for the W32 API
2 * Copyright (C) 1999 Free Software Foundation, Inc
3 * Copyright (C) 2001 Werner Koch (dd9jn)
4 * Copyright (C) 2002, 2005 Timo Schulz
5 *
6 * This file is part of MyGPGME.
7 *
8 * MyGPGME is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * MyGPGME is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <windows.h>
30 #include <process.h>
31 #include <io.h>
32
33 #include "util.h"
34
35
36 static char *
37 expand_path (const char *path)
38 {
39 DWORD len;
40 char *p;
41
42 len = ExpandEnvironmentStrings (path, NULL, 0);
43 if (!len)
44 return NULL;
45 len += 1;
46 p = calloc (1, len+1);
47 if (!p)
48 return NULL;
49 len = ExpandEnvironmentStrings (path, p, len);
50 if (!len) {
51 free (p);
52 return NULL;
53 }
54 return p;
55 }
56
57 /****************
58 * Return a string from the Win32 Registry or NULL in case of
59 * error. Caller must release the return value. A NULL for root
60 * is an alias fro HKEY_CURRENT_USER
61 */
62 static char *
63 read_w32_registry_string ( const char *root,
64 const char *dir, const char *name )
65 {
66 HKEY root_key, key_handle;
67 DWORD n1, nbytes;
68 DWORD type;
69 char *result = NULL;
70
71 if( !root )
72 root_key = HKEY_CURRENT_USER;
73 else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
74 root_key = HKEY_CLASSES_ROOT;
75 else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
76 root_key = HKEY_CURRENT_USER;
77 else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
78 root_key = HKEY_LOCAL_MACHINE;
79 else if( !strcmp( root, "HKEY_USERS" ) )
80 root_key = HKEY_USERS;
81 else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
82 root_key = HKEY_PERFORMANCE_DATA;
83 else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
84 root_key = HKEY_CURRENT_CONFIG;
85 else
86 return NULL;
87
88 if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
89 return NULL; /* no need for a RegClose, so return direct */
90
91 nbytes = 1;
92 if( RegQueryValueEx (key_handle, name, 0, NULL, NULL, &nbytes))
93 goto leave;
94 result = calloc (1, (n1=nbytes+1));
95 if (!result)
96 goto leave;
97 if (RegQueryValueEx (key_handle, name, 0, &type, result, &n1)) {
98 safe_free (result); result = NULL;
99 goto leave;
100 }
101 if (type == REG_EXPAND_SZ && strchr (result, '%')) {
102 char *p = expand_path (result);
103 DEBUG2 ("gpgProgram is expandable: %s -> %s\n", result, p);
104 free (result);
105 result = p;
106 }
107
108 leave:
109 RegCloseKey (key_handle);
110 return result;
111 }
112
113
114 const char *
115 _gpgme_get_gpg_path (int cleanup)
116 {
117 static char *gpg_program = NULL;
118
119 if (cleanup) {
120 if (gpg_program) {
121 free (gpg_program);
122 gpg_program = NULL;
123 }
124 return NULL;
125 }
126
127 if (!gpg_program) {
128 gpg_program = read_w32_registry_string (NULL,
129 "Software\\GNU\\GnuPG", "gpgProgram");
130 if (gpg_program) {
131 int i;
132
133 DEBUG1 ("found gpgProgram in registry: `%s'\n", gpg_program );
134 for (i=0; gpg_program[i]; i++) {
135 if (gpg_program[i] == '/')
136 gpg_program[i] = '\\';
137 }
138 }
139 else
140 gpg_program = strdup ("C:\\GNUPG\\GPG.EXE");
141 }
142
143 return gpg_program;
144 }
145
146
147 const char *
148 _gpgme_get_gpg_optfile (int cleanup)
149 {
150 static char *gpg_optfile = NULL;
151
152 if (cleanup) {
153 if (gpg_optfile) {
154 free (gpg_optfile);
155 gpg_optfile = NULL;
156 }
157 return NULL;
158 }
159
160 if (!gpg_optfile) {
161 FILE *fp;
162 gpg_optfile = read_w32_registry_string(
163 NULL, "Software\\GNU\\GnuPG", "OptFile" );
164 if (gpg_optfile) {
165 int i;
166 for (i = 0; gpg_optfile[i]; i++) {
167 if (gpg_optfile[i] == '/')
168 gpg_optfile[i] = '\\';
169 }
170 if (!strcmp(gpg_optfile, ""))
171 return NULL; /* Then ignore the file */
172 fp = fopen (gpg_optfile, "r");
173 if (fp == NULL)
174 return NULL;
175 fclose (fp);
176 }
177 }
178
179 return gpg_optfile;
180 } /* _gpgme_get_gpg_optfile */
181
182
183 void
184 util_cleanup (void)
185 {
186 _gpgme_get_gpg_optfile (1);
187 _gpgme_get_gpg_path (1);
188 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26