vercmp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * vercmp.c - comparison of version numbers
  4. *
  5. * Copyright © 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 <compat.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <dpkg.h>
  26. #include <dpkg-db.h>
  27. #include "parsedump.h"
  28. int epochsdiffer(const struct versionrevision *a,
  29. const struct versionrevision *b) {
  30. return a->epoch != b->epoch;
  31. }
  32. /* assume ascii; warning: evaluates x multiple times! */
  33. #define order(x) ((x) == '~' ? -1 \
  34. : cisdigit((x)) ? 0 \
  35. : !(x) ? 0 \
  36. : cisalpha((x)) ? (x) \
  37. : (x) + 256)
  38. static int verrevcmp(const char *val, const char *ref) {
  39. if (!val) val= "";
  40. if (!ref) ref= "";
  41. while (*val || *ref) {
  42. int first_diff= 0;
  43. while ( (*val && !cisdigit(*val)) || (*ref && !cisdigit(*ref)) ) {
  44. int vc= order(*val), rc= order(*ref);
  45. if (vc != rc) return vc - rc;
  46. val++; ref++;
  47. }
  48. while ( *val == '0' ) val++;
  49. while ( *ref == '0' ) ref++;
  50. while (cisdigit(*val) && cisdigit(*ref)) {
  51. if (!first_diff) first_diff= *val - *ref;
  52. val++; ref++;
  53. }
  54. if (cisdigit(*val)) return 1;
  55. if (cisdigit(*ref)) return -1;
  56. if (first_diff) return first_diff;
  57. }
  58. return 0;
  59. }
  60. int versioncompare(const struct versionrevision *version,
  61. const struct versionrevision *refversion) {
  62. int r;
  63. if (version->epoch > refversion->epoch) return 1;
  64. if (version->epoch < refversion->epoch) return -1;
  65. r= verrevcmp(version->version,refversion->version); if (r) return r;
  66. return verrevcmp(version->revision,refversion->revision);
  67. }
  68. int versionsatisfied3(const struct versionrevision *it,
  69. const struct versionrevision *ref,
  70. enum depverrel verrel) {
  71. int r;
  72. if (verrel == dvr_none) return 1;
  73. r= versioncompare(it,ref);
  74. switch (verrel) {
  75. case dvr_earlierequal: return r <= 0;
  76. case dvr_laterequal: return r >= 0;
  77. case dvr_earlierstrict: return r < 0;
  78. case dvr_laterstrict: return r > 0;
  79. case dvr_exact: return r == 0;
  80. default: internerr("unknown verrel");
  81. }
  82. return 0;
  83. }
  84. int versionsatisfied(struct pkginfoperfile *it, struct deppossi *against) {
  85. return versionsatisfied3(&it->version,&against->version,against->verrel);
  86. }