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