xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-2016 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 | #include "libioP.h" |
| 19 | #include "strfile.h" |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | |
| 24 | struct _IO_FILE_memstream |
| 25 | { |
| 26 | _IO_strfile _sf; |
| 27 | char **bufloc; |
| 28 | _IO_size_t *sizeloc; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | static int _IO_mem_sync (_IO_FILE* fp) __THROW; |
| 33 | static void _IO_mem_finish (_IO_FILE* fp, int) __THROW; |
| 34 | |
| 35 | |
| 36 | static const struct _IO_jump_t _IO_mem_jumps = |
| 37 | { |
| 38 | JUMP_INIT_DUMMY, |
| 39 | JUMP_INIT (finish, _IO_mem_finish), |
| 40 | JUMP_INIT (overflow, _IO_str_overflow), |
| 41 | JUMP_INIT (underflow, _IO_str_underflow), |
| 42 | JUMP_INIT (uflow, _IO_default_uflow), |
| 43 | JUMP_INIT (pbackfail, _IO_str_pbackfail), |
| 44 | JUMP_INIT (xsputn, _IO_default_xsputn), |
| 45 | JUMP_INIT (xsgetn, _IO_default_xsgetn), |
| 46 | JUMP_INIT (seekoff, _IO_str_seekoff), |
| 47 | JUMP_INIT (seekpos, _IO_default_seekpos), |
| 48 | JUMP_INIT (setbuf, _IO_default_setbuf), |
| 49 | JUMP_INIT (sync, _IO_mem_sync), |
| 50 | JUMP_INIT (doallocate, _IO_default_doallocate), |
| 51 | JUMP_INIT (read, _IO_default_read), |
| 52 | JUMP_INIT (write, _IO_default_write), |
| 53 | JUMP_INIT (seek, _IO_default_seek), |
| 54 | JUMP_INIT (close, _IO_default_close), |
| 55 | JUMP_INIT (stat, _IO_default_stat), |
| 56 | JUMP_INIT(showmanyc, _IO_default_showmanyc), |
| 57 | JUMP_INIT(imbue, _IO_default_imbue) |
| 58 | }; |
| 59 | |
| 60 | /* Open a stream that writes into a malloc'd buffer that is expanded as |
| 61 | necessary. *BUFLOC and *SIZELOC are updated with the buffer's location |
| 62 | and the number of characters written on fflush or fclose. */ |
| 63 | _IO_FILE * |
| 64 | __open_memstream (char **bufloc, _IO_size_t *sizeloc) |
| 65 | { |
| 66 | struct locked_FILE |
| 67 | { |
| 68 | struct _IO_FILE_memstream fp; |
| 69 | #ifdef _IO_MTSAFE_IO |
| 70 | _IO_lock_t lock; |
| 71 | #endif |
| 72 | struct _IO_wide_data wd; |
| 73 | } *new_f; |
| 74 | char *buf; |
| 75 | |
| 76 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
| 77 | if (new_f == NULL) |
| 78 | return NULL; |
| 79 | #ifdef _IO_MTSAFE_IO |
| 80 | new_f->fp._sf._sbf._f._lock = &new_f->lock; |
| 81 | #endif |
| 82 | |
| 83 | buf = calloc (1, _IO_BUFSIZ); |
| 84 | if (buf == NULL) |
| 85 | { |
| 86 | free (new_f); |
| 87 | return NULL; |
| 88 | } |
| 89 | _IO_init (&new_f->fp._sf._sbf._f, 0); |
| 90 | _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps; |
| 91 | _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf); |
| 92 | new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF; |
| 93 | new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc; |
| 94 | new_f->fp._sf._s._free_buffer = (_IO_free_type) free; |
| 95 | |
| 96 | new_f->fp.bufloc = bufloc; |
| 97 | new_f->fp.sizeloc = sizeloc; |
| 98 | |
| 99 | return (_IO_FILE *) &new_f->fp._sf._sbf; |
| 100 | } |
| 101 | libc_hidden_def (__open_memstream) |
| 102 | weak_alias (__open_memstream, open_memstream) |
| 103 | |
| 104 | |
| 105 | static int |
| 106 | _IO_mem_sync (_IO_FILE *fp) |
| 107 | { |
| 108 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; |
| 109 | |
| 110 | if (fp->_IO_write_ptr == fp->_IO_write_end) |
| 111 | { |
| 112 | _IO_str_overflow (fp, '\0'); |
| 113 | --fp->_IO_write_ptr; |
| 114 | } |
| 115 | else |
| 116 | *fp->_IO_write_ptr = '\0'; |
| 117 | |
| 118 | *mp->bufloc = fp->_IO_write_base; |
| 119 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static void |
| 126 | _IO_mem_finish (_IO_FILE *fp, int dummy) |
| 127 | { |
| 128 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; |
| 129 | |
| 130 | *mp->bufloc = (char *) realloc (fp->_IO_write_base, |
| 131 | fp->_IO_write_ptr - fp->_IO_write_base + 1); |
| 132 | if (*mp->bufloc != NULL) |
| 133 | { |
| 134 | (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0'; |
| 135 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; |
| 136 | |
| 137 | fp->_IO_buf_base = NULL; |
| 138 | } |
| 139 | |
| 140 | _IO_str_finish (fp, 0); |
| 141 | } |