1
/* gmp_vasprintf -- formatted output to an allocated space.
3
Copyright 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
38
#define vsnprintf __gmp_replacement_vsnprintf
42
/* vasprintf isn't used since we prefer all GMP allocs to go through
43
__gmp_allocate_func, and in particular we don't want the -1 return from
44
vasprintf for out-of-memory, instead __gmp_allocate_func should handle
45
that. Using vsnprintf unfortunately means we might have to re-run it if
46
our current space is insufficient.
48
The initial guess for the needed space is an arbitrary 256 bytes. If
49
that (and any extra GMP_ASPRINTF_T_NEED might give) isn't enough then an
50
ISO C99 standard vsnprintf will tell us what we really need.
52
GLIBC 2.0.x vsnprintf returns either -1 or space-1 to indicate overflow,
53
without giving any indication how much is really needed. In this case
54
keep trying with double the space each time.
56
A return of space-1 is success on a C99 vsnprintf, but we're not
57
bothering to identify which style vsnprintf we've got, so just take the
58
pessimistic option and assume it's glibc 2.0.x.
60
Notice the use of ret+2 for the new space in the C99 case. This ensures
61
the next vsnprintf return value will be space-2, which is unambiguously
62
successful. But actually GMP_ASPRINTF_T_NEED() will realloc to even
63
bigger than that ret+2.
65
vsnprintf might trash it's given ap, so copy it in case we need to use it
66
more than once. See comments with gmp_snprintf_format. */
69
gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt,
78
GMP_ASPRINTF_T_NEED (d, space);
79
space = d->alloc - d->size;
80
va_copy (ap, orig_ap);
81
ret = vsnprintf (d->buf + d->size, space, fmt, ap);
84
ASSERT (strlen (d->buf + d->size) == space-1);
88
/* done if output fits in our space */
93
space *= 2; /* possible glibc 2.0.x, so double */
95
space = ret+2; /* C99, so now know space required */
102
const struct doprnt_funs_t __gmp_asprintf_funs = {
103
(doprnt_format_t) gmp_asprintf_format,
104
(doprnt_memory_t) __gmp_asprintf_memory,
105
(doprnt_reps_t) __gmp_asprintf_reps,
106
(doprnt_final_t) __gmp_asprintf_final
110
gmp_vasprintf (char **result, const char *fmt, va_list ap)
112
struct gmp_asprintf_t d;
113
GMP_ASPRINTF_T_INIT (d, result);
114
return __gmp_doprnt (&__gmp_asprintf_funs, &d, fmt, ap);