1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* rpmvercmp.c
*
* Copyright (c) 2002 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
/* this function was taken from rpm 4.0.4 and rewritten */
int rpmvercmp(const char *a, const char *b)
{
char *str1, *str2;
char *one, *two;
char *rel1 = NULL, *rel2 = NULL;
char oldch1, oldch2;
int is1num, is2num;
int rc;
if(!strcmp(a,b)) {
return(0);
}
str1 = strdup(a);
str2 = strdup(b);
/* lose the release number */
for(one = str1; *one && *one != '-'; one++);
if(one) {
*one = '\0';
rel1 = ++one;
}
for(two = str2; *two && *two != '-'; two++);
if(two) {
*two = '\0';
rel2 = ++two;
}
one = str1;
two = str2;
while(*one || *two) {
while(*one && !isalnum(*one)) one++;
while(*two && !isalnum(*two)) two++;
str1 = one;
str2 = two;
/* find the next segment for each string */
if(isdigit(*str1)) {
is1num = 1;
while(*str1 && isdigit(*str1)) str1++;
} else {
is1num = 0;
while(*str1 && isalpha(*str1)) str1++;
}
if(isdigit(*str2)) {
is2num = 1;
while(*str2 && isdigit(*str2)) str2++;
} else {
is2num = 0;
while(*str2 && isalpha(*str2)) str2++;
}
oldch1 = *str1;
*str1 = '\0';
oldch2 = *str2;
*str2 = '\0';
/* see if we ran out of segments on one string */
if(one == str1 && two != str2) {
return(is2num ? -1 : 1);
}
if(one != str1 && two == str2) {
return(is1num ? 1 : -1);
}
/* see if we have a type mismatch (ie, one is alpha and one is digits) */
if(is1num && !is2num) return(1);
if(!is1num && is2num) return(-1);
if(is1num) while(*one == '0') one++;
if(is2num) while(*two == '0') two++;
rc = strverscmp(one, two);
if(rc) return(rc);
*str1 = oldch1;
*str2 = oldch2;
one = str1;
two = str2;
}
if((!*one) && (!*two)) {
/* compare release numbers */
if(rel1 && rel2) return(rpmvercmp(rel1, rel2));
return(0);
}
return(*one ? 1 : -1);
}
/* vim: set ts=2 sw=2 noet: */
|