Fix build for hurd/thread-self.c for i386.
[glibc.git] / libio / wmemstream.c
blob7b04b15cbcfe47c79cae39da60ee337bf5f3c849
1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
18 #include "libioP.h"
19 #include "strfile.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <wchar.h>
25 struct _IO_FILE_wmemstream
27 _IO_strfile _sf;
28 wchar_t **bufloc;
29 size_t *sizeloc;
33 /* Open a stream that writes into a malloc'd buffer that is expanded as
34 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
35 and the number of characters written on fflush or fclose. */
36 FILE *
37 open_wmemstream (wchar_t **bufloc, size_t *sizeloc)
39 struct locked_FILE
41 struct _IO_FILE_wmemstream fp;
42 #ifdef _IO_MTSAFE_IO
43 _IO_lock_t lock;
44 #endif
45 struct _IO_wide_data wd;
46 } *new_f;
47 wchar_t *buf;
49 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
50 if (new_f == NULL)
51 return NULL;
52 #ifdef _IO_MTSAFE_IO
53 new_f->fp._sf._sbf._f._lock = &new_f->lock;
54 #endif
56 buf = calloc (1, BUFSIZ);
57 if (buf == NULL)
59 free (new_f);
60 return NULL;
62 _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
63 _IO_fwide (&new_f->fp._sf._sbf._f, 1);
64 _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
65 BUFSIZ / sizeof (wchar_t), buf);
66 new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
67 new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
68 new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
70 new_f->fp.bufloc = bufloc;
71 new_f->fp.sizeloc = sizeloc;
73 /* Disable single thread optimization. BZ 21735. */
74 new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
76 return (FILE *) &new_f->fp._sf._sbf;
80 int
81 _IO_wmem_sync (FILE *fp)
83 struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
85 if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
87 _IO_wstr_overflow (fp, '\0');
88 --fp->_wide_data->_IO_write_ptr;
91 *mp->bufloc = fp->_wide_data->_IO_write_base;
92 *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
93 - fp->_wide_data->_IO_write_base);
95 return 0;
99 void
100 _IO_wmem_finish (FILE *fp, int dummy)
102 struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
104 *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
105 (fp->_wide_data->_IO_write_ptr
106 - fp->_wide_data->_IO_write_base + 1)
107 * sizeof (wchar_t));
108 if (*mp->bufloc != NULL)
110 size_t len = (fp->_wide_data->_IO_write_ptr
111 - fp->_wide_data->_IO_write_base);
112 (*mp->bufloc)[len] = '\0';
113 *mp->sizeloc = len;
115 fp->_wide_data->_IO_buf_base = NULL;
118 _IO_wstr_finish (fp, 0);