memstream.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #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. static int _IO_mem_sync (FILE* fp) __THROW;
  25. static void _IO_mem_finish (FILE* fp, int) __THROW;
  26. static const struct _IO_jump_t _IO_mem_jumps libio_vtable =
  27. {
  28. JUMP_INIT_DUMMY,
  29. JUMP_INIT (finish, _IO_mem_finish),
  30. JUMP_INIT (overflow, _IO_str_overflow),
  31. JUMP_INIT (underflow, _IO_str_underflow),
  32. JUMP_INIT (uflow, _IO_default_uflow),
  33. JUMP_INIT (pbackfail, _IO_str_pbackfail),
  34. JUMP_INIT (xsputn, _IO_default_xsputn),
  35. JUMP_INIT (xsgetn, _IO_default_xsgetn),
  36. JUMP_INIT (seekoff, _IO_str_seekoff),
  37. JUMP_INIT (seekpos, _IO_default_seekpos),
  38. JUMP_INIT (setbuf, _IO_default_setbuf),
  39. JUMP_INIT (sync, _IO_mem_sync),
  40. JUMP_INIT (doallocate, _IO_default_doallocate),
  41. JUMP_INIT (read, _IO_default_read),
  42. JUMP_INIT (write, _IO_default_write),
  43. JUMP_INIT (seek, _IO_default_seek),
  44. JUMP_INIT (close, _IO_default_close),
  45. JUMP_INIT (stat, _IO_default_stat),
  46. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  47. JUMP_INIT(imbue, _IO_default_imbue)
  48. };
  49. /* Open a stream that writes into a malloc'd buffer that is expanded as
  50. necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
  51. and the number of characters written on fflush or fclose. */
  52. FILE *
  53. __open_memstream (char **bufloc, size_t *sizeloc)
  54. {
  55. struct locked_FILE
  56. {
  57. struct _IO_FILE_memstream fp;
  58. #ifdef _IO_MTSAFE_IO
  59. _IO_lock_t lock;
  60. #endif
  61. struct _IO_wide_data wd;
  62. } *new_f;
  63. char *buf;
  64. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  65. if (new_f == NULL)
  66. return NULL;
  67. #ifdef _IO_MTSAFE_IO
  68. new_f->fp._sf._sbf._f._lock = &new_f->lock;
  69. #endif
  70. buf = calloc (1, BUFSIZ);
  71. if (buf == NULL)
  72. {
  73. free (new_f);
  74. return NULL;
  75. }
  76. _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
  77. _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
  78. _IO_str_init_static_internal (&new_f->fp._sf, buf, BUFSIZ, buf);
  79. new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
  80. new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
  81. new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
  82. new_f->fp.bufloc = bufloc;
  83. new_f->fp.sizeloc = sizeloc;
  84. /* Disable single thread optimization. BZ 21735. */
  85. new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
  86. return (FILE *) &new_f->fp._sf._sbf;
  87. }
  88. libc_hidden_def (__open_memstream)
  89. weak_alias (__open_memstream, open_memstream)
  90. static int
  91. _IO_mem_sync (FILE *fp)
  92. {
  93. struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
  94. if (fp->_IO_write_ptr == fp->_IO_write_end)
  95. {
  96. _IO_str_overflow (fp, '\0');
  97. --fp->_IO_write_ptr;
  98. }
  99. *mp->bufloc = fp->_IO_write_base;
  100. *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
  101. return 0;
  102. }
  103. static void
  104. _IO_mem_finish (FILE *fp, int dummy)
  105. {
  106. struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
  107. *mp->bufloc = (char *) realloc (fp->_IO_write_base,
  108. fp->_IO_write_ptr - fp->_IO_write_base + 1);
  109. if (*mp->bufloc != NULL)
  110. {
  111. (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
  112. *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
  113. fp->_IO_buf_base = NULL;
  114. }
  115. _IO_str_finish (fp, 0);
  116. }