2 * \file lib/rpmvercmp.c
7 #include <rpm/rpmlib.h> /* rpmvercmp proto */
8 #include <rpm/rpmstring.h>
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)
18 /* easy comparison to see if versions are identical */
19 if (rstreq(a, b)) return 0;
22 char abuf[strlen(a)+1], bbuf[strlen(b)+1];
23 char *str1 = abuf, *str2 = bbuf;
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++;
39 /* handle the tilde separator, it sorts before everything else */
40 if (*one == '~' || *two == '~') {
41 if (*one != '~') return 1;
42 if (*two != '~') return -1;
48 /* If we ran to the end of either, we are finished with the loop */
49 if (!(*one && *two)) break;
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++;
62 while (*str1 && risalpha(*str1)) str1++;
63 while (*str2 && risalpha(*str2)) str2++;
67 /* save character at the end of the alpha or numeric segment */
68 /* so that they can be restored after the comparison */
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 */
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);
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. */
90 /* throw away any leading zeros - it's a number, right? */
91 while (*one == '0') one++;
92 while (*two == '0') two++;
94 /* whichever number has more digits wins */
97 if (onelen > twolen) return 1;
98 if (twolen > onelen) return -1;
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 */
105 rc = strcmp(one, two);
106 if (rc) return (rc < 1 ? -1 : 1);
108 /* restore character that was replaced by null above */
115 /* this catches the case where all numeric and alpha segments have */
116 /* compared identically but the segment sepparating characters were */
118 if ((!*one) && (!*two)) return 0;
120 /* whichever version still has characters left over wins */
121 if (!*one) return -1; else return 1;
124 int rpmVersionCompare(Header first, Header second)
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);
131 if (epochOne < epochTwo)
133 else if (epochOne > epochTwo)
136 rc = rpmvercmp(headerGetString(first, RPMTAG_VERSION),
137 headerGetString(second, RPMTAG_VERSION));
141 return rpmvercmp(headerGetString(first, RPMTAG_RELEASE),
142 headerGetString(second, RPMTAG_RELEASE));