summaryrefslogtreecommitdiffstats
path: root/contrib/ccan/asprintf/asprintf.h
blob: d4cc5ca2dfea658464e854a1ca7e19987aa05f6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* SPDX-License-Identifier: MIT
 * Source: https://ccodearchive.net/info/asprintf.html */
#ifndef CCAN_ASPRINTF_H
#define CCAN_ASPRINTF_H
#include "config.h"
#include <ccan/compiler/compiler.h>

/**
 * afmt - allocate and populate a string with the given format.
 * @fmt: printf-style format.
 *
 * This is a simplified asprintf interface.  Returns NULL on error.
 */
char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...);

#if HAVE_ASPRINTF
#include <stdio.h>
#else
#include <stdarg.h>
/**
 * asprintf - printf to a dynamically-allocated string.
 * @strp: pointer to the string to allocate.
 * @fmt: printf-style format.
 *
 * Returns -1 (and leaves @strp undefined) on an error.  Otherwise returns
 * number of bytes printed into @strp.
 *
 * Example:
 *	static char *greeting(const char *name)
 *	{
 *		char *str;
 *		int len = asprintf(&str, "Hello %s", name);
 *		if (len < 0)
 *			return NULL;
 *		return str;
 *	}
 */
int PRINTF_FMT(2, 3) asprintf(char **strp, const char *fmt, ...);

/**
 * vasprintf - vprintf to a dynamically-allocated string.
 * @strp: pointer to the string to allocate.
 * @fmt: printf-style format.
 *
 * Returns -1 (and leaves @strp undefined) on an error.  Otherwise returns
 * number of bytes printed into @strp.
 */
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif

#endif /* CCAN_ASPRINTF_H */