Initial release, version 0.0.0.
[gsasl.git] / lib / version.c
blob4f8ad9d9b1e3453e112b06fa4dce8a88fdf9b952
1 /* version.c version handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 * Copyright (C) 2002 Simon Josefsson
5 * This file is part of libgsasl.
7 * Libgsasl 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 * Libgsasl 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 libgsasl; 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 _gsasl_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++ ) {
35 val *= 10;
36 val += *s - '0';
38 *number = val;
39 return val < 0? NULL : s;
43 static const char *
44 _gsasl_parse_version_string( const char *s, int *major, int *minor, int *micro )
46 s = _gsasl_parse_version_number( s, major );
47 if( !s || *s != '.' )
48 return NULL;
49 s++;
50 s = _gsasl_parse_version_number( s, minor );
51 if( !s || *s != '.' )
52 return NULL;
53 s++;
54 s = _gsasl_parse_version_number( s, micro );
55 if( !s )
56 return NULL;
57 return s; /* patchlevel */
60 /**
61 * gsasl_check_version:
62 * @req_version: version string to compare with, or NULL
64 * Check that the the version of the library is at minimum the one
65 * given as a string in @var{req_version} and return the actual
66 * version string of the library; return NULL if the condition is not
67 * met. If @code{NULL} is passed to this function no check is done
68 * and only the version string is returned. It is a pretty good idea
69 * to run this function as soon as possible, because it may also
70 * intializes some subsystems. In a multithreaded environment if
71 * should be called before any more threads are created.
72 **/
73 const char *
74 gsasl_check_version (const char *req_version)
76 const char *ver = VERSION;
77 int my_major, my_minor, my_micro;
78 int rq_major, rq_minor, rq_micro;
79 const char *my_plvl, *rq_plvl;
81 if ( !req_version )
82 return ver;
84 my_plvl = _gsasl_parse_version_string( ver,
85 &my_major, &my_minor, &my_micro );
86 if ( !my_plvl )
87 return NULL; /* very strange our own version is bogus */
88 rq_plvl = _gsasl_parse_version_string( req_version, &rq_major, &rq_minor,
89 &rq_micro );
90 if ( !rq_plvl )
91 return NULL; /* req version string is invalid */
93 if ( my_major > rq_major
94 || (my_major == rq_major && my_minor > rq_minor)
95 || (my_major == rq_major && my_minor == rq_minor
96 && my_micro > rq_micro)
97 || (my_major == rq_major && my_minor == rq_minor
98 && my_micro == rq_micro
99 && strcmp( my_plvl, rq_plvl ) >= 0) ) {
100 return ver;
102 return NULL;