Add LIBTASN1_CFLAGS.
[shishi.git] / lib / version.c
blobafddc3f9967233eeb8e41812959e487aa56816ff
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.
69 * Return value: the actual version string of the library; NULL if the
70 * condition is not met. If %NULL is passed to this function no check
71 * is done and only the version string is returned. It is a pretty
72 * good idea to run this function as soon as possible, because it may
73 * also intializes some subsystems. In a multithreaded environment if
74 * should be called before any more threads are created.
75 **/
76 const char *
77 shishi_check_version (const char *req_version)
79 const char *ver = VERSION;
80 int my_major, my_minor, my_micro;
81 int rq_major, rq_minor, rq_micro;
82 const char *my_plvl, *rq_plvl;
84 if (!req_version)
85 return ver;
87 my_plvl = _shishi_parse_version_string (ver,
88 &my_major, &my_minor, &my_micro);
89 if (!my_plvl)
90 return NULL; /* very strange our own version is bogus */
91 rq_plvl = _shishi_parse_version_string (req_version, &rq_major, &rq_minor,
92 &rq_micro);
93 if (!rq_plvl)
94 return NULL; /* req version string is invalid */
96 if (my_major > rq_major
97 || (my_major == rq_major && my_minor > rq_minor)
98 || (my_major == rq_major && my_minor == rq_minor
99 && my_micro > rq_micro)
100 || (my_major == rq_major && my_minor == rq_minor
101 && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
103 return ver;
105 return NULL;