1 /* version.c - version check
2 * Copyright (C) 2000 Werner Koch (dd9jn)
3 * Copyright (C) 2001 g10 Code GmbH
5 * This file is part of GPGME.
7 * GPGME is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GPGME is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 #include "key.h" /* for key_cache_init */
38 do_subsystem_inits (void)
44 _gpgme_sema_subsystem_init ();
45 _gpgme_key_cache_init ();
50 parse_version_number (const char *s, int *number)
54 if (*s == '0' && isdigit(s[1]))
55 return NULL; /* Leading zeros are not allowed. */
56 for (; isdigit(*s); s++)
62 return val < 0 ? NULL : s;
66 parse_version_string (const char *s, int *major, int *minor, int *micro)
68 s = parse_version_number (s, major);
72 s = parse_version_number (s, minor);
76 s = parse_version_number (s, micro);
79 return s; /* Patchlevel. */
83 _gpgme_compare_versions (const char *my_version,
84 const char *req_version)
86 int my_major, my_minor, my_micro;
87 int rq_major, rq_minor, rq_micro;
88 const char *my_plvl, *rq_plvl;
95 my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
97 return NULL; /* Very strange: our own version is bogus. */
98 rq_plvl = parse_version_string(req_version,
99 &rq_major, &rq_minor, &rq_micro);
101 return NULL; /* Requested version string is invalid. */
103 if (my_major > rq_major
104 || (my_major == rq_major && my_minor > rq_minor)
105 || (my_major == rq_major && my_minor == rq_minor
106 && my_micro > rq_micro)
107 || (my_major == rq_major && my_minor == rq_minor
108 && my_micro == rq_micro
109 && strcmp( my_plvl, rq_plvl ) >= 0))
117 * gpgme_check_version:
118 * @req_version: A string with a version
120 * Check that the the version of the library is at minimum the requested one
121 * and return the version string; return NULL if the condition is not
122 * met. If a NULL is passed to this function, no check is done and
123 * the version string is simply returned. It is a pretty good idea to
124 * run this function as soon as possible, because it also intializes
125 * some subsystems. In a multithreaded environment if should be called
126 * before the first thread is created.
128 * Return value: The version string or NULL
131 gpgme_check_version (const char *req_version)
133 do_subsystem_inits ();
134 return _gpgme_compare_versions (VERSION, req_version);
138 * gpgme_get_engine_info:
140 * Return information about the underlying crypto engines. This is an
141 * XML string with various information. A string is always returned
142 * even if the crypto engines is not installed; in this case a XML
143 * string with some error information is returned.
145 * Return value: A XML string with information about the crypto
149 gpgme_get_engine_info ()
151 static const char *engine_info;
152 DEFINE_STATIC_LOCK (engine_info_lock);
154 LOCK (engine_info_lock);
157 const char *openpgp_info = _gpgme_engine_get_info (GPGME_PROTOCOL_OpenPGP);
158 const char *cms_info = _gpgme_engine_get_info (GPGME_PROTOCOL_CMS);
161 if (!openpgp_info && !cms_info)
162 info = "<EngineInfo>\n</EngineInfo>\n";
163 else if (!openpgp_info || !cms_info)
165 const char *fmt = "<EngineInfo>\n"
169 info = xtrymalloc (strlen (fmt)
170 + strlen (openpgp_info
171 ? openpgp_info : cms_info) + 1);
173 sprintf (info, fmt, openpgp_info ? openpgp_info : cms_info);
177 const char *fmt = "<EngineInfo>\n"
180 info = xtrymalloc (strlen (fmt) + strlen (openpgp_info)
181 + strlen (cms_info) + 1);
183 sprintf (info, fmt, openpgp_info, cms_info);
186 info = "<EngineInfo>\n"
187 " <error>Out of core</error>\n"
191 UNLOCK (engine_info_lock);
197 * gpgme_check_engine:
199 * Check whether the installed crypto engine for the OpenPGP protocol
200 * matches the requirement of GPGME. This function is deprecated,
201 * instead use gpgme_engine_get_info() with the specific protocol you
204 * Return value: 0 or an error code.
207 gpgme_check_engine ()
209 return gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
213 #define LINELENGTH 80
216 _gpgme_get_program_version (const char *const path)
218 char line[LINELENGTH] = "";
223 char *argv[] = {NULL /* path */, "--version", 0};
224 struct spawn_fd_item_s pfd[] = { {0, -1}, {-1, -1} };
225 struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */}, {-1, -1} };
230 argv[0] = (char *) path;
232 if (_gpgme_io_pipe (rp, 1) < 0)
238 status = _gpgme_io_spawn (path, argv, cfd, pfd);
241 _gpgme_io_close (rp[0]);
242 _gpgme_io_close (rp[1]);
248 nread = _gpgme_io_read (rp[0], &line[linelen], LINELENGTH - linelen - 1);
251 line[linelen + nread] = '\0';
252 mark = strchr (&line[linelen], '\n');
261 while (nread > 0 && linelen < LINELENGTH - 1);
263 _gpgme_io_close (rp[0]);
267 mark = strrchr (line, ' ');
270 return xtrystrdup (mark + 1);