summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/vasprintf.c
blob: 1f012caa4004505b9890737a4d582b816e76e91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdarg.h> // for va_copy, va_end
#include <stdio.h>  // for vsnprintf, va_list, vasprintf
#include <stdlib.h> // for malloc

// TODO: maybe use memstream in future??
int vasprintf(char **restrict ptr, const char *restrict format, va_list ap)
{
	int l;
	va_list ap2;
	va_copy(ap2, ap);
	l = vsnprintf(0, 0, format, ap2);
	va_end(ap2);

	if (l < 0 || !(*ptr = malloc(l + 1U)))
		return -1;

	return vsnprintf(*ptr, l + 1U, format, ap);
}