summaryrefslogtreecommitdiff
path: root/lib/rpmvercmp.c
diff options
context:
space:
mode:
authorjbj <devnull@localhost>2001-06-17 22:18:03 +0000
committerjbj <devnull@localhost>2001-06-17 22:18:03 +0000
commit29ea1567e7eafc457aed5529b240161cf8d4657f (patch)
treea791a4344419b7b942fd4d755d25a3a6b4c4e5a3 /lib/rpmvercmp.c
parentc32276cdf5c28d7d701d7211e55f28ebc6f097d7 (diff)
downloadlibrpm-tizen-29ea1567e7eafc457aed5529b240161cf8d4657f.tar.gz
librpm-tizen-29ea1567e7eafc457aed5529b240161cf8d4657f.tar.bz2
librpm-tizen-29ea1567e7eafc457aed5529b240161cf8d4657f.zip
Sync with rpm-4_0 branch.
CVS patchset: 4876 CVS date: 2001/06/17 22:18:03
Diffstat (limited to 'lib/rpmvercmp.c')
-rw-r--r--lib/rpmvercmp.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/rpmvercmp.c b/lib/rpmvercmp.c
new file mode 100644
index 000000000..ec09447dd
--- /dev/null
+++ b/lib/rpmvercmp.c
@@ -0,0 +1,103 @@
+/** \ingroup rpmtrans
+ * \file lib/rpmvercmp.c
+ */
+
+#include "system.h"
+
+#include <rpmio.h>
+
+#include "debug.h"
+
+/* compare alpha and numeric segments of two versions */
+/* return 1: a is newer than b */
+/* 0: a and b are the same version */
+/* -1: b is newer than a */
+int rpmvercmp(const char * a, const char * b)
+{
+ char oldch1, oldch2;
+ char * str1, * str2;
+ char * one, * two;
+ int rc;
+ int isnum;
+
+ /* easy comparison to see if versions are identical */
+ if (!strcmp(a, b)) return 0;
+
+ str1 = alloca(strlen(a) + 1);
+ str2 = alloca(strlen(b) + 1);
+
+ strcpy(str1, a);
+ strcpy(str2, b);
+
+ one = str1;
+ two = str2;
+
+ /* loop through each version segment of str1 and str2 and compare them */
+ while (*one && *two) {
+ while (*one && !xisalnum(*one)) one++;
+ while (*two && !xisalnum(*two)) two++;
+
+ str1 = one;
+ str2 = two;
+
+ /* grab first completely alpha or completely numeric segment */
+ /* leave one and two pointing to the start of the alpha or numeric */
+ /* segment and walk str1 and str2 to end of segment */
+ if (xisdigit(*str1)) {
+ while (*str1 && xisdigit(*str1)) str1++;
+ while (*str2 && xisdigit(*str2)) str2++;
+ isnum = 1;
+ } else {
+ while (*str1 && xisalpha(*str1)) str1++;
+ while (*str2 && xisalpha(*str2)) str2++;
+ isnum = 0;
+ }
+
+ /* save character at the end of the alpha or numeric segment */
+ /* so that they can be restored after the comparison */
+ oldch1 = *str1;
+ *str1 = '\0';
+ oldch2 = *str2;
+ *str2 = '\0';
+
+ /* take care of the case where the two version segments are */
+ /* different types: one numeric, the other alpha (i.e. empty) */
+ if (one == str1) return -1; /* arbitrary */
+ if (two == str2) return 1;
+
+ if (isnum) {
+ /* this used to be done by converting the digit segments */
+ /* to ints using atoi() - it's changed because long */
+ /* digit segments can overflow an int - this should fix that. */
+
+ /* throw away any leading zeros - it's a number, right? */
+ while (*one == '0') one++;
+ while (*two == '0') two++;
+
+ /* whichever number has more digits wins */
+ if (strlen(one) > strlen(two)) return 1;
+ if (strlen(two) > strlen(one)) return -1;
+ }
+
+ /* strcmp will return which one is greater - even if the two */
+ /* segments are alpha or if they are numeric. don't return */
+ /* if they are equal because there might be more segments to */
+ /* compare */
+ rc = strcmp(one, two);
+ if (rc) return rc;
+
+ /* restore character that was replaced by null above */
+ *str1 = oldch1;
+ one = str1;
+ *str2 = oldch2;
+ two = str2;
+ }
+
+ /* this catches the case where all numeric and alpha segments have */
+ /* compared identically but the segment sepparating characters were */
+ /* different */
+ if ((!*one) && (!*two)) return 0;
+
+ /* whichever version still has characters left over wins */
+ if (!*one) return -1; else return 1;
+}