]>
Commit | Line | Data |
---|---|---|
f521be31 UD |
1 | /* Copyright (C) 1995,1997,1999-2002,2004,2006,2009 |
2 | Free Software Foundation, Inc. | |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
96aa2d94 | 4 | |
41bdb6e2 AJ |
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. | |
96aa2d94 | 9 | |
41bdb6e2 AJ |
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 | |
40a55d20 | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
41bdb6e2 | 13 | Lesser General Public License for more details. |
96aa2d94 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. | |
96aa2d94 | 18 | |
41bdb6e2 AJ |
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. */ | |
96aa2d94 RM |
27 | |
28 | #include <malloc.h> | |
761df3a7 | 29 | #include <string.h> |
96aa2d94 RM |
30 | #include "libioP.h" |
31 | #include "stdio.h" | |
0d875352 | 32 | #include <stdio_ext.h> |
96aa2d94 RM |
33 | #include "strfile.h" |
34 | ||
35 | int | |
36 | _IO_vasprintf (result_ptr, format, args) | |
37 | char **result_ptr; | |
38 | const char *format; | |
39 | _IO_va_list args; | |
40 | { | |
41 | /* Initial size of the buffer to be used. Will be doubled each time an | |
42 | overflow occurs. */ | |
43 | const _IO_size_t init_string_size = 100; | |
44 | char *string; | |
45 | _IO_strfile sf; | |
46 | int ret; | |
4100c5a0 UD |
47 | _IO_size_t needed; |
48 | _IO_size_t allocated; | |
107b8a92 UD |
49 | /* No need to clear the memory here (unlike for open_memstream) since |
50 | we know we will never seek on the stream. */ | |
40a55d20 | 51 | string = (char *) malloc (init_string_size); |
96aa2d94 RM |
52 | if (string == NULL) |
53 | return -1; | |
499e7464 | 54 | #ifdef _IO_MTSAFE_IO |
c020d48c | 55 | sf._sbf._f._lock = NULL; |
499e7464 | 56 | #endif |
f521be31 UD |
57 | _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL); |
58 | _IO_JUMPS (&sf._sbf) = &_IO_str_jumps; | |
40a54e4d | 59 | _IO_str_init_static_internal (&sf, string, init_string_size, string); |
f65fd747 | 60 | sf._sbf._f._flags &= ~_IO_USER_BUF; |
f8b87ef0 UD |
61 | sf._s._allocate_buffer = (_IO_alloc_type) malloc; |
62 | sf._s._free_buffer = (_IO_free_type) free; | |
77fe0b9c | 63 | ret = INTUSE(_IO_vfprintf) (&sf._sbf._f, format, args); |
96aa2d94 | 64 | if (ret < 0) |
383bd1c5 UD |
65 | { |
66 | free (sf._sbf._f._IO_buf_base); | |
67 | return ret; | |
68 | } | |
5cec9552 UD |
69 | /* Only use realloc if the size we need is of the same (binary) |
70 | order of magnitude then the memory we allocated. */ | |
4100c5a0 UD |
71 | needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1; |
72 | allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base; | |
5cec9552 | 73 | if ((allocated >> 1) <= needed) |
4100c5a0 UD |
74 | *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed); |
75 | else | |
76 | { | |
77 | *result_ptr = (char *) malloc (needed); | |
78 | if (*result_ptr != NULL) | |
79 | { | |
519ba0a3 | 80 | memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1); |
4100c5a0 UD |
81 | free (sf._sbf._f._IO_buf_base); |
82 | } | |
83 | else | |
84 | /* We have no choice, use the buffer we already have. */ | |
85 | *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed); | |
86 | } | |
96aa2d94 | 87 | if (*result_ptr == NULL) |
f65fd747 | 88 | *result_ptr = sf._sbf._f._IO_buf_base; |
519ba0a3 | 89 | (*result_ptr)[needed - 1] = '\0'; |
96aa2d94 RM |
90 | return ret; |
91 | } | |
c6251f03 | 92 | ldbl_weak_alias (_IO_vasprintf, vasprintf) |