po: Update German man pages translation
[dpkg.git] / lib / dpkg / error.c
blob42bdb43d42e05c89d958ce4cbc2ee68a92bb29a7
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * error.c - error message reporting
5 * Copyright © 2011-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #include <dpkg/dpkg.h>
29 #include <dpkg/varbuf.h>
30 #include <dpkg/error.h>
32 static void DPKG_ATTR_VPRINTF(4)
33 dpkg_error_set(struct dpkg_error *err, enum dpkg_msg_type type, int syserrno,
34 const char *fmt, va_list args)
36 struct varbuf str = VARBUF_INIT;
38 if (err == NULL)
39 return;
41 err->type = type;
42 err->syserrno = syserrno;
44 varbuf_vprintf(&str, fmt, args);
45 if (syserrno)
46 varbuf_printf(&str, " (%s)", strerror(syserrno));
48 err->str = str.buf;
51 bool
52 dpkg_has_error(struct dpkg_error *err)
54 return err != NULL && err->type != DPKG_MSG_NONE;
57 int
58 dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
60 va_list args;
62 va_start(args, fmt);
63 dpkg_error_set(err, DPKG_MSG_WARN, 0, fmt, args);
64 va_end(args);
66 return -1;
69 int
70 dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
72 va_list args;
74 va_start(args, fmt);
75 dpkg_error_set(err, DPKG_MSG_ERROR, 0, fmt, args);
76 va_end(args);
78 return -1;
81 int
82 dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
84 va_list args;
86 va_start(args, fmt);
87 dpkg_error_set(err, DPKG_MSG_ERROR, errno, fmt, args);
88 va_end(args);
90 return -1;
93 void
94 dpkg_error_print(struct dpkg_error *err, const char *fmt, ...)
96 va_list args;
97 char *str;
99 va_start(args, fmt);
100 m_vasprintf(&str, fmt, args);
101 va_end(args);
103 if (err->type == DPKG_MSG_WARN)
104 warning("%s: %s", str, err->str);
105 else
106 ohshit("%s: %s", str, err->str);
108 free(str);
111 void
112 dpkg_error_move(struct dpkg_error *dst, struct dpkg_error *src)
114 dst->type = src->type;
115 src->type = DPKG_MSG_NONE;
116 dst->str = src->str;
117 src->str = NULL;
120 void
121 dpkg_error_destroy(struct dpkg_error *err)
123 err->type = DPKG_MSG_NONE;
124 err->syserrno = 0;
125 free(err->str);
126 err->str = NULL;