Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

lib/rpmvercmp.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmlib.h>
00008 
00009 #include "debug.h"
00010 
00011 /* compare alpha and numeric segments of two versions */
00012 /* return 1: a is newer than b */
00013 /*        0: a and b are the same version */
00014 /*       -1: b is newer than a */
00015 int rpmvercmp(const char * a, const char * b)
00016 {
00017     char oldch1, oldch2;
00018     char * str1, * str2;
00019     char * one, * two;
00020     int rc;
00021     int isnum;
00022 
00023     /* easy comparison to see if versions are identical */
00024     if (!strcmp(a, b)) return 0;
00025 
00026     str1 = alloca(strlen(a) + 1);
00027     str2 = alloca(strlen(b) + 1);
00028 
00029     strcpy(str1, a);
00030     strcpy(str2, b);
00031 
00032     one = str1;
00033     two = str2;
00034 
00035     /* loop through each version segment of str1 and str2 and compare them */
00036     /*@-branchstate@*/
00037 /*@-boundsread@*/
00038     while (*one && *two) {
00039         while (*one && !xisalnum(*one)) one++;
00040         while (*two && !xisalnum(*two)) two++;
00041 
00042         str1 = one;
00043         str2 = two;
00044 
00045         /* grab first completely alpha or completely numeric segment */
00046         /* leave one and two pointing to the start of the alpha or numeric */
00047         /* segment and walk str1 and str2 to end of segment */
00048         if (xisdigit(*str1)) {
00049             while (*str1 && xisdigit(*str1)) str1++;
00050             while (*str2 && xisdigit(*str2)) str2++;
00051             isnum = 1;
00052         } else {
00053             while (*str1 && xisalpha(*str1)) str1++;
00054             while (*str2 && xisalpha(*str2)) str2++;
00055             isnum = 0;
00056         }
00057 
00058         /* save character at the end of the alpha or numeric segment */
00059         /* so that they can be restored after the comparison */
00060 /*@-boundswrite@*/
00061         oldch1 = *str1;
00062         *str1 = '\0';
00063         oldch2 = *str2;
00064         *str2 = '\0';
00065 /*@=boundswrite@*/
00066 
00067         /* take care of the case where the two version segments are */
00068         /* different types: one numeric, the other alpha (i.e. empty) */
00069         if (one == str1) return -1;     /* arbitrary */
00070         /* XXX See patch #60884 (and details) from bugzilla #50977. */
00071         if (two == str2) return (isnum ? 1 : -1);
00072 
00073         if (isnum) {
00074             /* this used to be done by converting the digit segments */
00075             /* to ints using atoi() - it's changed because long  */
00076             /* digit segments can overflow an int - this should fix that. */
00077 
00078             /* throw away any leading zeros - it's a number, right? */
00079             while (*one == '0') one++;
00080             while (*two == '0') two++;
00081 
00082             /* whichever number has more digits wins */
00083             if (strlen(one) > strlen(two)) return 1;
00084             if (strlen(two) > strlen(one)) return -1;
00085         }
00086 
00087         /* strcmp will return which one is greater - even if the two */
00088         /* segments are alpha or if they are numeric.  don't return  */
00089         /* if they are equal because there might be more segments to */
00090         /* compare */
00091         rc = strcmp(one, two);
00092         if (rc) return rc;
00093 
00094         /* restore character that was replaced by null above */
00095 /*@-boundswrite@*/
00096         *str1 = oldch1;
00097         one = str1;
00098         *str2 = oldch2;
00099         two = str2;
00100 /*@=boundswrite@*/
00101     }
00102     /*@=branchstate@*/
00103 /*@=boundsread@*/
00104 
00105     /* this catches the case where all numeric and alpha segments have */
00106     /* compared identically but the segment sepparating characters were */
00107     /* different */
00108 /*@-boundsread@*/
00109     if ((!*one) && (!*two)) return 0;
00110 
00111     /* whichever version still has characters left over wins */
00112     if (!*one) return -1; else return 1;
00113 /*@=boundsread@*/
00114 }

Generated on Sun Oct 26 13:02:01 2003 for rpm by doxygen1.2.18