Fix.
[shishi.git] / lib / version.c
blob913ee5944af929f867276dc569389cf9f3a60d64
1 /* version.c version handling
2 * Copyright (C) 2002, 2003 Simon Josefsson
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 * This file is part of shishi.
7 * Shishi is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Shishi 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 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 shishi; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* This file is based on src/global.c in libgcrypt */
25 #include "internal.h"
27 static const char *
28 _shishi_parse_version_number (const char *s, int *number)
30 int val = 0;
32 if (*s == '0' && isdigit (s[1]))
33 return NULL; /* leading zeros are not allowed */
34 for (; isdigit (*s); s++)
36 val *= 10;
37 val += *s - '0';
39 *number = val;
40 return val < 0 ? NULL : s;
44 static const char *
45 _shishi_parse_version_string (const char *s, int *major, int *minor,
46 int *micro)
48 s = _shishi_parse_version_number (s, major);
49 if (!s || *s != '.')
50 return NULL;
51 s++;
52 s = _shishi_parse_version_number (s, minor);
53 if (!s || *s != '.')
54 return NULL;
55 s++;
56 s = _shishi_parse_version_number (s, micro);
57 if (!s)
58 return NULL;
59 return s; /* patchlevel */
62 /**
63 * shishi_check_version:
64 * @req_version: version string to compare with, or NULL
66 * Check that the the version of the library is at minimum the one
67 * given as a string in @req_version and return the actual version
68 * string of the library; return NULL if the condition is not met. If
69 * %NULL is passed to this function no check is done and only the
70 * version string is returned. It is a pretty good idea to run this
71 * function as soon as possible, because it may also intializes some
72 * subsystems. In a multithreaded environment if should be called
73 * before any more threads are created.
74 **/
75 const char *
76 shishi_check_version (const char *req_version)
78 const char *ver = VERSION;
79 int my_major, my_minor, my_micro;
80 int rq_major, rq_minor, rq_micro;
81 const char *my_plvl, *rq_plvl;
83 if (!req_version)
84 return ver;
86 my_plvl = _shishi_parse_version_string (ver,
87 &my_major, &my_minor, &my_micro);
88 if (!my_plvl)
89 return NULL; /* very strange our own version is bogus */
90 rq_plvl = _shishi_parse_version_string (req_version, &rq_major, &rq_minor,
91 &rq_micro);
92 if (!rq_plvl)
93 return NULL; /* req version string is invalid */
95 if (my_major > rq_major
96 || (my_major == rq_major && my_minor > rq_minor)
97 || (my_major == rq_major && my_minor == rq_minor
98 && my_micro > rq_micro)
99 || (my_major == rq_major && my_minor == rq_minor
100 && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
102 return ver;
104 return NULL;