vercmp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * vercmp.c - comparison of version numbers
  4. *
  5. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <dpkg.h>
  25. #include <dpkg-db.h>
  26. #include "parsedump.h"
  27. int epochsdiffer(const struct versionrevision *a,
  28. const struct versionrevision *b) {
  29. return a->epoch != b->epoch;
  30. }
  31. /* assume ascii; warning: evaluates x multiple times! */
  32. #define order(x) ((x) == '~' ? -1 \
  33. : cisdigit((x)) ? 0 \
  34. : !(x) ? 0 \
  35. : cisalpha((x)) ? (x) \
  36. : (x) + 256)
  37. static int verrevcmp(const char *val, const char *ref) {
  38. if (!val) val= "";
  39. if (!ref) ref= "";
  40. while (*val || *ref) {
  41. int first_diff= 0;
  42. while ( (*val && !cisdigit(*val)) || (*ref && !cisdigit(*ref)) ) {
  43. int vc= order(*val), rc= order(*ref);
  44. if (vc != rc) return vc - rc;
  45. val++; ref++;
  46. }
  47. while ( *val == '0' ) val++;
  48. while ( *ref == '0' ) ref++;
  49. while (cisdigit(*val) && cisdigit(*ref)) {
  50. if (!first_diff) first_diff= *val - *ref;
  51. val++; ref++;
  52. }
  53. if (cisdigit(*val)) return 1;
  54. if (cisdigit(*ref)) return -1;
  55. if (first_diff) return first_diff;
  56. }
  57. return 0;
  58. }
  59. #if 0
  60. static int verrevcmp(const char *ival, const char *iref) {
  61. static char empty[] = "";
  62. int vc, rc;
  63. long vl, rl;
  64. char *vp, *rp;
  65. char *val, *ref;
  66. memcpy(&val,&ival,sizeof(char*));
  67. memcpy(&ref,&iref,sizeof(char*));
  68. if (!val) val= empty;
  69. if (!ref) ref= empty;
  70. for (;;) {
  71. vp= val; while (*vp && !isdigit(*vp)) vp++;
  72. rp= ref; while (*rp && !isdigit(*rp)) rp++;
  73. for (;;) {
  74. vc= val == vp ? 0 : *val++;
  75. rc= ref == rp ? 0 : *ref++;
  76. if (!rc && !vc) break;
  77. if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
  78. if (rc && !isalpha(rc)) rc += 256;
  79. if (vc != rc) return vc - rc;
  80. }
  81. val= vp;
  82. ref= rp;
  83. vl=0; if (isdigit(*vp)) vl= strtol(val,&val,10);
  84. rl=0; if (isdigit(*rp)) rl= strtol(ref,&ref,10);
  85. if (vl != rl) return vl - rl;
  86. if (!*val && !*ref) return 0;
  87. if (!*val) return -1;
  88. if (!*ref) return +1;
  89. }
  90. }
  91. #endif
  92. int versioncompare(const struct versionrevision *version,
  93. const struct versionrevision *refversion) {
  94. int r;
  95. if (version->epoch > refversion->epoch) return 1;
  96. if (version->epoch < refversion->epoch) return -1;
  97. r= verrevcmp(version->version,refversion->version); if (r) return r;
  98. return verrevcmp(version->revision,refversion->revision);
  99. }
  100. int versionsatisfied3(const struct versionrevision *it,
  101. const struct versionrevision *ref,
  102. enum depverrel verrel) {
  103. int r;
  104. if (verrel == dvr_none) return 1;
  105. r= versioncompare(it,ref);
  106. switch (verrel) {
  107. case dvr_earlierequal: return r <= 0;
  108. case dvr_laterequal: return r >= 0;
  109. case dvr_earlierstrict: return r < 0;
  110. case dvr_laterstrict: return r > 0;
  111. case dvr_exact: return r == 0;
  112. default: internerr("unknown verrel");
  113. }
  114. return 0;
  115. }
  116. int versionsatisfied(struct pkginfoperfile *it, struct deppossi *against) {
  117. return versionsatisfied3(&it->version,&against->version,against->verrel);
  118. }