1 /* version.c - Version check routines.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #ifdef HAVE_W32_SYSTEM
36 /* For _gpgme_sema_subsystem_init (). */
44 /* Bootstrap the subsystems needed for concurrent operation. This
45 must be done once at startup. We can not guarantee this using a
46 lock, though, because the semaphore subsystem needs to be
47 initialized itself before it can be used. So we expect that the
48 user performs the necessary synchronization. */
50 do_subsystem_inits (void)
57 _gpgme_sema_subsystem_init ();
59 assuan_set_assuan_log_level (0);
60 assuan_set_assuan_err_source (GPG_ERR_SOURCE_GPGME);
61 #endif /*HAVE_ASSUAN_H*/
62 _gpgme_debug_subsystem_init ();
63 _gpgme_io_subsystem_init ();
64 #if defined(HAVE_W32_SYSTEM) && defined(HAVE_ASSUAN_H)
65 /* We need to make sure that the sockets are initialized. */
69 WSAStartup (0x202, &wsadat);
71 #endif /*HAVE_W32_SYSTEM && HAVE_ASSUAN_H*/
77 /* Read the next number in the version string STR and return it in
78 *NUMBER. Return a pointer to the tail of STR after parsing, or
79 *NULL if the version string was invalid. */
81 parse_version_number (const char *str, int *number)
83 #define MAXVAL ((INT_MAX - 10) / 10)
86 /* Leading zeros are not allowed. */
87 if (*str == '0' && isdigit(str[1]))
90 while (isdigit (*str) && val <= MAXVAL)
93 val += *(str++) - '0';
96 return val > MAXVAL ? NULL : str;
100 /* Parse the version string STR in the format MAJOR.MINOR.MICRO (for
101 example, 9.3.2) and return the components in MAJOR, MINOR and MICRO
102 as integers. The function returns the tail of the string that
103 follows the version number. This might be te empty string if there
104 is nothing following the version number, or a patchlevel. The
105 function returns NULL if the version string is not valid. */
107 parse_version_string (const char *str, int *major, int *minor, int *micro)
109 str = parse_version_number (str, major);
110 if (!str || *str != '.')
114 str = parse_version_number (str, minor);
115 if (!str || *str != '.')
119 str = parse_version_number (str, micro);
123 /* A patchlevel might follow. */
128 /* Return true if MY_VERSION is at least REQ_VERSION, and false
131 _gpgme_compare_versions (const char *my_version,
132 const char *rq_version)
134 int my_major, my_minor, my_micro;
135 int rq_major, rq_minor, rq_micro;
136 const char *my_plvl, *rq_plvl;
143 my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
147 rq_plvl = parse_version_string (rq_version, &rq_major, &rq_minor, &rq_micro);
151 if (my_major > rq_major
152 || (my_major == rq_major && my_minor > rq_minor)
153 || (my_major == rq_major && my_minor == rq_minor
154 && my_micro > rq_micro)
155 || (my_major == rq_major && my_minor == rq_minor
156 && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
163 /* Check that the the version of the library is at minimum the
164 requested one and return the version string; return NULL if the
165 condition is not met. If a NULL is passed to this function, no
166 check is done and the version string is simply returned.
168 This function must be run once at startup, as it also initializes
169 some subsystems. Its invocation must be synchronized against
170 calling any of the other functions in a multi-threaded
173 gpgme_check_version (const char *req_version)
175 do_subsystem_inits ();
177 /* Catch-22: We need to get at least the debug subsystem ready
178 before using the tarce facility. If we won't the tarce would
179 automagically initialize the debug system with out the locks
180 being initialized and missing the assuan log level setting. */
181 TRACE2 (DEBUG_INIT, "gpgme_check_version: ", 0,
182 "req_version=%s, VERSION=%s",
183 req_version? req_version:"(null)", VERSION);
185 return _gpgme_compare_versions (VERSION, req_version) ? VERSION : NULL;
189 #define LINELENGTH 80
191 /* Extract the version string of a program from STRING. The version
192 number is expected to be in GNU style format:
195 foo (bar system) 1.2.3
197 foo (bar system) 1.2.3 cruft.
199 Spaces and tabs are skipped and used as delimiters, a term in
200 (nested) parenthesis before the version string is skipped, the
201 version string may consist of any non-space and non-tab characters
202 but needs to bstart with a digit.
205 extract_version_string (const char *string, size_t *r_len)
210 for (s=string; *s; s++)
211 if (*s == ' ' || *s == '\t')
213 while (*s == ' ' || *s == '\t')
217 for (count=1, s++; count && *s; s++)
223 /* For robustness we look for a digit. */
224 while ( *s && !(*s >= '0' && *s <= '9') )
226 if (*s >= '0' && *s <= '9')
228 for (len=0; s[len]; len++)
229 if (s[len] == ' ' || s[len] == '\t')
240 /* Retrieve the version number from the --version output of the
241 program FILE_NAME. */
243 _gpgme_get_program_version (const char *const file_name)
245 char line[LINELENGTH] = "";
250 char *argv[] = {NULL /* file_name */, "--version", 0};
251 struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0},
257 argv[0] = (char *) file_name;
259 if (_gpgme_io_pipe (rp, 1) < 0)
264 status = _gpgme_io_spawn (file_name, argv, cfd, NULL);
267 _gpgme_io_close (rp[0]);
268 _gpgme_io_close (rp[1]);
274 nread = _gpgme_io_read (rp[0], &line[linelen], LINELENGTH - linelen - 1);
277 line[linelen + nread] = '\0';
278 mark = strchr (&line[linelen], '\n');
281 if (mark > &line[0] && *mark == '\r')
289 while (nread > 0 && linelen < LINELENGTH - 1);
291 _gpgme_io_close (rp[0]);
298 s = extract_version_string (line, &len);
301 mark = malloc (len + 1);
304 memcpy (mark, s, len);