Back to index

glibc  2.9
vasprintf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1999-2002,2004,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.
00018 
00019    As a special exception, if you link the code in this file with
00020    files compiled with a GNU compiler to produce an executable,
00021    that does not cause the resulting executable to be covered by
00022    the GNU Lesser General Public License.  This exception does not
00023    however invalidate any other reasons why the executable file
00024    might be covered by the GNU Lesser General Public License.
00025    This exception applies to code released by its copyright holders
00026    in files containing the exception.  */
00027 
00028 #include <malloc.h>
00029 #include <string.h>
00030 #include "libioP.h"
00031 #include "stdio.h"
00032 #include <stdio_ext.h>
00033 #include "strfile.h"
00034 
00035 int
00036 _IO_vasprintf (result_ptr, format, args)
00037      char **result_ptr;
00038      const char *format;
00039      _IO_va_list args;
00040 {
00041   /* Initial size of the buffer to be used.  Will be doubled each time an
00042      overflow occurs.  */
00043   const _IO_size_t init_string_size = 100;
00044   char *string;
00045   _IO_strfile sf;
00046   int ret;
00047   _IO_size_t needed;
00048   _IO_size_t allocated;
00049   /* No need to clear the memory here (unlike for open_memstream) since
00050      we know we will never seek on the stream.  */
00051   string = (char *) malloc (init_string_size);
00052   if (string == NULL)
00053     return -1;
00054 #ifdef _IO_MTSAFE_IO
00055   sf._sbf._f._lock = NULL;
00056 #endif
00057   _IO_no_init ((_IO_FILE *) &sf._sbf, _IO_USER_LOCK, -1, NULL, NULL);
00058   _IO_JUMPS ((struct _IO_FILE_plus *) &sf._sbf) = &_IO_str_jumps;
00059   _IO_str_init_static_internal (&sf, string, init_string_size, string);
00060   sf._sbf._f._flags &= ~_IO_USER_BUF;
00061   sf._s._allocate_buffer = (_IO_alloc_type) malloc;
00062   sf._s._free_buffer = (_IO_free_type) free;
00063   ret = INTUSE(_IO_vfprintf) (&sf._sbf._f, format, args);
00064   if (ret < 0)
00065     {
00066       free (sf._sbf._f._IO_buf_base);
00067       return ret;
00068     }
00069   /* Only use realloc if the size we need is of the same (binary)
00070      order of magnitude then the memory we allocated.  */
00071   needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
00072   allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
00073   if ((allocated >> 1) <= needed)
00074     *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
00075   else
00076     {
00077       *result_ptr = (char *) malloc (needed);
00078       if (*result_ptr != NULL)
00079        {
00080          memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
00081          free (sf._sbf._f._IO_buf_base);
00082        }
00083       else
00084        /* We have no choice, use the buffer we already have.  */
00085        *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
00086     }
00087   if (*result_ptr == NULL)
00088     *result_ptr = sf._sbf._f._IO_buf_base;
00089   (*result_ptr)[needed - 1] = '\0';
00090   return ret;
00091 }
00092 ldbl_weak_alias (_IO_vasprintf, vasprintf)