github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/printf/vasprintf.c (about)

     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 either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include <stdarg.h>
    32  #include <stdio.h>
    33  #include <stdlib.h>
    34  #include <string.h>
    35  
    36  #include "gmp.h"
    37  #include "gmp-impl.h"
    38  
    39  #if ! HAVE_VSNPRINTF
    40  #define vsnprintf  __gmp_replacement_vsnprintf
    41  #endif
    42  
    43  
    44  /* vasprintf isn't used since we prefer all GMP allocs to go through
    45     __gmp_allocate_func, and in particular we don't want the -1 return from
    46     vasprintf for out-of-memory, instead __gmp_allocate_func should handle
    47     that.  Using vsnprintf unfortunately means we might have to re-run it if
    48     our current space is insufficient.
    49  
    50     The initial guess for the needed space is an arbitrary 256 bytes.  If
    51     that (and any extra GMP_ASPRINTF_T_NEED might give) isn't enough then an
    52     ISO C99 standard vsnprintf will tell us what we really need.
    53  
    54     GLIBC 2.0.x vsnprintf returns either -1 or space-1 to indicate overflow,
    55     without giving any indication how much is really needed.  In this case
    56     keep trying with double the space each time.
    57  
    58     A return of space-1 is success on a C99 vsnprintf, but we're not
    59     bothering to identify which style vsnprintf we've got, so just take the
    60     pessimistic option and assume it's glibc 2.0.x.
    61  
    62     Notice the use of ret+2 for the new space in the C99 case.  This ensures
    63     the next vsnprintf return value will be space-2, which is unambiguously
    64     successful.  But actually GMP_ASPRINTF_T_NEED() will realloc to even
    65     bigger than that ret+2.
    66  
    67     vsnprintf might trash it's given ap, so copy it in case we need to use it
    68     more than once.  See comments with gmp_snprintf_format.  */
    69  
    70  static int
    71  gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt,
    72                       va_list orig_ap)
    73  {
    74    int      ret;
    75    va_list  ap;
    76    size_t   space = 256;
    77  
    78    for (;;)
    79      {
    80        GMP_ASPRINTF_T_NEED (d, space);
    81        space = d->alloc - d->size;
    82        va_copy (ap, orig_ap);
    83        ret = vsnprintf (d->buf + d->size, space, fmt, ap);
    84        if (ret == -1)
    85          {
    86            ASSERT (strlen (d->buf + d->size) == space-1);
    87            ret = space-1;
    88          }
    89  
    90        /* done if output fits in our space */
    91        if (ret < space-1)
    92          break;
    93  
    94        if (ret == space-1)
    95          space *= 2;     /* possible glibc 2.0.x, so double */
    96        else
    97          space = ret+2;  /* C99, so now know space required */
    98      }
    99  
   100    d->size += ret;
   101    return ret;
   102  }
   103  
   104  const struct doprnt_funs_t  __gmp_asprintf_funs = {
   105    (doprnt_format_t) gmp_asprintf_format,
   106    (doprnt_memory_t) __gmp_asprintf_memory,
   107    (doprnt_reps_t)   __gmp_asprintf_reps,
   108    (doprnt_final_t)  __gmp_asprintf_final
   109  };
   110  
   111  int
   112  gmp_vasprintf (char **result, const char *fmt, va_list ap)
   113  {
   114    struct gmp_asprintf_t  d;
   115    GMP_ASPRINTF_T_INIT (d, result);
   116    return __gmp_doprnt (&__gmp_asprintf_funs, &d, fmt, ap);
   117  }