Update syscall lists for Linux 6.2
[glibc.git] / stdio-common / wprintf_buffer_as_file.c
blobb2e5b5ada90518de61196de35933f0399eb6c96e
1 /* FILE * interface to a struct __wprintf_buffer. Wide version.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <printf_buffer_as_file.h>
21 #include <assert.h>
22 #include <printf_buffer.h>
24 /* Commit the data directly written through the stdio stream. */
25 static void
26 __wprintf_buffer_as_file_commit (struct __wprintf_buffer_as_file *file)
28 /* Check that the write pointers in the file stream are consistent
29 with the next buffer. */
30 assert (file->wide_stream._IO_write_ptr >= file->next->write_ptr);
31 assert (file->wide_stream._IO_write_ptr <= file->next->write_end);
32 assert (file->wide_stream._IO_write_base == file->next->write_base);
33 assert (file->wide_stream._IO_write_end == file->next->write_end);
35 file->next->write_ptr = file->wide_stream._IO_write_ptr;
38 /* Pointer the FILE * write buffer into the active struct __wprintf_buffer
39 area. */
40 static void
41 __wprintf_buffer_as_file_switch_to_buffer (struct __wprintf_buffer_as_file *file)
43 file->wide_stream._IO_write_base = file->next->write_base;
44 file->wide_stream._IO_write_ptr = file->next->write_ptr;
45 file->wide_stream._IO_write_end = file->next->write_end;
48 /* Only a small subset of the vtable functions is implemented here,
49 following _IO_obstack_jumps. */
51 static wint_t
52 __wprintf_buffer_as_file_overflow (FILE *fp, int ch)
54 struct __wprintf_buffer_as_file *file
55 = (struct __wprintf_buffer_as_file *) fp;
57 __wprintf_buffer_as_file_commit (file);
59 /* EOF means only a flush is requested. */
60 if (ch != WEOF)
61 __wprintf_buffer_putc (file->next, ch);
62 else
63 ch = 0;
65 /* Ensure that flushing actually produces room. */
66 if (!__wprintf_buffer_has_failed (file->next)
67 && file->next->write_ptr == file->next->write_end)
68 __wprintf_buffer_flush (file->next);
70 __wprintf_buffer_as_file_switch_to_buffer (file);
72 if (!__wprintf_buffer_has_failed (file->next))
73 return (unsigned char) ch;
74 else
75 return WEOF;
78 static size_t
79 __wprintf_buffer_as_file_xsputn (FILE *fp, const void *buf, size_t len)
81 struct __wprintf_buffer_as_file *file
82 = (struct __wprintf_buffer_as_file *) fp;
84 __wprintf_buffer_as_file_commit (file);
86 /* Copy the data. */
87 __wprintf_buffer_write (file->next, buf, len);
89 __wprintf_buffer_as_file_switch_to_buffer (file);
91 if (!__wprintf_buffer_has_failed (file->next))
92 return len;
93 else
94 /* We may actually have written something. But the stream is
95 corrupted in this case anyway, so try not to divine the write
96 count here. */
97 return 0;
100 static const struct _IO_jump_t _IO_wprintf_buffer_as_file_jumps libio_vtable =
102 JUMP_INIT_DUMMY,
103 JUMP_INIT(finish, NULL),
104 JUMP_INIT(overflow, (_IO_overflow_t) __wprintf_buffer_as_file_overflow),
105 JUMP_INIT(underflow, NULL),
106 JUMP_INIT(uflow, NULL),
107 JUMP_INIT(pbackfail, NULL),
108 JUMP_INIT(xsputn, __wprintf_buffer_as_file_xsputn),
109 JUMP_INIT(xsgetn, NULL),
110 JUMP_INIT(seekoff, NULL),
111 JUMP_INIT(seekpos, NULL),
112 JUMP_INIT(setbuf, NULL),
113 JUMP_INIT(sync, NULL),
114 JUMP_INIT(doallocate, NULL),
115 JUMP_INIT(read, NULL),
116 JUMP_INIT(write, NULL),
117 JUMP_INIT(seek, NULL),
118 JUMP_INIT(close, NULL),
119 JUMP_INIT(stat, NULL),
120 JUMP_INIT(showmanyc, NULL),
121 JUMP_INIT(imbue, NULL)
124 void
125 __wprintf_buffer_as_file_init (struct __wprintf_buffer_as_file *file,
126 struct __wprintf_buffer *next)
128 file->stream._lock = NULL;
129 _IO_no_init (&file->stream, _IO_USER_LOCK, 0, &file->wide_stream,
130 &_IO_wprintf_buffer_as_file_jumps);
131 _IO_fwide (&file->stream, 1);
133 /* Set up the write buffer from the next buffer. */
134 file->next = next;
135 __wprintf_buffer_as_file_switch_to_buffer (file);
137 /* Mark the read area as inactive, by making all pointers equal. */
138 file->stream._IO_read_base = file->stream._IO_write_base;
139 file->stream._IO_read_ptr = file->stream._IO_write_base;
140 file->stream._IO_read_end = file->stream._IO_write_base;
143 bool
144 __wprintf_buffer_as_file_terminate (struct __wprintf_buffer_as_file *file)
146 if (file->stream._flags & _IO_ERR_SEEN)
147 return false;
148 else
150 __wprintf_buffer_as_file_commit (file);
151 return true;