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 3 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. If not, see http://www.gnu.org/licenses/. */
36 #define vsnprintf __gmp_replacement_vsnprintf
40 /* vasprintf isn't used since we prefer all GMP allocs to go through
41 __gmp_allocate_func, and in particular we don't want the -1 return from
42 vasprintf for out-of-memory, instead __gmp_allocate_func should handle
43 that. Using vsnprintf unfortunately means we might have to re-run it if
44 our current space is insufficient.
46 The initial guess for the needed space is an arbitrary 256 bytes. If
47 that (and any extra GMP_ASPRINTF_T_NEED might give) isn't enough then an
48 ISO C99 standard vsnprintf will tell us what we really need.
50 GLIBC 2.0.x vsnprintf returns either -1 or space-1 to indicate overflow,
51 without giving any indication how much is really needed. In this case
52 keep trying with double the space each time.
54 A return of space-1 is success on a C99 vsnprintf, but we're not
55 bothering to identify which style vsnprintf we've got, so just take the
56 pessimistic option and assume it's glibc 2.0.x.
58 Notice the use of ret+2 for the new space in the C99 case. This ensures
59 the next vsnprintf return value will be space-2, which is unambiguously
60 successful. But actually GMP_ASPRINTF_T_NEED() will realloc to even
61 bigger than that ret+2.
63 vsnprintf might trash it's given ap, so copy it in case we need to use it
64 more than once. See comments with gmp_snprintf_format. */
67 gmp_asprintf_format (struct gmp_asprintf_t
*d
, const char *fmt
,
76 GMP_ASPRINTF_T_NEED (d
, space
);
77 space
= d
->alloc
- d
->size
;
78 va_copy (ap
, orig_ap
);
79 ret
= vsnprintf (d
->buf
+ d
->size
, space
, fmt
, ap
);
82 ASSERT (strlen (d
->buf
+ d
->size
) == space
-1);
86 /* done if output fits in our space */
91 space
*= 2; /* possible glibc 2.0.x, so double */
93 space
= ret
+2; /* C99, so now know space required */
100 const struct doprnt_funs_t __gmp_asprintf_funs
= {
101 (doprnt_format_t
) gmp_asprintf_format
,
102 (doprnt_memory_t
) __gmp_asprintf_memory
,
103 (doprnt_reps_t
) __gmp_asprintf_reps
,
104 (doprnt_final_t
) __gmp_asprintf_final
108 gmp_vasprintf (char **result
, const char *fmt
, va_list ap
)
110 struct gmp_asprintf_t d
;
111 GMP_ASPRINTF_T_INIT (d
, result
);
112 return __gmp_doprnt (&__gmp_asprintf_funs
, &d
, fmt
, ap
);