Regenerable files cleanup
[middleman.git] / libntlm / version.c
1 /* version.c    version handling
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3  * Copyright (C) 2002  Simon Josefsson
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 /* This file is based on src/global.c from Werner Koch's libgcrypt */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 static const char*
31 parse_version_number( const char *s, int *number )
32 {
33     int val = 0;
34
35     if( *s == '0' && isdigit(s[1]) )
36         return NULL; /* leading zeros are not allowed */
37     for ( ; isdigit(*s); s++ ) {
38         val *= 10;
39         val += *s - '0';
40     }
41     *number = val;
42     return val < 0? NULL : s;
43 }
44
45
46 static const char *
47 parse_version_string( const char *s, int *major, int *minor, int *micro )
48 {
49     s = parse_version_number( s, major );
50     if( !s || *s != '.' )
51         return NULL;
52     s++;
53     s = parse_version_number( s, minor );
54     if( !s || *s != '.' )
55         return NULL;
56     s++;
57     s = parse_version_number( s, micro );
58     if( !s )
59         return NULL;
60     return s; /* patchlevel */
61 }
62
63 /****************
64  * Check that the the version of the library is at minimum the requested one
65  * and return the version string; return NULL if the condition is not
66  * satisfied.  If a NULL is passed to this function, no check is done,
67  * but the version string is simply returned.
68  */
69 const char *
70 ntlm_check_version( const char *req_version )
71 {
72     const char *ver = VERSION;
73     int my_major, my_minor, my_micro;
74     int rq_major, rq_minor, rq_micro;
75     const char *my_plvl, *rq_plvl;
76
77     if ( !req_version )
78         return ver;
79
80     my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro );
81     if ( !my_plvl )
82         return NULL;  /* very strange our own version is bogus */
83     rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor,
84                                                                 &rq_micro );
85     if ( !rq_plvl )
86         return NULL;  /* req version string is invalid */
87
88     if ( my_major > rq_major
89         || (my_major == rq_major && my_minor > rq_minor)
90         || (my_major == rq_major && my_minor == rq_minor
91                                  && my_micro > rq_micro)
92         || (my_major == rq_major && my_minor == rq_minor
93                                  && my_micro == rq_micro
94                                  && strcmp( my_plvl, rq_plvl ) >= 0) ) {
95         return ver;
96     }
97     return NULL;
98 }