You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
3.3 KiB

  1. #include <config.h>
  2. #include <apt-pkg/debversion.h>
  3. #include <ctype.h>
  4. #include <rpm/misc.h>
  5. #include <rpm/rpmio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #define xisdigit(x) isdigit(x)
  9. #define xisalpha(x) isalpha(x)
  10. #define xisalnum(x) (isdigit(x) || isalpha(x))
  11. using namespace std;
  12. int rpmvercmp(const char * a, const char * b)
  13. {
  14. char * str1, * str2;
  15. char * one, * two;
  16. int isnum;
  17. /* easy comparison to see if versions are identical */
  18. if (!strcmp(a, b)) return 0;
  19. str1 = (char *)alloca(strlen(a) + 1);
  20. str2 = (char *)alloca(strlen(b) + 1);
  21. strcpy(str1, a);
  22. strcpy(str2, b);
  23. one = str1;
  24. two = str2;
  25. /* loop through each version segment of str1 and str2 and compare them */
  26. while (*one && *two) {
  27. while (*one && !xisalnum(*one)) one++;
  28. while (*two && !xisalnum(*two)) two++;
  29. str1 = one;
  30. str2 = two;
  31. /* grab first completely alpha or completely numeric segment */
  32. /* leave one and two pointing to the start of the alpha or numeric */
  33. /* segment and walk str1 and str2 to end of segment */
  34. if (xisdigit(*str1)) {
  35. while (*str1 && xisdigit(*str1)) str1++;
  36. while (*str2 && xisdigit(*str2)) str2++;
  37. isnum = 1;
  38. } else {
  39. while (*str1 && xisalpha(*str1)) str1++;
  40. while (*str2 && xisalpha(*str2)) str2++;
  41. isnum = 0;
  42. }
  43. /* save character at the end of the alpha or numeric segment */
  44. /* so that they can be restored after the comparison */
  45. char oldch1 = *str1;
  46. *str1 = '\0';
  47. char oldch2 = *str2;
  48. *str2 = '\0';
  49. /* take care of the case where the two version segments are */
  50. /* different types: one numeric, the other alpha (i.e. empty) */
  51. if (one == str1) return -1; /* arbitrary */
  52. if (two == str2) return 1;
  53. if (isnum) {
  54. /* this used to be done by converting the digit segments */
  55. /* to ints using atoi() - it's changed because long */
  56. /* digit segments can overflow an int - this should fix that. */
  57. /* throw away any leading zeros - it's a number, right? */
  58. while (*one == '0') one++;
  59. while (*two == '0') two++;
  60. /* whichever number has more digits wins */
  61. if (strlen(one) > strlen(two)) return 1;
  62. if (strlen(two) > strlen(one)) return -1;
  63. }
  64. /* strcmp will return which one is greater - even if the two */
  65. /* segments are alpha or if they are numeric. don't return */
  66. /* if they are equal because there might be more segments to */
  67. /* compare */
  68. int rc = strcmp(one, two);
  69. if (rc) return rc;
  70. /* restore character that was replaced by null above */
  71. *str1 = oldch1;
  72. one = str1;
  73. *str2 = oldch2;
  74. two = str2;
  75. }
  76. /* this catches the case where all numeric and alpha segments have */
  77. /* compared identically but the segment sepparating characters were */
  78. /* different */
  79. if ((!*one) && (!*two)) return 0;
  80. /* whichever version still has characters left over wins */
  81. if (!*one) return -1; else return 1;
  82. }
  83. int main(int argc,const char *argv[])
  84. {
  85. printf("%i\n",strcmp(argv[1],argv[2]));
  86. printf("'%s' <> '%s': ",argv[1],argv[2]);
  87. printf("rpm: %i deb: %i\n",rpmvercmp(argv[1],argv[2]),
  88. debVS.CmpFragment(argv[1],argv[1]+strlen(argv[1]),
  89. argv[2],argv[2]+strlen(argv[2])));
  90. printf("'%s' <> '%s': ",argv[2],argv[1]);
  91. printf("rpm: %i deb: %i\n",rpmvercmp(argv[2],argv[1]),
  92. debVS.CmpFragment(argv[2],argv[2]+strlen(argv[2]),
  93. argv[1],argv[1]+strlen(argv[1])));
  94. return 0;
  95. }