vasprintf.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 1995-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <strfile.h>
  25. int
  26. __vasprintf_internal (char **result_ptr, const char *format, va_list args,
  27. unsigned int mode_flags)
  28. {
  29. /* Initial size of the buffer to be used. Will be doubled each time an
  30. overflow occurs. */
  31. const size_t init_string_size = 100;
  32. char *string;
  33. _IO_strfile sf;
  34. int ret;
  35. size_t needed;
  36. size_t allocated;
  37. /* No need to clear the memory here (unlike for open_memstream) since
  38. we know we will never seek on the stream. */
  39. string = (char *) malloc (init_string_size);
  40. if (string == NULL)
  41. return -1;
  42. #ifdef _IO_MTSAFE_IO
  43. sf._sbf._f._lock = NULL;
  44. #endif
  45. _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
  46. _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
  47. _IO_str_init_static_internal (&sf, string, init_string_size, string);
  48. sf._sbf._f._flags &= ~_IO_USER_BUF;
  49. sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
  50. sf._s._free_buffer_unused = (_IO_free_type) free;
  51. ret = __vfprintf_internal (&sf._sbf._f, format, args, mode_flags);
  52. if (ret < 0)
  53. {
  54. free (sf._sbf._f._IO_buf_base);
  55. return ret;
  56. }
  57. /* Only use realloc if the size we need is of the same (binary)
  58. order of magnitude then the memory we allocated. */
  59. needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
  60. allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
  61. if ((allocated >> 1) <= needed)
  62. *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
  63. else
  64. {
  65. *result_ptr = (char *) malloc (needed);
  66. if (*result_ptr != NULL)
  67. {
  68. memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
  69. free (sf._sbf._f._IO_buf_base);
  70. }
  71. else
  72. /* We have no choice, use the buffer we already have. */
  73. *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
  74. }
  75. if (*result_ptr == NULL)
  76. *result_ptr = sf._sbf._f._IO_buf_base;
  77. (*result_ptr)[needed - 1] = '\0';
  78. return ret;
  79. }
  80. int
  81. __vasprintf (char **result_ptr, const char *format, va_list args)
  82. {
  83. return __vasprintf_internal (result_ptr, format, args, 0);
  84. }
  85. ldbl_weak_alias (__vasprintf, vasprintf)