1/* Copyright (C) 1993-2018 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 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
26
27/* NOTE: libio is now exclusively used only by glibc since libstdc++ has its
28 own implementation. As a result, functions that were implemented for C++
29 (like *sputn) may no longer have C++ semantics. This is of course only
30 relevant for internal callers of these functions since these functions are
31 not intended for external use otherwise.
32
33 FIXME: All of the C++ cruft eventually needs to go away. */
34
35#ifndef _LIBIOP_H
36#define _LIBIOP_H 1
37
38#include <stddef.h>
39
40#include <errno.h>
41#include <libc-lock.h>
42
43#include <math_ldbl_opt.h>
44
45#include <stdio.h>
46#include <libio/libio.h>
47#include "iolibio.h"
48
49#include <shlib-compat.h>
50
51/* For historical reasons this is the name of the sysdeps header that
52 adjusts the libio configuration. */
53#include <_G_config.h>
54
55#define _IO_seek_set 0
56#define _IO_seek_cur 1
57#define _IO_seek_end 2
58
59/* THE JUMPTABLE FUNCTIONS.
60
61 * The _IO_FILE type is used to implement the FILE type in GNU libc,
62 * as well as the streambuf class in GNU iostreams for C++.
63 * These are all the same, just used differently.
64 * An _IO_FILE (or FILE) object is allows followed by a pointer to
65 * a jump table (of pointers to functions). The pointer is accessed
66 * with the _IO_JUMPS macro. The jump table has an eccentric format,
67 * so as to be compatible with the layout of a C++ virtual function table.
68 * (as implemented by g++). When a pointer to a streambuf object is
69 * coerced to an (FILE*), then _IO_JUMPS on the result just
70 * happens to point to the virtual function table of the streambuf.
71 * Thus the _IO_JUMPS function table used for C stdio/libio does
72 * double duty as the virtual function table for C++ streambuf.
73 *
74 * The entries in the _IO_JUMPS function table (and hence also the
75 * virtual functions of a streambuf) are described below.
76 * The first parameter of each function entry is the _IO_FILE/streambuf
77 * object being acted on (i.e. the 'this' parameter).
78 */
79
80/* Setting this macro to 1 enables the use of the _vtable_offset bias
81 in _IO_JUMPS_FUNCS, below. This is only needed for new-format
82 _IO_FILE in libc that must support old binaries (see oldfileops.c). */
83#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1) && !defined _IO_USE_OLD_IO_FILE
84# define _IO_JUMPS_OFFSET 1
85#else
86# define _IO_JUMPS_OFFSET 0
87#endif
88
89/* Type of MEMBER in struct type TYPE. */
90#define _IO_MEMBER_TYPE(TYPE, MEMBER) __typeof__ (((TYPE){}).MEMBER)
91
92/* Essentially ((TYPE *) THIS)->MEMBER, but avoiding the aliasing
93 violation in case THIS has a different pointer type. */
94#define _IO_CAST_FIELD_ACCESS(THIS, TYPE, MEMBER) \
95 (*(_IO_MEMBER_TYPE (TYPE, MEMBER) *)(((char *) (THIS)) \
96 + offsetof(TYPE, MEMBER)))
97
98#define _IO_JUMPS(THIS) (THIS)->vtable
99#define _IO_JUMPS_FILE_plus(THIS) \
100 _IO_CAST_FIELD_ACCESS ((THIS), struct _IO_FILE_plus, vtable)
101#define _IO_WIDE_JUMPS(THIS) \
102 _IO_CAST_FIELD_ACCESS ((THIS), struct _IO_FILE, _wide_data)->_wide_vtable
103#define _IO_CHECK_WIDE(THIS) \
104 (_IO_CAST_FIELD_ACCESS ((THIS), struct _IO_FILE, _wide_data) != NULL)
105
106#if _IO_JUMPS_OFFSET
107# define _IO_JUMPS_FUNC(THIS) \
108 (IO_validate_vtable \
109 (*(struct _IO_jump_t **) ((void *) &_IO_JUMPS_FILE_plus (THIS) \
110 + (THIS)->_vtable_offset)))
111# define _IO_vtable_offset(THIS) (THIS)->_vtable_offset
112#else
113# define _IO_JUMPS_FUNC(THIS) (IO_validate_vtable (_IO_JUMPS_FILE_plus (THIS)))
114# define _IO_vtable_offset(THIS) 0
115#endif
116#define _IO_WIDE_JUMPS_FUNC(THIS) _IO_WIDE_JUMPS(THIS)
117#define JUMP_FIELD(TYPE, NAME) TYPE NAME
118#define JUMP0(FUNC, THIS) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS)
119#define JUMP1(FUNC, THIS, X1) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS, X1)
120#define JUMP2(FUNC, THIS, X1, X2) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS, X1, X2)
121#define JUMP3(FUNC, THIS, X1,X2,X3) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS, X1,X2, X3)
122#define JUMP_INIT(NAME, VALUE) VALUE
123#define JUMP_INIT_DUMMY JUMP_INIT(dummy, 0), JUMP_INIT (dummy2, 0)
124
125#define WJUMP0(FUNC, THIS) (_IO_WIDE_JUMPS_FUNC(THIS)->FUNC) (THIS)
126#define WJUMP1(FUNC, THIS, X1) (_IO_WIDE_JUMPS_FUNC(THIS)->FUNC) (THIS, X1)
127#define WJUMP2(FUNC, THIS, X1, X2) (_IO_WIDE_JUMPS_FUNC(THIS)->FUNC) (THIS, X1, X2)
128#define WJUMP3(FUNC, THIS, X1,X2,X3) (_IO_WIDE_JUMPS_FUNC(THIS)->FUNC) (THIS, X1,X2, X3)
129
130/* The 'finish' function does any final cleaning up of an _IO_FILE object.
131 It does not delete (free) it, but does everything else to finalize it.
132 It matches the streambuf::~streambuf virtual destructor. */
133typedef void (*_IO_finish_t) (FILE *, int); /* finalize */
134#define _IO_FINISH(FP) JUMP1 (__finish, FP, 0)
135#define _IO_WFINISH(FP) WJUMP1 (__finish, FP, 0)
136
137/* The 'overflow' hook flushes the buffer.
138 The second argument is a character, or EOF.
139 It matches the streambuf::overflow virtual function. */
140typedef int (*_IO_overflow_t) (FILE *, int);
141#define _IO_OVERFLOW(FP, CH) JUMP1 (__overflow, FP, CH)
142#define _IO_WOVERFLOW(FP, CH) WJUMP1 (__overflow, FP, CH)
143
144/* The 'underflow' hook tries to fills the get buffer.
145 It returns the next character (as an unsigned char) or EOF. The next
146 character remains in the get buffer, and the get position is not changed.
147 It matches the streambuf::underflow virtual function. */
148typedef int (*_IO_underflow_t) (FILE *);
149#define _IO_UNDERFLOW(FP) JUMP0 (__underflow, FP)
150#define _IO_WUNDERFLOW(FP) WJUMP0 (__underflow, FP)
151
152/* The 'uflow' hook returns the next character in the input stream
153 (cast to unsigned char), and increments the read position;
154 EOF is returned on failure.
155 It matches the streambuf::uflow virtual function, which is not in the
156 cfront implementation, but was added to C++ by the ANSI/ISO committee. */
157#define _IO_UFLOW(FP) JUMP0 (__uflow, FP)
158#define _IO_WUFLOW(FP) WJUMP0 (__uflow, FP)
159
160/* The 'pbackfail' hook handles backing up.
161 It matches the streambuf::pbackfail virtual function. */
162typedef int (*_IO_pbackfail_t) (FILE *, int);
163#define _IO_PBACKFAIL(FP, CH) JUMP1 (__pbackfail, FP, CH)
164#define _IO_WPBACKFAIL(FP, CH) WJUMP1 (__pbackfail, FP, CH)
165
166/* The 'xsputn' hook writes upto N characters from buffer DATA.
167 Returns EOF or the number of character actually written.
168 It matches the streambuf::xsputn virtual function. */
169typedef size_t (*_IO_xsputn_t) (FILE *FP, const void *DATA,
170 size_t N);
171#define _IO_XSPUTN(FP, DATA, N) JUMP2 (__xsputn, FP, DATA, N)
172#define _IO_WXSPUTN(FP, DATA, N) WJUMP2 (__xsputn, FP, DATA, N)
173
174/* The 'xsgetn' hook reads upto N characters into buffer DATA.
175 Returns the number of character actually read.
176 It matches the streambuf::xsgetn virtual function. */
177typedef size_t (*_IO_xsgetn_t) (FILE *FP, void *DATA, size_t N);
178#define _IO_XSGETN(FP, DATA, N) JUMP2 (__xsgetn, FP, DATA, N)
179#define _IO_WXSGETN(FP, DATA, N) WJUMP2 (__xsgetn, FP, DATA, N)
180
181/* The 'seekoff' hook moves the stream position to a new position
182 relative to the start of the file (if DIR==0), the current position
183 (MODE==1), or the end of the file (MODE==2).
184 It matches the streambuf::seekoff virtual function.
185 It is also used for the ANSI fseek function. */
186typedef off64_t (*_IO_seekoff_t) (FILE *FP, off64_t OFF, int DIR,
187 int MODE);
188#define _IO_SEEKOFF(FP, OFF, DIR, MODE) JUMP3 (__seekoff, FP, OFF, DIR, MODE)
189#define _IO_WSEEKOFF(FP, OFF, DIR, MODE) WJUMP3 (__seekoff, FP, OFF, DIR, MODE)
190
191/* The 'seekpos' hook also moves the stream position,
192 but to an absolute position given by a fpos64_t (seekpos).
193 It matches the streambuf::seekpos virtual function.
194 It is also used for the ANSI fgetpos and fsetpos functions. */
195/* The _IO_seek_cur and _IO_seek_end options are not allowed. */
196typedef off64_t (*_IO_seekpos_t) (FILE *, off64_t, int);
197#define _IO_SEEKPOS(FP, POS, FLAGS) JUMP2 (__seekpos, FP, POS, FLAGS)
198#define _IO_WSEEKPOS(FP, POS, FLAGS) WJUMP2 (__seekpos, FP, POS, FLAGS)
199
200/* The 'setbuf' hook gives a buffer to the file.
201 It matches the streambuf::setbuf virtual function. */
202typedef FILE* (*_IO_setbuf_t) (FILE *, char *, ssize_t);
203#define _IO_SETBUF(FP, BUFFER, LENGTH) JUMP2 (__setbuf, FP, BUFFER, LENGTH)
204#define _IO_WSETBUF(FP, BUFFER, LENGTH) WJUMP2 (__setbuf, FP, BUFFER, LENGTH)
205
206/* The 'sync' hook attempts to synchronize the internal data structures
207 of the file with the external state.
208 It matches the streambuf::sync virtual function. */
209typedef int (*_IO_sync_t) (FILE *);
210#define _IO_SYNC(FP) JUMP0 (__sync, FP)
211#define _IO_WSYNC(FP) WJUMP0 (__sync, FP)
212
213/* The 'doallocate' hook is used to tell the file to allocate a buffer.
214 It matches the streambuf::doallocate virtual function, which is not
215 in the ANSI/ISO C++ standard, but is part traditional implementations. */
216typedef int (*_IO_doallocate_t) (FILE *);
217#define _IO_DOALLOCATE(FP) JUMP0 (__doallocate, FP)
218#define _IO_WDOALLOCATE(FP) WJUMP0 (__doallocate, FP)
219
220/* The following four hooks (sysread, syswrite, sysclose, sysseek, and
221 sysstat) are low-level hooks specific to this implementation.
222 There is no correspondence in the ANSI/ISO C++ standard library.
223 The hooks basically correspond to the Unix system functions
224 (read, write, close, lseek, and stat) except that a FILE*
225 parameter is used instead of an integer file descriptor; the default
226 implementation used for normal files just calls those functions.
227 The advantage of overriding these functions instead of the higher-level
228 ones (underflow, overflow etc) is that you can leave all the buffering
229 higher-level functions. */
230
231/* The 'sysread' hook is used to read data from the external file into
232 an existing buffer. It generalizes the Unix read(2) function.
233 It matches the streambuf::sys_read virtual function, which is
234 specific to this implementation. */
235typedef ssize_t (*_IO_read_t) (FILE *, void *, ssize_t);
236#define _IO_SYSREAD(FP, DATA, LEN) JUMP2 (__read, FP, DATA, LEN)
237#define _IO_WSYSREAD(FP, DATA, LEN) WJUMP2 (__read, FP, DATA, LEN)
238
239/* The 'syswrite' hook is used to write data from an existing buffer
240 to an external file. It generalizes the Unix write(2) function.
241 It matches the streambuf::sys_write virtual function, which is
242 specific to this implementation. */
243typedef ssize_t (*_IO_write_t) (FILE *, const void *, ssize_t);
244#define _IO_SYSWRITE(FP, DATA, LEN) JUMP2 (__write, FP, DATA, LEN)
245#define _IO_WSYSWRITE(FP, DATA, LEN) WJUMP2 (__write, FP, DATA, LEN)
246
247/* The 'sysseek' hook is used to re-position an external file.
248 It generalizes the Unix lseek(2) function.
249 It matches the streambuf::sys_seek virtual function, which is
250 specific to this implementation. */
251typedef off64_t (*_IO_seek_t) (FILE *, off64_t, int);
252#define _IO_SYSSEEK(FP, OFFSET, MODE) JUMP2 (__seek, FP, OFFSET, MODE)
253#define _IO_WSYSSEEK(FP, OFFSET, MODE) WJUMP2 (__seek, FP, OFFSET, MODE)
254
255/* The 'sysclose' hook is used to finalize (close, finish up) an
256 external file. It generalizes the Unix close(2) function.
257 It matches the streambuf::sys_close virtual function, which is
258 specific to this implementation. */
259typedef int (*_IO_close_t) (FILE *); /* finalize */
260#define _IO_SYSCLOSE(FP) JUMP0 (__close, FP)
261#define _IO_WSYSCLOSE(FP) WJUMP0 (__close, FP)
262
263/* The 'sysstat' hook is used to get information about an external file
264 into a struct stat buffer. It generalizes the Unix fstat(2) call.
265 It matches the streambuf::sys_stat virtual function, which is
266 specific to this implementation. */
267typedef int (*_IO_stat_t) (FILE *, void *);
268#define _IO_SYSSTAT(FP, BUF) JUMP1 (__stat, FP, BUF)
269#define _IO_WSYSSTAT(FP, BUF) WJUMP1 (__stat, FP, BUF)
270
271/* The 'showmany' hook can be used to get an image how much input is
272 available. In many cases the answer will be 0 which means unknown
273 but some cases one can provide real information. */
274typedef int (*_IO_showmanyc_t) (FILE *);
275#define _IO_SHOWMANYC(FP) JUMP0 (__showmanyc, FP)
276#define _IO_WSHOWMANYC(FP) WJUMP0 (__showmanyc, FP)
277
278/* The 'imbue' hook is used to get information about the currently
279 installed locales. */
280typedef void (*_IO_imbue_t) (FILE *, void *);
281#define _IO_IMBUE(FP, LOCALE) JUMP1 (__imbue, FP, LOCALE)
282#define _IO_WIMBUE(FP, LOCALE) WJUMP1 (__imbue, FP, LOCALE)
283
284
285#define _IO_CHAR_TYPE char /* unsigned char ? */
286#define _IO_INT_TYPE int
287
288struct _IO_jump_t
289{
290 JUMP_FIELD(size_t, __dummy);
291 JUMP_FIELD(size_t, __dummy2);
292 JUMP_FIELD(_IO_finish_t, __finish);
293 JUMP_FIELD(_IO_overflow_t, __overflow);
294 JUMP_FIELD(_IO_underflow_t, __underflow);
295 JUMP_FIELD(_IO_underflow_t, __uflow);
296 JUMP_FIELD(_IO_pbackfail_t, __pbackfail);
297 /* showmany */
298 JUMP_FIELD(_IO_xsputn_t, __xsputn);
299 JUMP_FIELD(_IO_xsgetn_t, __xsgetn);
300 JUMP_FIELD(_IO_seekoff_t, __seekoff);
301 JUMP_FIELD(_IO_seekpos_t, __seekpos);
302 JUMP_FIELD(_IO_setbuf_t, __setbuf);
303 JUMP_FIELD(_IO_sync_t, __sync);
304 JUMP_FIELD(_IO_doallocate_t, __doallocate);
305 JUMP_FIELD(_IO_read_t, __read);
306 JUMP_FIELD(_IO_write_t, __write);
307 JUMP_FIELD(_IO_seek_t, __seek);
308 JUMP_FIELD(_IO_close_t, __close);
309 JUMP_FIELD(_IO_stat_t, __stat);
310 JUMP_FIELD(_IO_showmanyc_t, __showmanyc);
311 JUMP_FIELD(_IO_imbue_t, __imbue);
312};
313
314/* We always allocate an extra word following an _IO_FILE.
315 This contains a pointer to the function jump table used.
316 This is for compatibility with C++ streambuf; the word can
317 be used to smash to a pointer to a virtual function table. */
318
319struct _IO_FILE_plus
320{
321 FILE file;
322 const struct _IO_jump_t *vtable;
323};
324
325#ifdef _IO_USE_OLD_IO_FILE
326/* This structure is used by the compatibility code as if it were an
327 _IO_FILE_plus, but has enough space to initialize the _mode argument
328 of an _IO_FILE_complete. */
329struct _IO_FILE_complete_plus
330{
331 struct _IO_FILE_complete file;
332 const struct _IO_jump_t *vtable;
333};
334#endif
335
336/* Special file type for fopencookie function. */
337struct _IO_cookie_file
338{
339 struct _IO_FILE_plus __fp;
340 void *__cookie;
341 cookie_io_functions_t __io_functions;
342};
343
344FILE *_IO_fopencookie (void *cookie, const char *mode,
345 cookie_io_functions_t io_functions);
346
347
348/* Iterator type for walking global linked list of _IO_FILE objects. */
349
350typedef FILE *_IO_ITER;
351
352/* Generic functions */
353
354extern void _IO_switch_to_main_get_area (FILE *) __THROW;
355extern void _IO_switch_to_backup_area (FILE *) __THROW;
356extern int _IO_switch_to_get_mode (FILE *);
357libc_hidden_proto (_IO_switch_to_get_mode)
358extern void _IO_init_internal (FILE *, int) attribute_hidden;
359extern int _IO_sputbackc (FILE *, int) __THROW;
360libc_hidden_proto (_IO_sputbackc)
361extern int _IO_sungetc (FILE *) __THROW;
362extern void _IO_un_link (struct _IO_FILE_plus *) __THROW;
363libc_hidden_proto (_IO_un_link)
364extern void _IO_link_in (struct _IO_FILE_plus *) __THROW;
365libc_hidden_proto (_IO_link_in)
366extern void _IO_doallocbuf (FILE *) __THROW;
367libc_hidden_proto (_IO_doallocbuf)
368extern void _IO_unsave_markers (FILE *) __THROW;
369libc_hidden_proto (_IO_unsave_markers)
370extern void _IO_setb (FILE *, char *, char *, int) __THROW;
371libc_hidden_proto (_IO_setb)
372extern unsigned _IO_adjust_column (unsigned, const char *, int) __THROW;
373libc_hidden_proto (_IO_adjust_column)
374#define _IO_sputn(__fp, __s, __n) _IO_XSPUTN (__fp, __s, __n)
375
376ssize_t _IO_least_wmarker (FILE *, wchar_t *) __THROW;
377libc_hidden_proto (_IO_least_wmarker)
378extern void _IO_switch_to_main_wget_area (FILE *) __THROW;
379libc_hidden_proto (_IO_switch_to_main_wget_area)
380extern void _IO_switch_to_wbackup_area (FILE *) __THROW;
381libc_hidden_proto (_IO_switch_to_wbackup_area)
382extern int _IO_switch_to_wget_mode (FILE *);
383libc_hidden_proto (_IO_switch_to_wget_mode)
384extern void _IO_wsetb (FILE *, wchar_t *, wchar_t *, int) __THROW;
385libc_hidden_proto (_IO_wsetb)
386extern wint_t _IO_sputbackwc (FILE *, wint_t) __THROW;
387libc_hidden_proto (_IO_sputbackwc)
388extern wint_t _IO_sungetwc (FILE *) __THROW;
389extern void _IO_wdoallocbuf (FILE *) __THROW;
390libc_hidden_proto (_IO_wdoallocbuf)
391extern void _IO_unsave_wmarkers (FILE *) __THROW;
392extern unsigned _IO_adjust_wcolumn (unsigned, const wchar_t *, int) __THROW;
393extern off64_t get_file_offset (FILE *fp);
394
395/* Marker-related function. */
396
397extern void _IO_init_marker (struct _IO_marker *, FILE *);
398extern void _IO_init_wmarker (struct _IO_marker *, FILE *);
399extern void _IO_remove_marker (struct _IO_marker *) __THROW;
400extern int _IO_marker_difference (struct _IO_marker *, struct _IO_marker *)
401 __THROW;
402extern int _IO_marker_delta (struct _IO_marker *) __THROW;
403extern int _IO_wmarker_delta (struct _IO_marker *) __THROW;
404extern int _IO_seekmark (FILE *, struct _IO_marker *, int) __THROW;
405extern int _IO_seekwmark (FILE *, struct _IO_marker *, int) __THROW;
406
407/* Functions for iterating global list and dealing with its lock */
408
409extern _IO_ITER _IO_iter_begin (void) __THROW;
410libc_hidden_proto (_IO_iter_begin)
411extern _IO_ITER _IO_iter_end (void) __THROW;
412libc_hidden_proto (_IO_iter_end)
413extern _IO_ITER _IO_iter_next (_IO_ITER) __THROW;
414libc_hidden_proto (_IO_iter_next)
415extern FILE *_IO_iter_file (_IO_ITER) __THROW;
416libc_hidden_proto (_IO_iter_file)
417extern void _IO_list_lock (void) __THROW;
418libc_hidden_proto (_IO_list_lock)
419extern void _IO_list_unlock (void) __THROW;
420libc_hidden_proto (_IO_list_unlock)
421extern void _IO_list_resetlock (void) __THROW;
422libc_hidden_proto (_IO_list_resetlock)
423extern void _IO_enable_locks (void) __THROW;
424libc_hidden_proto (_IO_enable_locks)
425
426/* Default jumptable functions. */
427
428extern int _IO_default_underflow (FILE *) __THROW;
429extern int _IO_default_uflow (FILE *);
430libc_hidden_proto (_IO_default_uflow)
431extern wint_t _IO_wdefault_uflow (FILE *);
432libc_hidden_proto (_IO_wdefault_uflow)
433extern int _IO_default_doallocate (FILE *) __THROW;
434libc_hidden_proto (_IO_default_doallocate)
435extern int _IO_wdefault_doallocate (FILE *) __THROW;
436libc_hidden_proto (_IO_wdefault_doallocate)
437extern void _IO_default_finish (FILE *, int) __THROW;
438libc_hidden_proto (_IO_default_finish)
439extern void _IO_wdefault_finish (FILE *, int) __THROW;
440libc_hidden_proto (_IO_wdefault_finish)
441extern int _IO_default_pbackfail (FILE *, int) __THROW;
442libc_hidden_proto (_IO_default_pbackfail)
443extern wint_t _IO_wdefault_pbackfail (FILE *, wint_t) __THROW;
444libc_hidden_proto (_IO_wdefault_pbackfail)
445extern FILE* _IO_default_setbuf (FILE *, char *, ssize_t);
446extern size_t _IO_default_xsputn (FILE *, const void *, size_t);
447libc_hidden_proto (_IO_default_xsputn)
448extern size_t _IO_wdefault_xsputn (FILE *, const void *, size_t);
449libc_hidden_proto (_IO_wdefault_xsputn)
450extern size_t _IO_default_xsgetn (FILE *, void *, size_t);
451libc_hidden_proto (_IO_default_xsgetn)
452extern size_t _IO_wdefault_xsgetn (FILE *, void *, size_t);
453libc_hidden_proto (_IO_wdefault_xsgetn)
454extern off64_t _IO_default_seekoff (FILE *, off64_t, int, int)
455 __THROW;
456extern off64_t _IO_default_seekpos (FILE *, off64_t, int);
457extern ssize_t _IO_default_write (FILE *, const void *, ssize_t);
458extern ssize_t _IO_default_read (FILE *, void *, ssize_t);
459extern int _IO_default_stat (FILE *, void *) __THROW;
460extern off64_t _IO_default_seek (FILE *, off64_t, int) __THROW;
461extern int _IO_default_sync (FILE *) __THROW;
462#define _IO_default_close ((_IO_close_t) _IO_default_sync)
463extern int _IO_default_showmanyc (FILE *) __THROW;
464extern void _IO_default_imbue (FILE *, void *) __THROW;
465
466extern const struct _IO_jump_t _IO_file_jumps;
467libc_hidden_proto (_IO_file_jumps)
468extern const struct _IO_jump_t _IO_file_jumps_mmap attribute_hidden;
469extern const struct _IO_jump_t _IO_file_jumps_maybe_mmap attribute_hidden;
470extern const struct _IO_jump_t _IO_wfile_jumps;
471libc_hidden_proto (_IO_wfile_jumps)
472extern const struct _IO_jump_t _IO_wfile_jumps_mmap attribute_hidden;
473extern const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap attribute_hidden;
474extern const struct _IO_jump_t _IO_old_file_jumps attribute_hidden;
475extern const struct _IO_jump_t _IO_streambuf_jumps;
476extern const struct _IO_jump_t _IO_old_proc_jumps attribute_hidden;
477extern const struct _IO_jump_t _IO_str_jumps attribute_hidden;
478extern const struct _IO_jump_t _IO_wstr_jumps attribute_hidden;
479extern const struct _IO_codecvt __libio_codecvt attribute_hidden;
480extern int _IO_do_write (FILE *, const char *, size_t);
481libc_hidden_proto (_IO_do_write)
482extern int _IO_new_do_write (FILE *, const char *, size_t);
483extern int _IO_old_do_write (FILE *, const char *, size_t);
484extern int _IO_wdo_write (FILE *, const wchar_t *, size_t);
485libc_hidden_proto (_IO_wdo_write)
486extern int _IO_flush_all_lockp (int);
487extern int _IO_flush_all (void);
488libc_hidden_proto (_IO_flush_all)
489extern int _IO_cleanup (void);
490extern void _IO_flush_all_linebuffered (void);
491libc_hidden_proto (_IO_flush_all_linebuffered)
492extern int _IO_new_fgetpos (FILE *, __fpos_t *);
493extern int _IO_old_fgetpos (FILE *, __fpos_t *);
494extern int _IO_new_fsetpos (FILE *, const __fpos_t *);
495extern int _IO_old_fsetpos (FILE *, const __fpos_t *);
496extern int _IO_new_fgetpos64 (FILE *, __fpos64_t *);
497extern int _IO_old_fgetpos64 (FILE *, __fpos64_t *);
498extern int _IO_new_fsetpos64 (FILE *, const __fpos64_t *);
499extern int _IO_old_fsetpos64 (FILE *, const __fpos64_t *);
500extern void _IO_old_init (FILE *fp, int flags) __THROW;
501
502
503#define _IO_do_flush(_f) \
504 ((_f)->_mode <= 0 \
505 ? _IO_do_write(_f, (_f)->_IO_write_base, \
506 (_f)->_IO_write_ptr-(_f)->_IO_write_base) \
507 : _IO_wdo_write(_f, (_f)->_wide_data->_IO_write_base, \
508 ((_f)->_wide_data->_IO_write_ptr \
509 - (_f)->_wide_data->_IO_write_base)))
510#define _IO_old_do_flush(_f) \
511 _IO_old_do_write(_f, (_f)->_IO_write_base, \
512 (_f)->_IO_write_ptr-(_f)->_IO_write_base)
513#define _IO_in_put_mode(_fp) ((_fp)->_flags & _IO_CURRENTLY_PUTTING)
514#define _IO_mask_flags(fp, f, mask) \
515 ((fp)->_flags = ((fp)->_flags & ~(mask)) | ((f) & (mask)))
516#define _IO_setg(fp, eb, g, eg) ((fp)->_IO_read_base = (eb),\
517 (fp)->_IO_read_ptr = (g), (fp)->_IO_read_end = (eg))
518#define _IO_wsetg(fp, eb, g, eg) ((fp)->_wide_data->_IO_read_base = (eb),\
519 (fp)->_wide_data->_IO_read_ptr = (g), \
520 (fp)->_wide_data->_IO_read_end = (eg))
521#define _IO_setp(__fp, __p, __ep) \
522 ((__fp)->_IO_write_base = (__fp)->_IO_write_ptr \
523 = __p, (__fp)->_IO_write_end = (__ep))
524#define _IO_wsetp(__fp, __p, __ep) \
525 ((__fp)->_wide_data->_IO_write_base \
526 = (__fp)->_wide_data->_IO_write_ptr = __p, \
527 (__fp)->_wide_data->_IO_write_end = (__ep))
528#define _IO_have_backup(fp) ((fp)->_IO_save_base != NULL)
529#define _IO_have_wbackup(fp) ((fp)->_wide_data->_IO_save_base != NULL)
530#define _IO_in_backup(fp) ((fp)->_flags & _IO_IN_BACKUP)
531#define _IO_have_markers(fp) ((fp)->_markers != NULL)
532#define _IO_blen(fp) ((fp)->_IO_buf_end - (fp)->_IO_buf_base)
533#define _IO_wblen(fp) ((fp)->_wide_data->_IO_buf_end \
534 - (fp)->_wide_data->_IO_buf_base)
535
536/* Jumptable functions for files. */
537
538extern int _IO_file_doallocate (FILE *) __THROW;
539libc_hidden_proto (_IO_file_doallocate)
540extern FILE* _IO_file_setbuf (FILE *, char *, ssize_t);
541libc_hidden_proto (_IO_file_setbuf)
542extern off64_t _IO_file_seekoff (FILE *, off64_t, int, int);
543libc_hidden_proto (_IO_file_seekoff)
544extern off64_t _IO_file_seekoff_mmap (FILE *, off64_t, int, int)
545 __THROW;
546extern size_t _IO_file_xsputn (FILE *, const void *, size_t);
547libc_hidden_proto (_IO_file_xsputn)
548extern size_t _IO_file_xsgetn (FILE *, void *, size_t);
549libc_hidden_proto (_IO_file_xsgetn)
550extern int _IO_file_stat (FILE *, void *) __THROW;
551libc_hidden_proto (_IO_file_stat)
552extern int _IO_file_close (FILE *) __THROW;
553libc_hidden_proto (_IO_file_close)
554extern int _IO_file_close_mmap (FILE *) __THROW;
555extern int _IO_file_underflow (FILE *);
556libc_hidden_proto (_IO_file_underflow)
557extern int _IO_file_underflow_mmap (FILE *);
558extern int _IO_file_underflow_maybe_mmap (FILE *);
559extern int _IO_file_overflow (FILE *, int);
560libc_hidden_proto (_IO_file_overflow)
561#define _IO_file_is_open(__fp) ((__fp)->_fileno != -1)
562extern FILE* _IO_file_attach (FILE *, int);
563libc_hidden_proto (_IO_file_attach)
564extern FILE* _IO_file_open (FILE *, const char *, int, int, int, int);
565libc_hidden_proto (_IO_file_open)
566extern FILE* _IO_file_fopen (FILE *, const char *, const char *, int);
567libc_hidden_proto (_IO_file_fopen)
568extern ssize_t _IO_file_write (FILE *, const void *, ssize_t);
569extern ssize_t _IO_file_read (FILE *, void *, ssize_t);
570libc_hidden_proto (_IO_file_read)
571extern int _IO_file_sync (FILE *);
572libc_hidden_proto (_IO_file_sync)
573extern int _IO_file_close_it (FILE *);
574libc_hidden_proto (_IO_file_close_it)
575extern off64_t _IO_file_seek (FILE *, off64_t, int) __THROW;
576libc_hidden_proto (_IO_file_seek)
577extern void _IO_file_finish (FILE *, int);
578libc_hidden_proto (_IO_file_finish)
579
580extern FILE* _IO_new_file_attach (FILE *, int);
581extern int _IO_new_file_close_it (FILE *);
582extern void _IO_new_file_finish (FILE *, int);
583extern FILE* _IO_new_file_fopen (FILE *, const char *, const char *,
584 int);
585extern void _IO_no_init (FILE *, int, int, struct _IO_wide_data *,
586 const struct _IO_jump_t *) __THROW;
587extern void _IO_new_file_init_internal (struct _IO_FILE_plus *)
588 __THROW attribute_hidden;
589extern FILE* _IO_new_file_setbuf (FILE *, char *, ssize_t);
590extern FILE* _IO_file_setbuf_mmap (FILE *, char *, ssize_t);
591extern int _IO_new_file_sync (FILE *);
592extern int _IO_new_file_underflow (FILE *);
593extern int _IO_new_file_overflow (FILE *, int);
594extern off64_t _IO_new_file_seekoff (FILE *, off64_t, int, int);
595extern ssize_t _IO_new_file_write (FILE *, const void *, ssize_t);
596extern size_t _IO_new_file_xsputn (FILE *, const void *, size_t);
597
598extern FILE* _IO_old_file_setbuf (FILE *, char *, ssize_t);
599extern off64_t _IO_old_file_seekoff (FILE *, off64_t, int, int);
600extern size_t _IO_old_file_xsputn (FILE *, const void *, size_t);
601extern int _IO_old_file_underflow (FILE *);
602extern int _IO_old_file_overflow (FILE *, int);
603extern void _IO_old_file_init_internal (struct _IO_FILE_plus *)
604 __THROW attribute_hidden;
605extern FILE* _IO_old_file_attach (FILE *, int);
606extern FILE* _IO_old_file_fopen (FILE *, const char *, const char *);
607extern ssize_t _IO_old_file_write (FILE *, const void *, ssize_t);
608extern int _IO_old_file_sync (FILE *);
609extern int _IO_old_file_close_it (FILE *);
610extern void _IO_old_file_finish (FILE *, int);
611
612extern int _IO_wfile_doallocate (FILE *) __THROW;
613extern size_t _IO_wfile_xsputn (FILE *, const void *, size_t);
614libc_hidden_proto (_IO_wfile_xsputn)
615extern FILE* _IO_wfile_setbuf (FILE *, wchar_t *, ssize_t);
616extern wint_t _IO_wfile_sync (FILE *);
617libc_hidden_proto (_IO_wfile_sync)
618extern wint_t _IO_wfile_underflow (FILE *);
619libc_hidden_proto (_IO_wfile_underflow)
620extern wint_t _IO_wfile_overflow (FILE *, wint_t);
621libc_hidden_proto (_IO_wfile_overflow)
622extern off64_t _IO_wfile_seekoff (FILE *, off64_t, int, int);
623libc_hidden_proto (_IO_wfile_seekoff)
624
625/* Jumptable functions for proc_files. */
626extern FILE* _IO_proc_open (FILE *, const char *, const char *)
627 __THROW;
628extern FILE* _IO_new_proc_open (FILE *, const char *, const char *)
629 __THROW;
630extern FILE* _IO_old_proc_open (FILE *, const char *, const char *);
631extern int _IO_proc_close (FILE *) __THROW;
632extern int _IO_new_proc_close (FILE *) __THROW;
633extern int _IO_old_proc_close (FILE *);
634
635/* Jumptable functions for strfiles. */
636extern int _IO_str_underflow (FILE *) __THROW;
637libc_hidden_proto (_IO_str_underflow)
638extern int _IO_str_overflow (FILE *, int) __THROW;
639libc_hidden_proto (_IO_str_overflow)
640extern int _IO_str_pbackfail (FILE *, int) __THROW;
641libc_hidden_proto (_IO_str_pbackfail)
642extern off64_t _IO_str_seekoff (FILE *, off64_t, int, int) __THROW;
643libc_hidden_proto (_IO_str_seekoff)
644extern void _IO_str_finish (FILE *, int) __THROW;
645
646/* Other strfile functions */
647struct _IO_strfile_;
648extern ssize_t _IO_str_count (FILE *) __THROW;
649
650/* And the wide character versions. */
651extern void _IO_wstr_init_static (FILE *, wchar_t *, size_t, wchar_t *)
652 __THROW;
653extern ssize_t _IO_wstr_count (FILE *) __THROW;
654extern wint_t _IO_wstr_overflow (FILE *, wint_t) __THROW;
655extern wint_t _IO_wstr_underflow (FILE *) __THROW;
656extern off64_t _IO_wstr_seekoff (FILE *, off64_t, int, int)
657 __THROW;
658extern wint_t _IO_wstr_pbackfail (FILE *, wint_t) __THROW;
659extern void _IO_wstr_finish (FILE *, int) __THROW;
660
661extern int _IO_vasprintf (char **result_ptr, const char *format,
662 va_list args) __THROW;
663extern int _IO_vdprintf (int d, const char *format, va_list arg);
664extern int _IO_vsnprintf (char *string, size_t maxlen,
665 const char *format, va_list args) __THROW;
666
667
668extern size_t _IO_getline (FILE *,char *, size_t, int, int);
669libc_hidden_proto (_IO_getline)
670extern size_t _IO_getline_info (FILE *,char *, size_t,
671 int, int, int *);
672libc_hidden_proto (_IO_getline_info)
673extern ssize_t _IO_getdelim (char **, size_t *, int, FILE *);
674extern size_t _IO_getwline (FILE *,wchar_t *, size_t, wint_t, int);
675extern size_t _IO_getwline_info (FILE *,wchar_t *, size_t,
676 wint_t, int, wint_t *);
677
678extern struct _IO_FILE_plus *_IO_list_all;
679libc_hidden_proto (_IO_list_all)
680extern void (*_IO_cleanup_registration_needed) (void);
681
682extern void _IO_str_init_static_internal (struct _IO_strfile_ *, char *,
683 size_t, char *) __THROW;
684extern off64_t _IO_seekoff_unlocked (FILE *, off64_t, int, int)
685 attribute_hidden;
686extern off64_t _IO_seekpos_unlocked (FILE *, off64_t, int)
687 attribute_hidden;
688
689#if _G_HAVE_MMAP
690
691# include <unistd.h>
692# include <fcntl.h>
693# include <sys/mman.h>
694# include <sys/param.h>
695
696# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
697# define MAP_ANONYMOUS MAP_ANON
698# endif
699
700# if !defined(MAP_ANONYMOUS) || !defined(EXEC_PAGESIZE)
701# undef _G_HAVE_MMAP
702# define _G_HAVE_MMAP 0
703# endif
704
705#endif /* _G_HAVE_MMAP */
706
707extern int _IO_vscanf (const char *, va_list) __THROW;
708
709#ifdef _IO_MTSAFE_IO
710/* check following! */
711# ifdef _IO_USE_OLD_IO_FILE
712# define FILEBUF_LITERAL(CHAIN, FLAGS, FD, WDP) \
713 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
714 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (FILE *) CHAIN, FD, \
715 0, _IO_pos_BAD, 0, 0, { 0 }, &_IO_stdfile_##FD##_lock }
716# else
717# define FILEBUF_LITERAL(CHAIN, FLAGS, FD, WDP) \
718 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
719 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (FILE *) CHAIN, FD, \
720 0, _IO_pos_BAD, 0, 0, { 0 }, &_IO_stdfile_##FD##_lock, _IO_pos_BAD,\
721 NULL, WDP, 0 }
722# endif
723#else
724# ifdef _IO_USE_OLD_IO_FILE
725# define FILEBUF_LITERAL(CHAIN, FLAGS, FD, WDP) \
726 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
727 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (FILE *) CHAIN, FD, \
728 0, _IO_pos_BAD }
729# else
730# define FILEBUF_LITERAL(CHAIN, FLAGS, FD, WDP) \
731 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
732 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (FILE *) CHAIN, FD, \
733 0, _IO_pos_BAD, 0, 0, { 0 }, 0, _IO_pos_BAD, \
734 NULL, WDP, 0 }
735# endif
736#endif
737
738extern struct _IO_fake_stdiobuf _IO_stdin_buf, _IO_stdout_buf, _IO_stderr_buf;
739
740#ifdef IO_DEBUG
741# define CHECK_FILE(FILE, RET) do { \
742 if ((FILE) == NULL || \
743 ((FILE)->_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
744 { \
745 __set_errno (EINVAL); \
746 return RET; \
747 } \
748 } while (0)
749#else
750# define CHECK_FILE(FILE, RET) do { } while (0)
751#endif
752
753static inline void
754__attribute__ ((__always_inline__))
755_IO_acquire_lock_fct (FILE **p)
756{
757 FILE *fp = *p;
758 if ((fp->_flags & _IO_USER_LOCK) == 0)
759 _IO_funlockfile (fp);
760}
761
762static inline void
763__attribute__ ((__always_inline__))
764_IO_acquire_lock_clear_flags2_fct (FILE **p)
765{
766 FILE *fp = *p;
767 fp->_flags2 &= ~(_IO_FLAGS2_FORTIFY | _IO_FLAGS2_SCANF_STD);
768 if ((fp->_flags & _IO_USER_LOCK) == 0)
769 _IO_funlockfile (fp);
770}
771
772#if !defined _IO_MTSAFE_IO && IS_IN (libc)
773# define _IO_acquire_lock(_fp) \
774 do { \
775 FILE *_IO_acquire_lock_file = NULL
776# define _IO_acquire_lock_clear_flags2(_fp) \
777 do { \
778 FILE *_IO_acquire_lock_file = (_fp)
779# define _IO_release_lock(_fp) \
780 if (_IO_acquire_lock_file != NULL) \
781 _IO_acquire_lock_file->_flags2 &= ~(_IO_FLAGS2_FORTIFY \
782 | _IO_FLAGS2_SCANF_STD); \
783 } while (0)
784#endif
785
786/* Collect all vtables in a special section for vtable verification.
787 These symbols cover the extent of this section. */
788symbol_set_declare (__libc_IO_vtables)
789
790/* libio vtables need to carry this attribute so that they pass
791 validation. */
792#define libio_vtable __attribute__ ((section ("__libc_IO_vtables")))
793
794#ifdef SHARED
795/* If equal to &_IO_vtable_check (with pointer guard protection),
796 unknown vtable pointers are valid. This function pointer is solely
797 used as a flag. */
798extern void (*IO_accept_foreign_vtables) (void) attribute_hidden;
799
800/* Assigns the passed function pointer (either NULL or
801 &_IO_vtable_check) to IO_accept_foreign_vtables. */
802static inline void
803IO_set_accept_foreign_vtables (void (*flag) (void))
804{
805#ifdef PTR_MANGLE
806 PTR_MANGLE (flag);
807#endif
808 atomic_store_relaxed (&IO_accept_foreign_vtables, flag);
809}
810
811#else /* !SHARED */
812
813/* The statically-linked version does nothing. */
814static inline void
815IO_set_accept_foreign_vtables (void (*flag) (void))
816{
817}
818
819#endif
820
821/* Check if unknown vtable pointers are permitted; otherwise,
822 terminate the process. */
823void _IO_vtable_check (void) attribute_hidden;
824
825/* Perform vtable pointer validation. If validation fails, terminate
826 the process. */
827static inline const struct _IO_jump_t *
828IO_validate_vtable (const struct _IO_jump_t *vtable)
829{
830 /* Fast path: The vtable pointer is within the __libc_IO_vtables
831 section. */
832 uintptr_t section_length = __stop___libc_IO_vtables - __start___libc_IO_vtables;
833 uintptr_t ptr = (uintptr_t) vtable;
834 uintptr_t offset = ptr - (uintptr_t) __start___libc_IO_vtables;
835 if (__glibc_unlikely (offset >= section_length))
836 /* The vtable pointer is not in the expected section. Use the
837 slow path, which will terminate the process if necessary. */
838 _IO_vtable_check ();
839 return vtable;
840}
841
842#endif /* libioP.h. */
843