Updated to fedora-glibc-20090427T1419
[glibc/history.git] / libio / vasprintf.c
blob7e15eb044a3f51d6fc051dce8bef860cb311a901
1 /* Copyright (C) 1995,1997,1999-2002,2004,2006,2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 #include <malloc.h>
30 #include <string.h>
31 #include "libioP.h"
32 #include "stdio.h"
33 #include <stdio_ext.h>
34 #include "strfile.h"
36 int
37 _IO_vasprintf (result_ptr, format, args)
38 char **result_ptr;
39 const char *format;
40 _IO_va_list args;
42 /* Initial size of the buffer to be used. Will be doubled each time an
43 overflow occurs. */
44 const _IO_size_t init_string_size = 100;
45 char *string;
46 _IO_strfile sf;
47 int ret;
48 _IO_size_t needed;
49 _IO_size_t allocated;
50 /* No need to clear the memory here (unlike for open_memstream) since
51 we know we will never seek on the stream. */
52 string = (char *) malloc (init_string_size);
53 if (string == NULL)
54 return -1;
55 #ifdef _IO_MTSAFE_IO
56 sf._sbf._f._lock = NULL;
57 #endif
58 _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
59 _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
60 _IO_str_init_static_internal (&sf, string, init_string_size, string);
61 sf._sbf._f._flags &= ~_IO_USER_BUF;
62 sf._s._allocate_buffer = (_IO_alloc_type) malloc;
63 sf._s._free_buffer = (_IO_free_type) free;
64 ret = INTUSE(_IO_vfprintf) (&sf._sbf._f, format, args);
65 if (ret < 0)
67 free (sf._sbf._f._IO_buf_base);
68 return ret;
70 /* Only use realloc if the size we need is of the same (binary)
71 order of magnitude then the memory we allocated. */
72 needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
73 allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
74 if ((allocated >> 1) <= needed)
75 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
76 else
78 *result_ptr = (char *) malloc (needed);
79 if (*result_ptr != NULL)
81 memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
82 free (sf._sbf._f._IO_buf_base);
84 else
85 /* We have no choice, use the buffer we already have. */
86 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
88 if (*result_ptr == NULL)
89 *result_ptr = sf._sbf._f._IO_buf_base;
90 (*result_ptr)[needed - 1] = '\0';
91 return ret;
93 ldbl_weak_alias (_IO_vasprintf, vasprintf)