vercmp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * vercmp.c - comparison of version numbers
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.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 <ctype.h>
  22. #include <string.h>
  23. #include <config.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. static int verrevcmp(const char *val, const char *ref) {
  32. int vc, rc;
  33. long vl, rl;
  34. const char *vp, *rp;
  35. if (!val) val= "";
  36. if (!ref) ref= "";
  37. for (;;) {
  38. vp= val; while (*vp && !isdigit(*vp)) vp++;
  39. rp= ref; while (*rp && !isdigit(*rp)) rp++;
  40. for (;;) {
  41. vc= val == vp ? 0 : *val++;
  42. rc= ref == rp ? 0 : *ref++;
  43. if (!rc && !vc) break;
  44. if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
  45. if (rc && !isalpha(rc)) rc += 256;
  46. if (vc != rc) return vc - rc;
  47. }
  48. val= vp;
  49. ref= rp;
  50. vl=0; if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
  51. rl=0; if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
  52. if (vl != rl) return vl - rl;
  53. if (!*val && !*ref) return 0;
  54. if (!*val) return -1;
  55. if (!*ref) return +1;
  56. }
  57. }
  58. int versioncompare(const struct versionrevision *version,
  59. const struct versionrevision *refversion) {
  60. int r;
  61. if (version->epoch > refversion->epoch) return 1;
  62. if (version->epoch < refversion->epoch) return -1;
  63. r= verrevcmp(version->version,refversion->version); if (r) return r;
  64. return verrevcmp(version->revision,refversion->revision);
  65. }
  66. int versionsatisfied3(const struct versionrevision *it,
  67. const struct versionrevision *ref,
  68. enum depverrel verrel) {
  69. int r;
  70. if (verrel == dvr_none) return 1;
  71. r= versioncompare(it,ref);
  72. switch (verrel) {
  73. case dvr_earlierequal: return r <= 0;
  74. case dvr_laterequal: return r >= 0;
  75. case dvr_earlierstrict: return r < 0;
  76. case dvr_laterstrict: return r > 0;
  77. case dvr_exact: return r == 0;
  78. default: internerr("unknown verrel");
  79. }
  80. }
  81. int versionsatisfied(struct pkginfoperfile *it, struct deppossi *against) {
  82. return versionsatisfied3(&it->version,&against->version,against->verrel);
  83. }