summaryrefslogtreecommitdiff
path: root/asprintf.h
blob: acec2b12ff034582936b99ba0cfc28d234382e74 (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
#ifndef ASPRINTF_H
#define ASPRINTF_H

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

static inline int vasprintf(char **strp, const char *fmt, va_list ap)
{
	va_list cp;
	va_copy(cp, ap);

	int len = vsnprintf(NULL, 0, fmt, ap) + 1;
	if (len < 0) {
		return -1;
	}

	*strp = malloc(len);
	if (*strp == NULL) {
		return -1;
	}

	int ret = vsnprintf(*strp, len, fmt, cp);

	va_end(cp);
	return ret;
}

static inline int asprintf(char **strp, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	int ret = vasprintf(strp, fmt, ap);

	va_end(ap);
	return ret;
}

#endif