h8300: Assembly functions
[uclibc-ng.git] / libc / stdio / vasprintf.c
bloba901ee815520b3e6573c87ef802082b8bf6d5f28
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
8 #include <features.h>
10 #ifdef __USE_GNU
11 #include "_stdio.h"
12 #include <stdarg.h>
13 #include <bits/uClibc_va_copy.h>
16 #ifdef __UCLIBC_MJN3_ONLY__
17 /* Do the memstream stuff inline to avoid fclose and the openlist? */
18 #warning CONSIDER: avoid open_memstream call?
19 #endif
21 #ifndef __STDIO_HAS_VSNPRINTF
22 #warning Skipping vasprintf since no vsnprintf!
23 #else
25 int vasprintf(char **__restrict buf, const char * __restrict format,
26 va_list arg)
28 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
30 FILE *f;
31 size_t size;
32 int rv = -1;
34 *buf = NULL;
36 if ((f = open_memstream(buf, &size)) != NULL) {
37 rv = vfprintf(f, format, arg);
38 fclose(f);
39 if (rv < 0) {
40 free(*buf);
41 *buf = NULL;
45 assert(rv >= -1);
47 return rv;
49 #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
51 /* This implementation actually calls the printf machinery twice, but
52 * only does one malloc. This can be a problem though when custom printf
53 * specs or the %m specifier are involved because the results of the
54 * second call might be different from the first. */
55 va_list arg2;
56 int rv;
58 va_copy(arg2, arg);
59 rv = vsnprintf(NULL, 0, format, arg2);
60 va_end(arg2);
62 *buf = NULL;
64 if (rv >= 0) {
65 if ((*buf = malloc(++rv)) != NULL) {
66 if ((rv = vsnprintf(*buf, rv, format, arg)) < 0) {
67 free(*buf);
68 *buf = NULL;
73 assert(rv >= -1);
75 return rv;
77 #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
79 libc_hidden_def(vasprintf)
81 #endif
82 #endif