error.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * error.c - error message reporting
  4. *
  5. * Copyright © 2011 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 <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <errno.h>
  23. #include <stdlib.h>
  24. #include <dpkg/dpkg.h>
  25. #include <dpkg/varbuf.h>
  26. #include <dpkg/error.h>
  27. static void DPKG_ATTR_VPRINTF(3)
  28. dpkg_error_set(struct dpkg_error *err, int type, const char *fmt, va_list args)
  29. {
  30. struct varbuf str = VARBUF_INIT;
  31. if (err == NULL)
  32. return;
  33. err->type = type;
  34. varbuf_vprintf(&str, fmt, args);
  35. err->str = str.buf;
  36. }
  37. int
  38. dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
  39. {
  40. va_list args;
  41. va_start(args, fmt);
  42. dpkg_error_set(err, DPKG_MSG_WARN, fmt, args);
  43. va_end(args);
  44. return -1;
  45. }
  46. int
  47. dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
  48. {
  49. va_list args;
  50. va_start(args, fmt);
  51. dpkg_error_set(err, DPKG_MSG_ERROR, fmt, args);
  52. va_end(args);
  53. return -1;
  54. }
  55. int
  56. dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
  57. {
  58. va_list args;
  59. char *new_fmt;
  60. int errno_saved = errno;
  61. va_start(args, fmt);
  62. m_asprintf(&new_fmt, "%s (%s)", fmt, strerror(errno_saved));
  63. va_end(args);
  64. dpkg_error_set(err, DPKG_MSG_ERROR, new_fmt, args);
  65. free(new_fmt);
  66. return -1;
  67. }
  68. void
  69. dpkg_error_destroy(struct dpkg_error *err)
  70. {
  71. err->type = DPKG_MSG_NONE;
  72. free(err->str);
  73. err->str = NULL;
  74. }