7b67dc21446cf6ed916cc1eb6881429172171329
5 * Compare version numbers using the Debian algorithm
7 * (c) 2007 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
35 #include "versioncmp.h"
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @versioncmp@ --- *
41 * Arguments: @const char *va, *vb@ = two version strings
43 * Returns: Less than, equal to, or greater than zero, according to
44 * whether @va@ is less than, equal to, or greater than @vb@.
46 * Use: Compares version number strings.
48 * The algorithm is an extension of the Debian version
49 * comparison algorithm. A version number consists of three
52 * [EPOCH :] MAIN [- SUB]
54 * The MAIN part may contain colons or hyphens if there is an
55 * EPOCH or SUB, respectively. Version strings are compared
56 * componentwise: first epochs, then main parts, and finally
59 * The component comparison is done as follows. First, the
60 * initial subsequence of nondigit characters is extracted from
61 * each string, and these are compared lexicographically, using
62 * ASCII ordering, except that letters precede non-letters. If
63 * both are the same, an initial sequence of digits is extracted
64 * from the remaining parts of the version strings, and these
65 * are compared numerically (an empty sequence being considered
66 * to have the value zero). This process is repeated until we
67 * have a winner or until both strings are exhausted.
76 static int vint(const char **vv
, const char *vl
)
84 if (!isdigit((unsigned char)ch
))
87 n
= n
* 10 + (ch
- '0');
93 static const char *vchr(const char **vv
, const char *vl
)
101 if (isdigit((unsigned char)ch
))
109 #define CMP(x, y) ((x) < (y) ? -1 : +1)
111 static int vcmp(const char *va
, const char *val
,
112 const char *vb
, const char *vbl
)
119 /* --- See if we're done --- */
121 if (va
== val
&& vb
== vbl
)
124 /* --- Compare nondigit portions --- */
126 pa
= vchr(&va
, val
); pb
= vchr(&vb
, vbl
);
128 if (pa
== va
) ia
= 1;
129 else if (isalpha((unsigned char)*pa
)) ia
= 2;
130 else if (*pa
== '~') ia
= 0;
133 if (pb
== vb
) ib
= 1;
134 else if (isalpha((unsigned char)*pb
)) ib
= 2;
135 else if (*pb
== '~') ib
= 0;
138 if (ia
!= ib
) return (CMP(ia
, ib
));
139 else if (pa
== va
&& pb
== vb
) break;
140 else if (*pa
!= *pb
) return (CMP(*pa
, *pb
));
144 /* --- Compare digit portions --- */
146 ia
= vint(&va
, val
); ib
= vint(&vb
, vbl
);
147 if (ia
!= ib
) return (CMP(ia
, ib
));
151 static void vsplit(const char *v
, struct vinfo
*vi
)
156 if ((p
= strchr(v
, ':')) == 0)
165 if ((p
= strrchr(v
, '-')) == 0)
177 int versioncmp(const char *va
, const char *vb
)
179 struct vinfo via
, vib
;
182 vsplit(va
, &via
); vsplit(vb
, &vib
);
183 if ((rc
= vcmp(via
.e
, via
.el
, vib
.e
, vib
.el
)) != 0 ||
184 (rc
= vcmp(via
.m
, via
.ml
, vib
.m
, vib
.ml
)) != 0 ||
185 (rc
= vcmp(via
.s
, via
.sl
, vib
.s
, vib
.sl
)) != 0)
190 /*----- That's all, folks -------------------------------------------------*/