chiark / gitweb /
gpg agent lockup fix: Interrupt main loop when active_connections_value==0
[gnupg2.git] / common / xasprintf.c
1 /* xasprintf.c
2  *      Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 #include <config.h>
31 #include <stdlib.h>
32 #include <errno.h>
33
34 #include "util.h"
35
36 /* Same as asprintf but return an allocated buffer suitable to be
37    freed using xfree.  This function simply dies on memory failure,
38    thus no extra check is required.
39
40    FIXME: We should remove these functions in favor of gpgrt_bsprintf
41    and a xgpgrt_bsprintf or rename them to xbsprintf and
42    xtrybsprintf.  */
43 char *
44 xasprintf (const char *fmt, ...)
45 {
46   va_list ap;
47   char *buf;
48
49   va_start (ap, fmt);
50   if (gpgrt_vasprintf (&buf, fmt, ap) < 0)
51     log_fatal ("estream_asprintf failed: %s\n", strerror (errno));
52   va_end (ap);
53   return buf;
54 }
55
56 /* Same as above but return NULL on memory failure.  */
57 char *
58 xtryasprintf (const char *fmt, ...)
59 {
60   int rc;
61   va_list ap;
62   char *buf;
63
64   va_start (ap, fmt);
65   rc = gpgrt_vasprintf (&buf, fmt, ap);
66   va_end (ap);
67   if (rc < 0)
68     return NULL;
69   return buf;
70 }