]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/vasprintf.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / libio / vasprintf.c
CommitLineData
d614a753 1/* Copyright (C) 1995-2020 Free Software Foundation, Inc.
41bdb6e2 2 This file is part of the GNU C Library.
96aa2d94 3
41bdb6e2
AJ
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.
96aa2d94 8
41bdb6e2
AJ
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
40a55d20 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
96aa2d94 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>.
96aa2d94 17
41bdb6e2
AJ
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. */
96aa2d94 26
761df3a7 27#include <string.h>
698fb75b
ZW
28#include <stdlib.h>
29#include <strfile.h>
96aa2d94
RM
30
31int
698fb75b
ZW
32__vasprintf_internal (char **result_ptr, const char *format, va_list args,
33 unsigned int mode_flags)
96aa2d94
RM
34{
35 /* Initial size of the buffer to be used. Will be doubled each time an
36 overflow occurs. */
9964a145 37 const size_t init_string_size = 100;
96aa2d94
RM
38 char *string;
39 _IO_strfile sf;
40 int ret;
9964a145
ZW
41 size_t needed;
42 size_t allocated;
107b8a92
UD
43 /* No need to clear the memory here (unlike for open_memstream) since
44 we know we will never seek on the stream. */
40a55d20 45 string = (char *) malloc (init_string_size);
96aa2d94
RM
46 if (string == NULL)
47 return -1;
499e7464 48#ifdef _IO_MTSAFE_IO
c020d48c 49 sf._sbf._f._lock = NULL;
499e7464 50#endif
f521be31
UD
51 _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
52 _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
40a54e4d 53 _IO_str_init_static_internal (&sf, string, init_string_size, string);
f65fd747 54 sf._sbf._f._flags &= ~_IO_USER_BUF;
4e8a6346
FW
55 sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
56 sf._s._free_buffer_unused = (_IO_free_type) free;
698fb75b 57 ret = __vfprintf_internal (&sf._sbf._f, format, args, mode_flags);
96aa2d94 58 if (ret < 0)
383bd1c5
UD
59 {
60 free (sf._sbf._f._IO_buf_base);
61 return ret;
62 }
5cec9552
UD
63 /* Only use realloc if the size we need is of the same (binary)
64 order of magnitude then the memory we allocated. */
4100c5a0
UD
65 needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
66 allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
5cec9552 67 if ((allocated >> 1) <= needed)
4100c5a0
UD
68 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
69 else
70 {
71 *result_ptr = (char *) malloc (needed);
72 if (*result_ptr != NULL)
73 {
519ba0a3 74 memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
4100c5a0
UD
75 free (sf._sbf._f._IO_buf_base);
76 }
77 else
78 /* We have no choice, use the buffer we already have. */
79 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
80 }
96aa2d94 81 if (*result_ptr == NULL)
f65fd747 82 *result_ptr = sf._sbf._f._IO_buf_base;
519ba0a3 83 (*result_ptr)[needed - 1] = '\0';
96aa2d94
RM
84 return ret;
85}
698fb75b
ZW
86
87int
88__vasprintf (char **result_ptr, const char *format, va_list args)
89{
90 return __vasprintf_internal (result_ptr, format, args, 0);
91}
92ldbl_weak_alias (__vasprintf, vasprintf)