mprintf.c 466 Bytes
Newer Older
Julian Rother's avatar
Julian Rother committed
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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "../util.h"

char *vmprintf(const char *fmt, va_list ap)
{
	char *ptr;
	size_t size;
	FILE *stream;
	ptr = 0;
	size = 0;
	if (!(stream = open_memstream(&ptr, &size)))
		exit(99);
	if (vfprintf(stream, fmt, ap) < 0)
		exit(99);
	fclose(stream);
	return ptr;
}

char *mprintf(const char *fmt, ...)
{
	char *ret;
	va_list ap;
	va_start(ap, fmt);
	ret = vmprintf(fmt, ap);
	va_end(ap);
	return ret;
}