memstream.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 1995-2026 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. <https://www.gnu.org/licenses/>. */
  14. #include "libioP.h"
  15. #include "strfile.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. struct _IO_FILE_memstream
  19. {
  20. _IO_strfile _sf;
  21. char **bufloc;
  22. size_t *sizeloc;
  23. };
  24. /* Open a stream that writes into a malloc'd buffer that is expanded as
  25. necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
  26. and the number of characters written on fflush or fclose. */
  27. FILE *
  28. __open_memstream (char **bufloc, size_t *sizeloc)
  29. {
  30. struct locked_FILE
  31. {
  32. struct _IO_FILE_memstream fp;
  33. #ifdef _IO_MTSAFE_IO
  34. _IO_lock_t lock;
  35. #endif
  36. struct _IO_wide_data wd;
  37. } *new_f;
  38. char *buf;
  39. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  40. if (new_f == NULL)
  41. return NULL;
  42. #ifdef _IO_MTSAFE_IO
  43. new_f->fp._sf._sbf._f._lock = &new_f->lock;
  44. #endif
  45. buf = calloc (1, BUFSIZ);
  46. if (buf == NULL)
  47. {
  48. free (new_f);
  49. return NULL;
  50. }
  51. _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
  52. _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
  53. _IO_str_init_static_internal (&new_f->fp._sf, buf, BUFSIZ, buf);
  54. new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
  55. new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
  56. new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
  57. new_f->fp.bufloc = bufloc;
  58. new_f->fp.sizeloc = sizeloc;
  59. /* Disable single thread optimization. BZ 21735. */
  60. new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
  61. return (FILE *) &new_f->fp._sf._sbf;
  62. }
  63. libc_hidden_def (__open_memstream)
  64. weak_alias (__open_memstream, open_memstream)
  65. int
  66. _IO_mem_sync (FILE *fp)
  67. {
  68. struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
  69. if (fp->_IO_write_ptr == fp->_IO_write_end)
  70. {
  71. _IO_str_overflow (fp, '\0');
  72. --fp->_IO_write_ptr;
  73. }
  74. *mp->bufloc = fp->_IO_write_base;
  75. *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
  76. return 0;
  77. }
  78. void
  79. _IO_mem_finish (FILE *fp, int dummy)
  80. {
  81. struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
  82. *mp->bufloc = (char *) realloc (fp->_IO_write_base,
  83. fp->_IO_write_ptr - fp->_IO_write_base + 1);
  84. if (*mp->bufloc != NULL)
  85. {
  86. (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
  87. *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
  88. fp->_IO_buf_base = NULL;
  89. }
  90. _IO_str_finish (fp, 0);
  91. }