Skip to content

Instantly share code, notes, and snippets.

@ayourtch
Created June 2, 2020 17:27
Show Gist options
  • Save ayourtch/91c891d8cb2abd43822e73fbf7f06def to your computer and use it in GitHub Desktop.
Save ayourtch/91c891d8cb2abd43822e73fbf7f06def to your computer and use it in GitHub Desktop.
A quick hack to show the sorting of the package versions by apt and rpm
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int order(char c)
{
if (isdigit(c))
return 0;
else if (isalpha(c))
return c;
else if (c == '~')
return -1;
else if (c)
return c + 256;
else
return 0;
}
int CmpFragment(const char *A,const char *AEnd,
const char *B,const char *BEnd)
{
/* Iterate over the whole string
What this does is to split the whole string into groups of
numeric and non numeric portions. For instance:
a67bhgs89
Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
2.7.2-linux-1
Has '2', '.', '7', '.' ,'-linux-','1' */
const char *lhs = A;
const char *rhs = B;
while (lhs != AEnd && rhs != BEnd)
{
int first_diff = 0;
while (lhs != AEnd && rhs != BEnd &&
(!isdigit(*lhs) || !isdigit(*rhs)))
{
int vc = order(*lhs);
int rc = order(*rhs);
if (vc != rc)
return vc - rc;
++lhs; ++rhs;
}
while (*lhs == '0')
++lhs;
while (*rhs == '0')
++rhs;
while (isdigit(*lhs) && isdigit(*rhs))
{
if (!first_diff)
first_diff = *lhs - *rhs;
++lhs;
++rhs;
}
if (isdigit(*lhs))
return 1;
if (isdigit(*rhs))
return -1;
if (first_diff)
return first_diff;
}
// The strings must be equal
if (lhs == AEnd && rhs == BEnd)
return 0;
// lhs is shorter
if (lhs == AEnd)
{
if (*rhs == '~') return 1;
return -1;
}
// rhs is shorter
if (rhs == BEnd)
{
if (*lhs == '~') return -1;
return 1;
}
// Shouldn't happen
return 1;
}
/* This fragments the version into E:V-R triples and compares each
portion separately. */
int DoCmpVersion(char *A,char *AEnd,
char *B, char *BEnd)
{
// Strip off the epoch and compare it
char *lhs = ( char*) memchr(A, ':', AEnd - A);
char *rhs = ( char*) memchr(B, ':', BEnd - B);
if (lhs == NULL)
lhs = A;
if (rhs == NULL)
rhs = B;
// Special case: a zero epoch is the same as no epoch,
// so remove it.
if (lhs != A)
{
for (; *A == '0'; ++A);
if (A == lhs)
{
++A;
++lhs;
}
}
if (rhs != B)
{
for (; *B == '0'; ++B);
if (B == rhs)
{
++B;
++rhs;
}
}
// Compare the epoch
int Res = CmpFragment(A,lhs,B,rhs);
if (Res != 0)
return Res;
// Skip the :
if (lhs != A)
lhs++;
if (rhs != B)
rhs++;
// Find the last -
char *dlhs = ( char*) memrchr(lhs, '-', AEnd - lhs);
char *drhs = ( char*) memrchr(rhs, '-', BEnd - rhs);
if (dlhs == NULL)
dlhs = AEnd;
if (drhs == NULL)
drhs = BEnd;
// Compare the main version
Res = CmpFragment(lhs,dlhs,rhs,drhs);
if (Res != 0)
return Res;
// Skip the -
if (dlhs != lhs)
dlhs++;
if (drhs != rhs)
drhs++;
// no debian revision need to be treated like -0
if (*(dlhs-1) == '-' && *(drhs-1) == '-')
return CmpFragment(dlhs,AEnd,drhs,BEnd);
else if (*(dlhs-1) == '-')
{
char* null = "0";
return CmpFragment(dlhs,AEnd,null, null+1);
}
else if (*(drhs-1) == '-')
{
char* null = "0";
return CmpFragment(null, null+1, drhs, BEnd);
}
else
return 0;
}
char cmp_a[1024];
char cmp_b[1024];
int deb_compare(char **a, char **b) {
int len_a = strlen(*a);
memcpy(cmp_a, *a, len_a+1);
char *end_a = cmp_a + len_a;
int len_b = strlen(*b);
memcpy(cmp_b, *b, len_b+1);
char *end_b = cmp_b + len_b;
int res = -DoCmpVersion(cmp_a, end_a, cmp_b, end_b);
printf("A: '%s' B: '%s' res:%d\n", cmp_a, cmp_b, res);
return res;
}
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* This was lifted pretty much verbatim from
* https://fastapi.metacpan.org/source/HAG/RPM-VersionSort-1.00/VersionSort.xs */
/* This is lifted pretty much verbatim from the RPM 4.0.2 sources.
See lib/misc.c
*/
/* Note that the semantics as documented by redhat are incorrect: some
return value semantics are inherited from strcmp, which only promises
to use integers greater, less, or equal to zero, which is not the same
as (-1,0,1). So we fix this up in the xsub.
*/
/* 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 && !isalnum(*one)) one++;
while (*two && !isalnum(*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 (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';
/* take care of the case where the two version segments are */
/* different types: one numeric and one alpha */
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;
}
int rpm_compare(const char *a, const char *b)
{
int vercmp = rpmvercmp(a, b);
int result = 0;
if(vercmp > 0) result = 1;
if(vercmp < 0) result = -1;
return result;
}
int main(int argc, char **argv)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char **versions = malloc(1024);
int n_versions = 0;
if (argc < 2) {
printf("Need a file name with the list of versions\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
exit(EXIT_FAILURE);
int i;
while ((read = getline(&versions[n_versions], &len, fp)) != -1) {
line = versions[n_versions];
if (strlen(line) > 0) {
line[strlen(line)-1] = 0;
}
n_versions++;
}
printf("Original list:\n");
for (i=0; i<n_versions; i++) {
printf("%s\n", versions[i]);
}
printf("\nSorting:\n");
qsort(versions, n_versions, sizeof(char *), deb_compare);
printf("\nDEB sorting:\n");
for (i=0; i<n_versions; i++) {
printf("%s\n", versions[i]);
}
qsort(versions, n_versions, sizeof(char *), rpm_compare);
printf("\nRPM sorting:\n");
for (i=0; i<n_versions; i++) {
printf("%s\n", versions[i]);
}
fclose(fp);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment