vercmp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*BK modified april 2011*/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. # define _(Text) Text
  27. struct versionrevision {
  28. unsigned long epoch;
  29. char *version;
  30. const char *revision;
  31. };
  32. static int verrevcmp(const char *val, const char *ref)
  33. {
  34. int vc, rc;
  35. long vl, rl;
  36. const char *vp, *rp;
  37. const char *vsep, *rsep;
  38. if (!val) val= "";
  39. if (!ref) ref= "";
  40. for (;;) {
  41. vp= val; while (*vp && !isdigit(*vp)) vp++;
  42. rp= ref; while (*rp && !isdigit(*rp)) rp++;
  43. for (;;) {
  44. vc= val == vp ? 0 : *val++;
  45. rc= ref == rp ? 0 : *ref++;
  46. if (!rc && !vc) break;
  47. if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
  48. if (rc && !isalpha(rc)) rc += 256;
  49. if (vc != rc) return vc - rc;
  50. }
  51. val= vp;
  52. ref= rp;
  53. vl=0; if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
  54. rl=0; if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
  55. if (vl != rl) return vl - rl;
  56. vc = *val;
  57. rc = *ref;
  58. vsep = strchr(".-", vc);
  59. rsep = strchr(".-", rc);
  60. if (vsep && !rsep) return -1;
  61. if (!vsep && rsep) return +1;
  62. if (!*val && !*ref) return 0;
  63. if (!*val) return -1;
  64. if (!*ref) return +1;
  65. }
  66. }
  67. int versioncompare(const struct versionrevision *version,
  68. const struct versionrevision *refversion)
  69. {
  70. int r;
  71. if (version->epoch > refversion->epoch) return 1;
  72. if (version->epoch < refversion->epoch) return -1;
  73. r= verrevcmp(version->version,refversion->version); if (r) return r;
  74. r= verrevcmp(version->revision,refversion->revision); if (r) return r;
  75. return r;
  76. }
  77. int versionsatisfied3(const struct versionrevision *it,
  78. const struct versionrevision *ref,
  79. const char *op)
  80. {
  81. int r;
  82. r= versioncompare(it,ref);
  83. if (strcmp(op, "le") == 0 || strcmp(op, "<") == 0)
  84. return r <= 0;
  85. if (strcmp(op, "ge") == 0 || strcmp(op, ">") == 0)
  86. return r >= 0;
  87. if (strcmp(op, "lt") == 0)
  88. return r < 0;
  89. if (strcmp(op, "gt") == 0)
  90. return r > 0;
  91. if (strcmp(op, "eq") == 0)
  92. return r == 0;
  93. fprintf(stderr, "unknown operator: %s", op);
  94. exit(1);
  95. }
  96. const char *parseversion(struct versionrevision *rversion, const char *string)
  97. {
  98. char *hyphen, *colon, *eepochcolon;
  99. unsigned long epoch;
  100. if (!*string) return _("version string is empty");
  101. colon= strchr(string,':');
  102. if (colon) {
  103. epoch= strtoul(string,&eepochcolon,10);
  104. if (colon != eepochcolon) return _("epoch in version is not number");
  105. if (!*++colon) return _("nothing after colon in version number");
  106. string= colon;
  107. rversion->epoch= epoch;
  108. } else {
  109. rversion->epoch= 0;
  110. }
  111. rversion->revision = "";
  112. rversion->version= malloc(strlen(string)+1);
  113. strcpy(rversion->version, string);
  114. #if 0
  115. fprintf(stderr,"Parsed version: %lu, %s\n",
  116. rversion->epoch,
  117. rversion->version);
  118. #endif
  119. return 0;
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. const char *err;
  124. struct versionrevision ver, ref;
  125. if (argc < 4) {
  126. fprintf(stderr, "usage: %s version1 lt|gt|le|ge|eq version2\n return value 0 if true, else 1\n", argv[0]);
  127. return 2;
  128. }
  129. err = parseversion(&ver, argv[1]);
  130. if (err) {
  131. fprintf(stderr, "Invalid version `%s': %s\n", argv[1], err);
  132. return 2;
  133. }
  134. err = parseversion(&ref, argv[3]);
  135. if (err) {
  136. fprintf(stderr, "Invalid version `%s': %s\n", argv[3], err);
  137. return 2;
  138. }
  139. return ! versionsatisfied3(&ver, &ref, argv[2]);
  140. }