vercmp.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. int versioncompare(const struct versionrevision *version,
  60. const struct versionrevision *refversion) {
  61. int r;
  62. if (version->epoch > refversion->epoch) return 1;
  63. if (version->epoch < refversion->epoch) return -1;
  64. r= verrevcmp(version->version,refversion->version); if (r) return r;
  65. return verrevcmp(version->revision,refversion->revision);
  66. }
  67. int versionsatisfied3(const struct versionrevision *it,
  68. const struct versionrevision *ref,
  69. enum depverrel verrel) {
  70. int r;
  71. if (verrel == dvr_none) return 1;
  72. r= versioncompare(it,ref);
  73. switch (verrel) {
  74. case dvr_earlierequal: return r <= 0;
  75. case dvr_laterequal: return r >= 0;
  76. case dvr_earlierstrict: return r < 0;
  77. case dvr_laterstrict: return r > 0;
  78. case dvr_exact: return r == 0;
  79. default: internerr("unknown verrel");
  80. }
  81. return 0;
  82. }
  83. int versionsatisfied(struct pkginfoperfile *it, struct deppossi *against) {
  84. return versionsatisfied3(&it->version,&against->version,against->verrel);
  85. }