Blame lib/rpmvercmp.c

2ff057
/** \ingroup rpmts
2ff057
 * \file lib/rpmvercmp.c
2ff057
 */
2ff057
2ff057
#include "system.h"
2ff057
2ff057
#include <rpm/rpmlib.h>		/* rpmvercmp proto */
2ff057
#include <rpm/rpmstring.h>
2ff057
2ff057
#include "debug.h"
2ff057
2ff057
/* compare alpha and numeric segments of two versions */
2ff057
/* return 1: a is newer than b */
2ff057
/*        0: a and b are the same version */
2ff057
/*       -1: b is newer than a */
2ff057
int rpmvercmp(const char * a, const char * b)
2ff057
{
2ff057
    /* easy comparison to see if versions are identical */
2ff057
    if (rstreq(a, b)) return 0;
2ff057
2ff057
    char oldch1, oldch2;
2ff057
    char abuf[strlen(a)+1], bbuf[strlen(b)+1];
2ff057
    char *str1 = abuf, *str2 = bbuf;
2ff057
    char * one, * two;
2ff057
    int rc;
2ff057
    int isnum;
2ff057
2ff057
    strcpy(str1, a);
2ff057
    strcpy(str2, b);
2ff057
2ff057
    one = str1;
2ff057
    two = str2;
2ff057
2ff057
    /* loop through each version segment of str1 and str2 and compare them */
2ff057
    while (*one || *two) {
Packit Service bb4f4d
	while (*one && !risalnum(*one) && *one != '~') one++;
Packit Service bb4f4d
	while (*two && !risalnum(*two) && *two != '~') two++;
2ff057
2ff057
	/* handle the tilde separator, it sorts before everything else */
2ff057
	if (*one == '~' || *two == '~') {
2ff057
	    if (*one != '~') return 1;
2ff057
	    if (*two != '~') return -1;
2ff057
	    one++;
2ff057
	    two++;
2ff057
	    continue;
2ff057
	}
2ff057
2ff057
	/* If we ran to the end of either, we are finished with the loop */
2ff057
	if (!(*one && *two)) break;
2ff057
2ff057
	str1 = one;
2ff057
	str2 = two;
2ff057
2ff057
	/* grab first completely alpha or completely numeric segment */
2ff057
	/* leave one and two pointing to the start of the alpha or numeric */
2ff057
	/* segment and walk str1 and str2 to end of segment */
2ff057
	if (risdigit(*str1)) {
2ff057
	    while (*str1 && risdigit(*str1)) str1++;
2ff057
	    while (*str2 && risdigit(*str2)) str2++;
2ff057
	    isnum = 1;
2ff057
	} else {
2ff057
	    while (*str1 && risalpha(*str1)) str1++;
2ff057
	    while (*str2 && risalpha(*str2)) str2++;
2ff057
	    isnum = 0;
2ff057
	}
2ff057
2ff057
	/* save character at the end of the alpha or numeric segment */
2ff057
	/* so that they can be restored after the comparison */
2ff057
	oldch1 = *str1;
2ff057
	*str1 = '\0';
2ff057
	oldch2 = *str2;
2ff057
	*str2 = '\0';
2ff057
2ff057
	/* this cannot happen, as we previously tested to make sure that */
2ff057
	/* the first string has a non-null segment */
2ff057
	if (one == str1) return -1;	/* arbitrary */
2ff057
2ff057
	/* take care of the case where the two version segments are */
2ff057
	/* different types: one numeric, the other alpha (i.e. empty) */
2ff057
	/* numeric segments are always newer than alpha segments */
2ff057
	/* XXX See patch #60884 (and details) from bugzilla #50977. */
2ff057
	if (two == str2) return (isnum ? 1 : -1);
2ff057
2ff057
	if (isnum) {
2ff057
	    size_t onelen, twolen;
2ff057
	    /* this used to be done by converting the digit segments */
2ff057
	    /* to ints using atoi() - it's changed because long  */
2ff057
	    /* digit segments can overflow an int - this should fix that. */
2ff057
2ff057
	    /* throw away any leading zeros - it's a number, right? */
2ff057
	    while (*one == '0') one++;
2ff057
	    while (*two == '0') two++;
2ff057
2ff057
	    /* whichever number has more digits wins */
2ff057
	    onelen = strlen(one);
2ff057
	    twolen = strlen(two);
2ff057
	    if (onelen > twolen) return 1;
2ff057
	    if (twolen > onelen) return -1;
2ff057
	}
2ff057
2ff057
	/* strcmp will return which one is greater - even if the two */
2ff057
	/* segments are alpha or if they are numeric.  don't return  */
2ff057
	/* if they are equal because there might be more segments to */
2ff057
	/* compare */
2ff057
	rc = strcmp(one, two);
2ff057
	if (rc) return (rc < 1 ? -1 : 1);
2ff057
2ff057
	/* restore character that was replaced by null above */
2ff057
	*str1 = oldch1;
2ff057
	one = str1;
2ff057
	*str2 = oldch2;
2ff057
	two = str2;
2ff057
    }
2ff057
2ff057
    /* this catches the case where all numeric and alpha segments have */
2ff057
    /* compared identically but the segment sepparating characters were */
2ff057
    /* different */
2ff057
    if ((!*one) && (!*two)) return 0;
2ff057
2ff057
    /* whichever version still has characters left over wins */
2ff057
    if (!*one) return -1; else return 1;
2ff057
}
2ff057
2ff057
int rpmVersionCompare(Header first, Header second)
2ff057
{
2ff057
    /* Missing epoch becomes zero here, which is what we want */
2ff057
    uint32_t epochOne = headerGetNumber(first, RPMTAG_EPOCH);
2ff057
    uint32_t epochTwo = headerGetNumber(second, RPMTAG_EPOCH);
2ff057
    int rc;
2ff057
2ff057
    if (epochOne < epochTwo)
2ff057
	return -1;
2ff057
    else if (epochOne > epochTwo)
2ff057
	return 1;
2ff057
2ff057
    rc = rpmvercmp(headerGetString(first, RPMTAG_VERSION),
2ff057
		   headerGetString(second, RPMTAG_VERSION));
2ff057
    if (rc)
2ff057
	return rc;
2ff057
2ff057
    return rpmvercmp(headerGetString(first, RPMTAG_RELEASE),
2ff057
		     headerGetString(second, RPMTAG_RELEASE));
2ff057
}