Merge branch 'vendor/MDOCML'
[dragonfly.git] / contrib / gmp / printf / vasprintf.c
1 /* gmp_vasprintf -- formatted output to an allocated space.
2
3 Copyright 2001 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
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.
11
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.
16
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/.  */
19
20 #include "config.h"
21
22 #if HAVE_STDARG
23 #include <stdarg.h>
24 #else
25 #include <varargs.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "gmp.h"
33 #include "gmp-impl.h"
34
35 #if ! HAVE_VSNPRINTF
36 #define vsnprintf  __gmp_replacement_vsnprintf
37 #endif
38
39
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.
45
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.
49
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.
53
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.
57
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.
62
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.  */
65
66 static int
67 gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt,
68                      va_list orig_ap)
69 {
70   int      ret;
71   va_list  ap;
72   size_t   space = 256;
73
74   for (;;)
75     {
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);
80       if (ret == -1)
81         {
82           ASSERT (strlen (d->buf + d->size) == space-1);
83           ret = space-1;
84         }
85
86       /* done if output fits in our space */
87       if (ret < space-1)
88         break;
89
90       if (ret == space-1)
91         space *= 2;     /* possible glibc 2.0.x, so double */
92       else
93         space = ret+2;  /* C99, so now know space required */
94     }
95
96   d->size += ret;
97   return ret;
98 }
99
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
105 };
106
107 int
108 gmp_vasprintf (char **result, const char *fmt, va_list ap)
109 {
110   struct gmp_asprintf_t  d;
111   GMP_ASPRINTF_T_INIT (d, result);
112   return __gmp_doprnt (&__gmp_asprintf_funs, &d, fmt, ap);
113 }