Check version.
[libidn.git] / version.c
blob1e2c95ec550839bb753d1c2359a40def17d2a4d7
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 free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this file; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* This file is based on src/global.c from Werner Koch's libgcrypt */
23 #include "internal.h"
25 static const char *
26 parse_version_number (const char *s, int *number)
28 int val = 0;
30 if (*s == '0' && isdigit (s[1]))
31 return NULL; /* leading zeros are not allowed */
32 for (; isdigit (*s); s++)
34 val *= 10;
35 val += *s - '0';
37 *number = val;
38 return val < 0 ? NULL : s;
42 static const char *
43 parse_version_string (const char *s, int *major, int *minor, int *micro)
45 s = parse_version_number (s, major);
46 if (!s || *s != '.')
47 return NULL;
48 s++;
49 s = parse_version_number (s, minor);
50 if (!s || *s != '.')
51 return NULL;
52 s++;
53 s = parse_version_number (s, micro);
54 if (!s)
55 return NULL;
56 return s; /* patchlevel */
59 /****************
60 * Check that the the version of the library is at minimum the requested one
61 * and return the version string; return NULL if the condition is not
62 * satisfied. If a NULL is passed to this function, no check is done,
63 * but the version string is simply returned.
65 const char *
66 stringprep_check_version (const char *req_version)
68 const char *ver = VERSION;
69 int my_major, my_minor, my_micro;
70 int rq_major, rq_minor, rq_micro;
71 const char *my_plvl, *rq_plvl;
73 if (!req_version)
74 return ver;
76 my_plvl = parse_version_string (ver, &my_major, &my_minor, &my_micro);
77 if (!my_plvl)
78 return NULL; /* very strange our own version is bogus */
79 rq_plvl = parse_version_string (req_version, &rq_major, &rq_minor,
80 &rq_micro);
81 if (!rq_plvl)
82 return NULL; /* req version string is invalid */
84 if (my_major > rq_major
85 || (my_major == rq_major && my_minor > rq_minor)
86 || (my_major == rq_major && my_minor == rq_minor
87 && my_micro > rq_micro)
88 || (my_major == rq_major && my_minor == rq_minor
89 && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
91 return ver;
93 return NULL;