[systemd-devel] [PATCH 7/8] add rpmvercmp()
Shawn Landden
shawn at churchofgit.com
Fri Feb 20 14:31:04 PST 2015
---
Makefile.am | 2 +
src/shared/rpmvercmp.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++
src/shared/rpmvercmp.h | 14 ++++++
3 files changed, 138 insertions(+)
create mode 100644 src/shared/rpmvercmp.c
create mode 100644 src/shared/rpmvercmp.h
diff --git a/Makefile.am b/Makefile.am
index e0813a6..1310a20 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -900,6 +900,8 @@ libsystemd_shared_la_SOURCES = \
src/shared/nss-util.h \
src/shared/verbs.c \
src/shared/verbs.h \
+ src/shared/rpmvercmp.c \
+ src/shared/rpmvercmp.h \
src/shared/sigbus.c \
src/shared/sigbus.h \
src/shared/rbtree.c \
diff --git a/src/shared/rpmvercmp.c b/src/shared/rpmvercmp.c
new file mode 100644
index 0000000..c69c2e3
--- /dev/null
+++ b/src/shared/rpmvercmp.c
@@ -0,0 +1,122 @@
+/* From rpm (Library GPLv2+, which is compatible with LGPLv2.1+)
+ * git://rpm.org/rpm.git
+ */
+
+#include <stddef.h>
+#include <ctype.h>
+#include <string.h>
+
+#include "util.h"
+#include "rpmvercmp.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)
+{
+ /* easy comparison to see if versions are identical */
+ if (streq_ptr(a, b)) return 0;
+
+ char oldch1, oldch2;
+ char abuf[strlen(a)+1], bbuf[strlen(b)+1];
+ char *str1 = abuf, *str2 = bbuf;
+ char * one, * two;
+ int rc;
+ int isnum;
+
+ 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 && !isalnum(*one) && *one != '~') one++;
+ while (*two && !isalnum(*two) && *two != '~') two++;
+
+ /* handle the tilde separator, it sorts before everything else */
+ if (*one == '~' || *two == '~') {
+ if (*one != '~') return 1;
+ if (*two != '~') return -1;
+ one++;
+ two++;
+ continue;
+ }
+
+ /* If we ran to the end of either, we are finished with the loop */
+ if (!(*one && *two)) break;
+
+ 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 (isdigit(*str1)) {
+ while (*str1 && isdigit(*str1)) str1++;
+ while (*str2 && isdigit(*str2)) str2++;
+ isnum = 1;
+ } else {
+ while (*str1 && isalpha(*str1)) str1++;
+ while (*str2 && isalpha(*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';
+
+ /* this cannot happen, as we previously tested to make sure that */
+ /* the first string has a non-null segment */
+ if (one == str1) return -1; /* arbitrary */
+
+ /* take care of the case where the two version segments are */
+ /* different types: one numeric, the other alpha (i.e. empty) */
+ /* numeric segments are always newer than alpha segments */
+ /* XXX See patch #60884 (and details) from bugzilla #50977. */
+ if (two == str2) return (isnum ? 1 : -1);
+
+ if (isnum) {
+ size_t onelen, twolen;
+ /* 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 */
+ onelen = strlen(one);
+ twolen = strlen(two);
+ if (onelen > twolen) return 1;
+ if (twolen > onelen) 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 < 1 ? -1 : 1);
+
+ /* 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;
+}
diff --git a/src/shared/rpmvercmp.h b/src/shared/rpmvercmp.h
new file mode 100644
index 0000000..4ba6f36
--- /dev/null
+++ b/src/shared/rpmvercmp.h
@@ -0,0 +1,14 @@
+/* From rpm (Library GPLv2+, which is compatible with LGPLv2.1+)
+ * git://rpm.org/rpm.git
+ */
+
+#pragma once
+
+/** \ingroup rpmtrans
+ * Segmented string compare for version or release strings.
+ *
+ * @param a 1st string
+ * @param b 2nd string
+ * @return +1 if a is "newer", 0 if equal, -1 if b is "newer"
+ */
+int rpmvercmp(const char * a, const char * b);
--
2.2.1.209.g41e5f3a
More information about the systemd-devel
mailing list