summaryrefslogtreecommitdiff
path: root/libc/stdio/vasprintf.c
blob: 1ef75df9433b5d880a309b0875db7b60f4d0fbf9 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
 *
 * GNU Library General Public License (LGPL) version 2 or later.
 *
 * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
 */

#include "_stdio.h"
#include <stdarg.h>

#ifdef __UCLIBC_MJN3_ONLY__
/* Do the memstream stuff inline to avoid fclose and the openlist? */
#warning CONSIDER: avoid open_memstream call?
#endif

#ifndef __STDIO_HAS_VSNPRINTF
#warning Skipping vasprintf since no vsnprintf!
#else

/* Deal with pre-C99 compilers. */

#ifndef va_copy

#ifdef __va_copy
#define va_copy(A,B)	__va_copy(A,B)
#else
#warning Neither va_copy (C99/SUSv3) nor __va_copy is defined.  Using a simple copy instead.  But you should really check that this is appropriate...
#define va_copy(A,B)	A = B
#endif

#endif /* va_copy */

int vasprintf(char **__restrict buf, const char * __restrict format,
			 va_list arg)
{
#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__

	FILE *f;
	size_t size;
	int rv = -1;

	*buf = NULL;

	if ((f = open_memstream(buf, &size)) != NULL) {
		rv = vfprintf(f, format, arg);
		fclose(f);
		if (rv < 0) {
			free(*buf);
			*buf = NULL;
		}
	}

	assert(rv >= -1);

	return rv;

#else  /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */

	/* This implementation actually calls the printf machinery twice, but only
	 * only does one malloc.  This can be a problem though when custom printf
	 * specs or the %m specifier are involved because the results of the
	 * second call might be different from the first. */
	va_list arg2;
	int rv;

	va_copy(arg2, arg);
 	rv = vsnprintf(NULL, 0, format, arg2);
	va_end(arg2);

	*buf = NULL;

	if (rv >= 0) {
		if ((*buf = malloc(++rv)) != NULL) {
			if ((rv = vsnprintf(*buf, rv, format, arg)) < 0) {
				free(*buf);
				*buf = NULL;
			}
		}
	}

	assert(rv >= -1);

	return rv;

#endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
}

#endif