]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1995-2024 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
96aa2d94 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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 | |
478b92f0 UD |
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 | |
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 | |
96aa2d94 | 18 | #include "libioP.h" |
edf5b2d7 | 19 | #include "strfile.h" |
fa0bc87c | 20 | #include <stdio.h> |
96aa2d94 RM |
21 | #include <stdlib.h> |
22 | ||
23 | ||
24 | struct _IO_FILE_memstream | |
25 | { | |
26 | _IO_strfile _sf; | |
27 | char **bufloc; | |
9964a145 | 28 | size_t *sizeloc; |
96aa2d94 RM |
29 | }; |
30 | ||
31 | ||
96aa2d94 RM |
32 | /* Open a stream that writes into a malloc'd buffer that is expanded as |
33 | necessary. *BUFLOC and *SIZELOC are updated with the buffer's location | |
34 | and the number of characters written on fflush or fclose. */ | |
9964a145 ZW |
35 | FILE * |
36 | __open_memstream (char **bufloc, size_t *sizeloc) | |
96aa2d94 | 37 | { |
edf5b2d7 UD |
38 | struct locked_FILE |
39 | { | |
40 | struct _IO_FILE_memstream fp; | |
499e7464 | 41 | #ifdef _IO_MTSAFE_IO |
edf5b2d7 | 42 | _IO_lock_t lock; |
499e7464 | 43 | #endif |
d64b6ad0 | 44 | struct _IO_wide_data wd; |
edf5b2d7 | 45 | } *new_f; |
96aa2d94 RM |
46 | char *buf; |
47 | ||
edf5b2d7 UD |
48 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
49 | if (new_f == NULL) | |
96aa2d94 | 50 | return NULL; |
499e7464 | 51 | #ifdef _IO_MTSAFE_IO |
f65fd747 | 52 | new_f->fp._sf._sbf._f._lock = &new_f->lock; |
499e7464 | 53 | #endif |
96aa2d94 | 54 | |
9964a145 | 55 | buf = calloc (1, BUFSIZ); |
f8b87ef0 | 56 | if (buf == NULL) |
3932737d AM |
57 | { |
58 | free (new_f); | |
59 | return NULL; | |
60 | } | |
db3476af | 61 | _IO_init_internal (&new_f->fp._sf._sbf._f, 0); |
e69dcccb | 62 | _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps; |
9964a145 | 63 | _IO_str_init_static_internal (&new_f->fp._sf, buf, BUFSIZ, buf); |
f65fd747 | 64 | new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF; |
4e8a6346 FW |
65 | new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc; |
66 | new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free; | |
96aa2d94 | 67 | |
edf5b2d7 UD |
68 | new_f->fp.bufloc = bufloc; |
69 | new_f->fp.sizeloc = sizeloc; | |
77a58cad | 70 | |
de895ddc SN |
71 | /* Disable single thread optimization. BZ 21735. */ |
72 | new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK; | |
73 | ||
9964a145 | 74 | return (FILE *) &new_f->fp._sf._sbf; |
96aa2d94 | 75 | } |
711f67a7 JM |
76 | libc_hidden_def (__open_memstream) |
77 | weak_alias (__open_memstream, open_memstream) | |
96aa2d94 RM |
78 | |
79 | ||
3020f726 | 80 | int |
9964a145 | 81 | _IO_mem_sync (FILE *fp) |
96aa2d94 RM |
82 | { |
83 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; | |
96aa2d94 RM |
84 | |
85 | if (fp->_IO_write_ptr == fp->_IO_write_end) | |
86 | { | |
d18ea0c5 | 87 | _IO_str_overflow (fp, '\0'); |
96aa2d94 RM |
88 | --fp->_IO_write_ptr; |
89 | } | |
96aa2d94 | 90 | |
77a58cad RM |
91 | *mp->bufloc = fp->_IO_write_base; |
92 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; | |
96aa2d94 RM |
93 | |
94 | return 0; | |
95 | } | |
96 | ||
97 | ||
3020f726 | 98 | void |
9964a145 | 99 | _IO_mem_finish (FILE *fp, int dummy) |
96aa2d94 RM |
100 | { |
101 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; | |
96aa2d94 | 102 | |
77a58cad RM |
103 | *mp->bufloc = (char *) realloc (fp->_IO_write_base, |
104 | fp->_IO_write_ptr - fp->_IO_write_base + 1); | |
105 | if (*mp->bufloc != NULL) | |
106 | { | |
107 | (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0'; | |
108 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; | |
96aa2d94 | 109 | |
bd7d6b40 UD |
110 | fp->_IO_buf_base = NULL; |
111 | } | |
a334319f | 112 | |
bd7d6b40 | 113 | _IO_str_finish (fp, 0); |
96aa2d94 | 114 | } |