dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / error.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * error.c - error message reporting
4 *
5 * Copyright © 2011-2015 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
21#include <config.h>
22#include <compat.h>
23
24#include <errno.h>
25#include <string.h>
26#include <stdlib.h>
27
28#include <dpkg/dpkg.h>
29#include <dpkg/varbuf.h>
30#include <dpkg/error.h>
31
32static void DPKG_ATTR_VPRINTF(3)
33dpkg_error_set(struct dpkg_error *err, int type, const char *fmt, va_list args)
34{
35 struct varbuf str = VARBUF_INIT;
36
37 if (err == NULL)
38 return;
39
40 err->type = type;
41
42 varbuf_vprintf(&str, fmt, args);
43 err->str = str.buf;
44}
45
46int
47dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
48{
49 va_list args;
50
51 va_start(args, fmt);
52 dpkg_error_set(err, DPKG_MSG_WARN, fmt, args);
53 va_end(args);
54
55 return -1;
56}
57
58int
59dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
60{
61 va_list args;
62
63 va_start(args, fmt);
64 dpkg_error_set(err, DPKG_MSG_ERROR, fmt, args);
65 va_end(args);
66
67 return -1;
68}
69
70int
71dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
72{
73 va_list args;
74 char *new_fmt;
75
76 new_fmt = str_fmt("%s (%s)", fmt, strerror(errno));
77
78 va_start(args, fmt);
79 dpkg_error_set(err, DPKG_MSG_ERROR, new_fmt, args);
80 va_end(args);
81
82 free(new_fmt);
83
84 return -1;
85}
86
87void
88dpkg_error_print(struct dpkg_error *err, const char *fmt, ...)
89{
90 va_list args;
91 char *str;
92
93 va_start(args, fmt);
94 m_vasprintf(&str, fmt, args);
95 va_end(args);
96
97 if (err->type == DPKG_MSG_WARN)
98 warning("%s: %s", str, err->str);
99 else
100 ohshit("%s: %s", str, err->str);
101
102 free(str);
103}
104
105void
106dpkg_error_destroy(struct dpkg_error *err)
107{
108 err->type = DPKG_MSG_NONE;
109 free(err->str);
110 err->str = NULL;
111}