1 /* Copyright (C) 1995, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
35 _IO_vasprintf (result_ptr
, format
, args
)
40 /* Initial size of the buffer to be used. Will be doubled each time an
42 const _IO_size_t init_string_size
= 100;
51 string
= (char *) malloc (init_string_size
);
55 sf
._sbf
._f
._lock
= &lock
;
57 _IO_no_init ((_IO_FILE
*) &sf
._sbf
, 0, -1, NULL
, NULL
);
58 _IO_JUMPS ((struct _IO_FILE_plus
*) &sf
._sbf
) = &_IO_str_jumps
;
59 _IO_str_init_static (&sf
, string
, init_string_size
, string
);
60 sf
._sbf
._f
._flags
&= ~_IO_USER_BUF
;
61 sf
._s
._allocate_buffer
= (_IO_alloc_type
) malloc
;
62 sf
._s
._free_buffer
= (_IO_free_type
) free
;
63 ret
= _IO_vfprintf ((_IO_FILE
*) &sf
, format
, args
);
66 /* Only use realloc if the size we need is of the same order of
67 magnitude then the memory we allocated. */
68 needed
= sf
._sbf
._f
._IO_write_ptr
- sf
._sbf
._f
._IO_write_base
+ 1;
69 allocated
= sf
._sbf
._f
._IO_write_end
- sf
._sbf
._f
._IO_write_base
;
70 if ((allocated
<< 1) <= needed
)
71 *result_ptr
= (char *) realloc (sf
._sbf
._f
._IO_buf_base
, needed
);
74 *result_ptr
= (char *) malloc (needed
);
75 if (*result_ptr
!= NULL
)
77 memcpy (*result_ptr
, sf
._sbf
._f
._IO_buf_base
, needed
- 1);
78 free (sf
._sbf
._f
._IO_buf_base
);
81 /* We have no choice, use the buffer we already have. */
82 *result_ptr
= (char *) realloc (sf
._sbf
._f
._IO_buf_base
, needed
);
84 if (*result_ptr
== NULL
)
85 *result_ptr
= sf
._sbf
._f
._IO_buf_base
;
86 (*result_ptr
)[needed
- 1] = '\0';
91 weak_alias (_IO_vasprintf
, vasprintf
)