]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/memstream.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / libio / memstream.c
CommitLineData
d614a753 1/* Copyright (C) 1995-2020 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
24struct _IO_FILE_memstream
25{
26 _IO_strfile _sf;
27 char **bufloc;
9964a145 28 size_t *sizeloc;
96aa2d94
RM
29};
30
31
9964a145
ZW
32static int _IO_mem_sync (FILE* fp) __THROW;
33static void _IO_mem_finish (FILE* fp, int) __THROW;
96aa2d94
RM
34
35
db3476af 36static const struct _IO_jump_t _IO_mem_jumps libio_vtable =
96aa2d94
RM
37{
38 JUMP_INIT_DUMMY,
77a58cad 39 JUMP_INIT (finish, _IO_mem_finish),
d18ea0c5
AS
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),
96aa2d94
RM
47 JUMP_INIT (seekpos, _IO_default_seekpos),
48 JUMP_INIT (setbuf, _IO_default_setbuf),
49 JUMP_INIT (sync, _IO_mem_sync),
d18ea0c5 50 JUMP_INIT (doallocate, _IO_default_doallocate),
96aa2d94
RM
51 JUMP_INIT (read, _IO_default_read),
52 JUMP_INIT (write, _IO_default_write),
53 JUMP_INIT (seek, _IO_default_seek),
77a58cad 54 JUMP_INIT (close, _IO_default_close),
dfd2257a
UD
55 JUMP_INIT (stat, _IO_default_stat),
56 JUMP_INIT(showmanyc, _IO_default_showmanyc),
57 JUMP_INIT(imbue, _IO_default_imbue)
96aa2d94
RM
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. */
9964a145
ZW
63FILE *
64__open_memstream (char **bufloc, size_t *sizeloc)
96aa2d94 65{
edf5b2d7
UD
66 struct locked_FILE
67 {
68 struct _IO_FILE_memstream fp;
499e7464 69#ifdef _IO_MTSAFE_IO
edf5b2d7 70 _IO_lock_t lock;
499e7464 71#endif
d64b6ad0 72 struct _IO_wide_data wd;
edf5b2d7 73 } *new_f;
96aa2d94
RM
74 char *buf;
75
edf5b2d7
UD
76 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
77 if (new_f == NULL)
96aa2d94 78 return NULL;
499e7464 79#ifdef _IO_MTSAFE_IO
f65fd747 80 new_f->fp._sf._sbf._f._lock = &new_f->lock;
499e7464 81#endif
96aa2d94 82
9964a145 83 buf = calloc (1, BUFSIZ);
f8b87ef0 84 if (buf == NULL)
3932737d
AM
85 {
86 free (new_f);
87 return NULL;
88 }
db3476af 89 _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
e69dcccb 90 _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
9964a145 91 _IO_str_init_static_internal (&new_f->fp._sf, buf, BUFSIZ, buf);
f65fd747 92 new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
4e8a6346
FW
93 new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
94 new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
96aa2d94 95
edf5b2d7
UD
96 new_f->fp.bufloc = bufloc;
97 new_f->fp.sizeloc = sizeloc;
77a58cad 98
de895ddc
SN
99 /* Disable single thread optimization. BZ 21735. */
100 new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
101
9964a145 102 return (FILE *) &new_f->fp._sf._sbf;
96aa2d94 103}
711f67a7
JM
104libc_hidden_def (__open_memstream)
105weak_alias (__open_memstream, open_memstream)
96aa2d94
RM
106
107
108static int
9964a145 109_IO_mem_sync (FILE *fp)
96aa2d94
RM
110{
111 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
96aa2d94
RM
112
113 if (fp->_IO_write_ptr == fp->_IO_write_end)
114 {
d18ea0c5 115 _IO_str_overflow (fp, '\0');
96aa2d94
RM
116 --fp->_IO_write_ptr;
117 }
96aa2d94 118
77a58cad
RM
119 *mp->bufloc = fp->_IO_write_base;
120 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
96aa2d94
RM
121
122 return 0;
123}
124
125
77a58cad 126static void
9964a145 127_IO_mem_finish (FILE *fp, int dummy)
96aa2d94
RM
128{
129 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
96aa2d94 130
77a58cad
RM
131 *mp->bufloc = (char *) realloc (fp->_IO_write_base,
132 fp->_IO_write_ptr - fp->_IO_write_base + 1);
133 if (*mp->bufloc != NULL)
134 {
135 (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
136 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
96aa2d94 137
bd7d6b40
UD
138 fp->_IO_buf_base = NULL;
139 }
a334319f 140
bd7d6b40 141 _IO_str_finish (fp, 0);
96aa2d94 142}