error.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. m_asprintf(&new_fmt, "%s (%s)", fmt, strerror(errno));
  61. va_start(args, fmt);
  62. dpkg_error_set(err, DPKG_MSG_ERROR, new_fmt, args);
  63. va_end(args);
  64. free(new_fmt);
  65. return -1;
  66. }
  67. void
  68. dpkg_error_destroy(struct dpkg_error *err)
  69. {
  70. err->type = DPKG_MSG_NONE;
  71. free(err->str);
  72. err->str = NULL;
  73. }