vercmp.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. static int verrevcmp(const char *val, const char *ref) {
  28. int vc, rc;
  29. long vl, rl;
  30. const char *vp, *rp;
  31. if (!val) val= "";
  32. if (!ref) ref= "";
  33. for (;;) {
  34. vp= val; while (*vp && !isdigit(*vp)) vp++;
  35. rp= ref; while (*rp && !isdigit(*rp)) rp++;
  36. for (;;) {
  37. vc= val == vp ? 0 : *val++;
  38. rc= ref == rp ? 0 : *ref++;
  39. if (!rc && !vc) break;
  40. if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
  41. if (rc && !isalpha(rc)) rc += 256;
  42. if (vc != rc) return vc - rc;
  43. }
  44. val= vp;
  45. ref= rp;
  46. vl=0; if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
  47. rl=0; if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
  48. if (vl != rl) return vl - rl;
  49. if (!*val && !*ref) return 0;
  50. if (!*val) return -1;
  51. if (!*ref) return +1;
  52. }
  53. }
  54. int versioncompare(const char *version, const char *revision,
  55. const char *refversion, const char *refrevision) {
  56. int r;
  57. r= verrevcmp(version,refversion); if (r) return r;
  58. return verrevcmp(revision,refrevision);
  59. }