rpmver.cc 3.3 KB

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