summaryrefslogtreecommitdiff
path: root/lib/vasprintf.c
blob: 24e30d8731c59ac635f4557fcdde777dfec395b3 (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
#include <config.h>
#include <stdio.h>
#include "vasprintf.h"

#ifndef HAVE_VASPRINTF

#define MAX_BSIZE 1024
#define NO_MORE_MAX (16*MAX_BSIZE)

int _gnutls_vasprintf(char **strp, const char *fmt, va_list ap)
{
	char *buf;
	int ret, max;

	max = MAX_BSIZE / 2;

	do {
		max *= 2;

		buf = malloc(max);
		if (buf == NULL)
			return -1;

		ret = vsnprintf(buf, max, fmt, ap);
	}
	while (ret > max && max < NO_MORE_MAX);

	return ret;
}

#endif