]> git.ozlabs.org Git - ccan/blob - ccan/asprintf/asprintf.h
958e244b56bc7a73ec71907da85dfcf810266315
[ccan] / ccan / asprintf / asprintf.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_ASPRINTF_H
3 #define CCAN_ASPRINTF_H
4 #include "config.h"
5 #include <ccan/compiler/compiler.h>
6
7 /**
8  * afmt - allocate and populate a string with the given format.
9  * @fmt: printf-style format.
10  *
11  * This is a simplified asprintf interface.  Returns NULL on error.
12  */
13 char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...);
14
15 #if HAVE_ASPRINTF
16 #include <stdio.h>
17 #else
18 #include <stdarg.h>
19 /**
20  * asprintf - printf to a dynamically-allocated string.
21  * @strp: pointer to the string to allocate.
22  * @fmt: printf-style format.
23  *
24  * Returns -1 (and leaves @strp undefined) on an error.  Otherwise returns
25  * number of bytes printed into @strp.
26  *
27  * Example:
28  *      static char *greeting(const char *name)
29  *      {
30  *              char *str;
31  *              int len = asprintf(&str, "Hello %s", name);
32  *              if (len < 0)
33  *                      return NULL;
34  *              return str;
35  *      }
36  */
37 int PRINTF_FMT(2, 3) asprintf(char **strp, const char *fmt, ...);
38
39 /**
40  * vasprintf - vprintf to a dynamically-allocated string.
41  * @strp: pointer to the string to allocate.
42  * @fmt: printf-style format.
43  *
44  * Returns -1 (and leaves @strp undefined) on an error.  Otherwise returns
45  * number of bytes printed into @strp.
46  */
47 int vasprintf(char **strp, const char *fmt, va_list ap);
48 #endif
49
50 #endif /* CCAN_ASPRINTF_H */