Add macro %isu_package to generate ISU Package
[platform/upstream/rpm.git] / lib / rpmvercmp.c
1 /** \ingroup rpmts
2  * \file lib/rpmvercmp.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmlib.h>         /* rpmvercmp proto */
8 #include <rpm/rpmstring.h>
9
10 #include "debug.h"
11
12 /* compare alpha and numeric segments of two versions */
13 /* return 1: a is newer than b */
14 /*        0: a and b are the same version */
15 /*       -1: b is newer than a */
16 int rpmvercmp(const char * a, const char * b)
17 {
18     /* easy comparison to see if versions are identical */
19     if (rstreq(a, b)) return 0;
20
21     char oldch1, oldch2;
22     char abuf[strlen(a)+1], bbuf[strlen(b)+1];
23     char *str1 = abuf, *str2 = bbuf;
24     char * one, * two;
25     int rc;
26     int isnum;
27
28     strcpy(str1, a);
29     strcpy(str2, b);
30
31     one = str1;
32     two = str2;
33
34     /* loop through each version segment of str1 and str2 and compare them */
35     while (*one || *two) {
36         while (*one && !risalnum(*one) && *one != '~') one++;
37         while (*two && !risalnum(*two) && *two != '~') two++;
38
39         /* handle the tilde separator, it sorts before everything else */
40         if (*one == '~' || *two == '~') {
41             if (*one != '~') return 1;
42             if (*two != '~') return -1;
43             one++;
44             two++;
45             continue;
46         }
47
48         /* If we ran to the end of either, we are finished with the loop */
49         if (!(*one && *two)) break;
50
51         str1 = one;
52         str2 = two;
53
54         /* grab first completely alpha or completely numeric segment */
55         /* leave one and two pointing to the start of the alpha or numeric */
56         /* segment and walk str1 and str2 to end of segment */
57         if (risdigit(*str1)) {
58             while (*str1 && risdigit(*str1)) str1++;
59             while (*str2 && risdigit(*str2)) str2++;
60             isnum = 1;
61         } else {
62             while (*str1 && risalpha(*str1)) str1++;
63             while (*str2 && risalpha(*str2)) str2++;
64             isnum = 0;
65         }
66
67         /* save character at the end of the alpha or numeric segment */
68         /* so that they can be restored after the comparison */
69         oldch1 = *str1;
70         *str1 = '\0';
71         oldch2 = *str2;
72         *str2 = '\0';
73
74         /* this cannot happen, as we previously tested to make sure that */
75         /* the first string has a non-null segment */
76         if (one == str1) return -1;     /* arbitrary */
77
78         /* take care of the case where the two version segments are */
79         /* different types: one numeric, the other alpha (i.e. empty) */
80         /* numeric segments are always newer than alpha segments */
81         /* XXX See patch #60884 (and details) from bugzilla #50977. */
82         if (two == str2) return (isnum ? 1 : -1);
83
84         if (isnum) {
85             size_t onelen, twolen;
86             /* this used to be done by converting the digit segments */
87             /* to ints using atoi() - it's changed because long  */
88             /* digit segments can overflow an int - this should fix that. */
89
90             /* throw away any leading zeros - it's a number, right? */
91             while (*one == '0') one++;
92             while (*two == '0') two++;
93
94             /* whichever number has more digits wins */
95             onelen = strlen(one);
96             twolen = strlen(two);
97             if (onelen > twolen) return 1;
98             if (twolen > onelen) return -1;
99         }
100
101         /* strcmp will return which one is greater - even if the two */
102         /* segments are alpha or if they are numeric.  don't return  */
103         /* if they are equal because there might be more segments to */
104         /* compare */
105         rc = strcmp(one, two);
106         if (rc) return (rc < 1 ? -1 : 1);
107
108         /* restore character that was replaced by null above */
109         *str1 = oldch1;
110         one = str1;
111         *str2 = oldch2;
112         two = str2;
113     }
114
115     /* this catches the case where all numeric and alpha segments have */
116     /* compared identically but the segment sepparating characters were */
117     /* different */
118     if ((!*one) && (!*two)) return 0;
119
120     /* whichever version still has characters left over wins */
121     if (!*one) return -1; else return 1;
122 }
123
124 int rpmVersionCompare(Header first, Header second)
125 {
126     /* Missing epoch becomes zero here, which is what we want */
127     uint32_t epochOne = headerGetNumber(first, RPMTAG_EPOCH);
128     uint32_t epochTwo = headerGetNumber(second, RPMTAG_EPOCH);
129     int rc;
130
131     if (epochOne < epochTwo)
132         return -1;
133     else if (epochOne > epochTwo)
134         return 1;
135
136     rc = rpmvercmp(headerGetString(first, RPMTAG_VERSION),
137                    headerGetString(second, RPMTAG_VERSION));
138     if (rc)
139         return rc;
140
141     return rpmvercmp(headerGetString(first, RPMTAG_RELEASE),
142                      headerGetString(second, RPMTAG_RELEASE));
143 }