1 | /* Copyright (C) 1995-2018 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
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. |
8 | |
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. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. |
17 | |
18 | As a special exception, if you link the code in this file with |
19 | files compiled with a GNU compiler to produce an executable, |
20 | that does not cause the resulting executable to be covered by |
21 | the GNU Lesser General Public License. This exception does not |
22 | however invalidate any other reasons why the executable file |
23 | might be covered by the GNU Lesser General Public License. |
24 | This exception applies to code released by its copyright holders |
25 | in files containing the exception. */ |
26 | |
27 | #include <malloc.h> |
28 | #include <string.h> |
29 | #include "libioP.h" |
30 | #include "stdio.h" |
31 | #include <stdio_ext.h> |
32 | #include "strfile.h" |
33 | |
34 | int |
35 | _IO_vasprintf (char **result_ptr, const char *format, _IO_va_list args) |
36 | { |
37 | /* Initial size of the buffer to be used. Will be doubled each time an |
38 | overflow occurs. */ |
39 | const _IO_size_t init_string_size = 100; |
40 | char *string; |
41 | _IO_strfile sf; |
42 | int ret; |
43 | _IO_size_t needed; |
44 | _IO_size_t allocated; |
45 | /* No need to clear the memory here (unlike for open_memstream) since |
46 | we know we will never seek on the stream. */ |
47 | string = (char *) malloc (init_string_size); |
48 | if (string == NULL) |
49 | return -1; |
50 | #ifdef _IO_MTSAFE_IO |
51 | sf._sbf._f._lock = NULL; |
52 | #endif |
53 | _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL); |
54 | _IO_JUMPS (&sf._sbf) = &_IO_str_jumps; |
55 | _IO_str_init_static_internal (&sf, string, init_string_size, string); |
56 | sf._sbf._f._flags &= ~_IO_USER_BUF; |
57 | sf._s._allocate_buffer = (_IO_alloc_type) malloc; |
58 | sf._s._free_buffer = (_IO_free_type) free; |
59 | ret = _IO_vfprintf (&sf._sbf._f, format, args); |
60 | if (ret < 0) |
61 | { |
62 | free (sf._sbf._f._IO_buf_base); |
63 | return ret; |
64 | } |
65 | /* Only use realloc if the size we need is of the same (binary) |
66 | order of magnitude then the memory we allocated. */ |
67 | needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1; |
68 | allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base; |
69 | if ((allocated >> 1) <= needed) |
70 | *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed); |
71 | else |
72 | { |
73 | *result_ptr = (char *) malloc (needed); |
74 | if (*result_ptr != NULL) |
75 | { |
76 | memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1); |
77 | free (sf._sbf._f._IO_buf_base); |
78 | } |
79 | else |
80 | /* We have no choice, use the buffer we already have. */ |
81 | *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed); |
82 | } |
83 | if (*result_ptr == NULL) |
84 | *result_ptr = sf._sbf._f._IO_buf_base; |
85 | (*result_ptr)[needed - 1] = '\0'; |
86 | return ret; |
87 | } |
88 | ldbl_weak_alias (_IO_vasprintf, vasprintf) |
89 | |