deb-version.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * deb-version.c - deb format version handling routines
  4. *
  5. * Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <dpkg/i18n.h>
  25. #include <dpkg/c-ctype.h>
  26. #include <dpkg/dpkg.h>
  27. #include <dpkg/deb-version.h>
  28. /**
  29. * Parse a .deb format version.
  30. *
  31. * It takes a string and parses a .deb archive format version in the form
  32. * of "X.Y", without any leading whitespace, and ending in either a newline
  33. * or a NUL. If there is any syntax error a descriptive error string is
  34. * returned.
  35. *
  36. * @param version The version to return.
  37. * @param str The string to parse.
  38. *
  39. * @return An error string, or NULL if there was no error.
  40. */
  41. const char *
  42. deb_version_parse(struct deb_version *version, const char *str)
  43. {
  44. const char *str_minor, *end;
  45. int major = 0;
  46. int minor = 0;
  47. for (end = str; *end && c_isdigit(*end); end++)
  48. major = major * 10 + *end - '0';
  49. if (end == str)
  50. return _("format version with empty major component");
  51. if (*end != '.')
  52. return _("format version has no dot");
  53. for (end = str_minor = end + 1; *end && c_isdigit(*end); end++)
  54. minor = minor * 10 + *end - '0';
  55. if (end == str_minor)
  56. return _("format version with empty minor component");
  57. if (*end != '\n' && *end != '\0')
  58. return _("format version followed by junk");
  59. version->major = major;
  60. version->minor = minor;
  61. return NULL;
  62. }