1/* Copyright (C) 1993, 1997 Free Software Foundation, Inc.
2   This file is part of the GNU IO Library.
3
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU General Public License as
6   published by the Free Software Foundation; either version 2, or (at
7   your option) any later version.
8
9   This library is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this library; see the file COPYING.  If not, write to
16   the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17   MA 02111-1307, USA.
18
19   As a special exception, if you link this library with files
20   compiled with a GNU compiler to produce an executable, this does
21   not cause the resulting executable to be covered by the GNU General
22   Public License.  This exception does not however invalidate any
23   other reasons why the executable file might be covered by the GNU
24   General Public License.  */
25
26#ifndef _POSIX_SOURCE
27# define _POSIX_SOURCE
28#endif
29
30#include <errno.h>
31#ifndef __set_errno
32# define __set_errno(Val) errno = (Val)
33#endif
34
35#ifdef _IO_MTSAFE_IO
36# if defined __GLIBC__ && (__GLIBC__ >= 2 || __GLIBC__ <= -2)
37#  if __GLIBC_MINOR__ > 0
38#   include <bits/libc-lock.h>
39#  else
40#   include <libc-lock.h>
41#  endif
42# else
43/*# include <comthread.h>*/
44# endif
45#endif
46
47#include "iolibio.h"
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53#define _IO_seek_set 0
54#define _IO_seek_cur 1
55#define _IO_seek_end 2
56
57/* THE JUMPTABLE FUNCTIONS.
58
59 * The _IO_FILE type is used to implement the FILE type in GNU libc,
60 * as well as the streambuf class in GNU iostreams for C++.
61 * These are all the same, just used differently.
62 * An _IO_FILE (or FILE) object is allows followed by a pointer to
63 * a jump table (of pointers to functions).  The pointer is accessed
64 * with the _IO_JUMPS macro.  The jump table has a eccentric format,
65 * so as to be compatible with the layout of a C++ virtual function table.
66 * (as implemented by g++).  When a pointer to a streambuf object is
67 * coerced to an (_IO_FILE*), then _IO_JUMPS on the result just
68 * happens to point to the virtual function table of the streambuf.
69 * Thus the _IO_JUMPS function table used for C stdio/libio does
70 * double duty as the virtual function table for C++ streambuf.
71 *
72 * The entries in the _IO_JUMPS function table (and hence also the
73 * virtual functions of a streambuf) are described below.
74 * The first parameter of each function entry is the _IO_FILE/streambuf
75 * object being acted on (i.e. the 'this' parameter).
76 */
77
78#define _IO_JUMPS(THIS) ((struct _IO_FILE_plus *) (THIS))->vtable
79#ifdef _G_USING_THUNKS
80# define JUMP_FIELD(TYPE, NAME) TYPE NAME
81# define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC (THIS)
82# define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC (THIS, X1)
83# define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC (THIS, X1, X2)
84# define JUMP3(FUNC, THIS, X1,X2,X3) _IO_JUMPS(THIS)->FUNC (THIS, X1,X2, X3)
85# define JUMP_INIT(NAME, VALUE) VALUE
86# define JUMP_INIT_DUMMY JUMP_INIT(dummy, 0), JUMP_INIT (dummy2, 0)
87#else
88/* These macros will change when we re-implement vtables to use "thunks"! */
89# define JUMP_FIELD(TYPE, NAME) struct { short delta1, delta2; TYPE pfn; } NAME
90# define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC.pfn (THIS)
91# define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC.pfn (THIS, X1)
92# define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC.pfn (THIS, X1, X2)
93# define JUMP3(FUNC, THIS, X1,X2,X3) _IO_JUMPS(THIS)->FUNC.pfn (THIS, X1,X2,X3)
94# define JUMP_INIT(NAME, VALUE) {0, 0, VALUE}
95# define JUMP_INIT_DUMMY JUMP_INIT(dummy, 0)
96#endif
97
98/* The 'finish' function does any final cleaning up of an _IO_FILE object.
99   It does not delete (free) it, but does everything else to finalize it/
100   It matches the streambuf::~streambuf virtual destructor.  */
101typedef void (*_IO_finish_t) __PMT ((_IO_FILE *, int)); /* finalize */
102#define _IO_FINISH(FP) JUMP1 (__finish, FP, 0)
103
104/* The 'overflow' hook flushes the buffer.
105   The second argument is a character, or EOF.
106   It matches the streambuf::overflow virtual function. */
107typedef int (*_IO_overflow_t) __PMT ((_IO_FILE *, int));
108#define _IO_OVERFLOW(FP, CH) JUMP1 (__overflow, FP, CH)
109
110/* The 'underflow' hook tries to fills the get buffer.
111   It returns the next character (as an unsigned char) or EOF.  The next
112   character remains in the get buffer, and the get position is not changed.
113   It matches the streambuf::underflow virtual function. */
114typedef int (*_IO_underflow_t) __PMT ((_IO_FILE *));
115#define _IO_UNDERFLOW(FP) JUMP0 (__underflow, FP)
116
117/* The 'uflow' hook returns the next character in the input stream
118   (cast to unsigned char), and increments the read position;
119   EOF is returned on failure.
120   It matches the streambuf::uflow virtual function, which is not in the
121   cfront implementation, but was added to C++ by the ANSI/ISO committee. */
122#define _IO_UFLOW(FP) JUMP0 (__uflow, FP)
123
124/* The 'pbackfail' hook handles backing up.
125   It matches the streambuf::pbackfail virtual function. */
126typedef int (*_IO_pbackfail_t) __PMT ((_IO_FILE *, int));
127#define _IO_PBACKFAIL(FP, CH) JUMP1 (__pbackfail, FP, CH)
128
129/* The 'xsputn' hook writes upto N characters from buffer DATA.
130   Returns the number of character actually written.
131   It matches the streambuf::xsputn virtual function. */
132typedef _IO_size_t (*_IO_xsputn_t) __PMT ((_IO_FILE *FP, const void *DATA,
133					 _IO_size_t N));
134#define _IO_XSPUTN(FP, DATA, N) JUMP2 (__xsputn, FP, DATA, N)
135
136/* The 'xsgetn' hook reads upto N characters into buffer DATA.
137   Returns the number of character actually read.
138   It matches the streambuf::xsgetn virtual function. */
139typedef _IO_size_t (*_IO_xsgetn_t) __PMT ((_IO_FILE *FP, void *DATA,
140					 _IO_size_t N));
141#define _IO_XSGETN(FP, DATA, N) JUMP2 (__xsgetn, FP, DATA, N)
142
143/* The 'seekoff' hook moves the stream position to a new position
144   relative to the start of the file (if DIR==0), the current position
145   (MODE==1), or the end of the file (MODE==2).
146   It matches the streambuf::seekoff virtual function.
147   It is also used for the ANSI fseek function. */
148#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
149typedef _IO_off64_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off64_t OFF,
150					  int DIR, int MODE));
151#else
152typedef _IO_off_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off_t OFF,
153					  int DIR, int MODE));
154#endif
155#define _IO_SEEKOFF(FP, OFF, DIR, MODE) JUMP3 (__seekoff, FP, OFF, DIR, MODE)
156
157/* The 'seekpos' hook also moves the stream position,
158   but to an absolute position given by a fpos_t (seekpos).
159   It matches the streambuf::seekpos virtual function.
160   It is also used for the ANSI fgetpos and fsetpos functions.  */
161/* The _IO_seek_cur and _IO_seek_end options are not allowed. */
162#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
163typedef _IO_off64_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_off64_t, int));
164#else
165typedef _IO_off_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_off_t, int));
166#endif
167#define _IO_SEEKPOS(FP, POS, FLAGS) JUMP2 (__seekpos, FP, POS, FLAGS)
168
169/* The 'setbuf' hook gives a buffer to the file.
170   It matches the streambuf::setbuf virtual function. */
171typedef _IO_FILE* (*_IO_setbuf_t) __PMT ((_IO_FILE *, char *, _IO_ssize_t));
172#define _IO_SETBUF(FP, BUFFER, LENGTH) JUMP2 (__setbuf, FP, BUFFER, LENGTH)
173
174/* The 'sync' hook attempts to synchronize the internal data structures
175   of the file with the external state.
176   It matches the streambuf::sync virtual function. */
177typedef int (*_IO_sync_t) __PMT ((_IO_FILE *));
178#define _IO_SYNC(FP) JUMP0 (__sync, FP)
179
180/* The 'doallocate' hook is used to tell the file to allocate a buffer.
181   It matches the streambuf::doallocate virtual function, which is not
182   in the ANSI/ISO C++ standard, but is part traditional implementations. */
183typedef int (*_IO_doallocate_t) __PMT ((_IO_FILE *));
184#define _IO_DOALLOCATE(FP) JUMP0 (__doallocate, FP)
185
186/* The following four hooks (sysread, syswrite, sysclose, sysseek, and
187   sysstat) are low-level hooks specific to this implementation.
188   There is no correspondence in the ANSI/ISO C++ standard library.
189   The hooks basically correspond to the Unix system functions
190   (read, write, close, lseek, and stat) except that a _IO_FILE*
191   parameter is used instead of a integer file descriptor;  the default
192   implementation used for normal files just calls those functions.
193   The advantage of overriding these functions instead of the higher-level
194   ones (underflow, overflow etc) is that you can leave all the buffering
195   higher-level functions.  */
196
197/* The 'sysread' hook is used to read data from the external file into
198   an existing buffer.  It generalizes the Unix read(2) function.
199   It matches the streambuf::sys_read virtual function, which is
200   specific to this implementation. */
201typedef _IO_ssize_t (*_IO_read_t) __PMT ((_IO_FILE *, void *, _IO_ssize_t));
202#define _IO_SYSREAD(FP, DATA, LEN) JUMP2 (__read, FP, DATA, LEN)
203
204/* The 'syswrite' hook is used to write data from an existing buffer
205   to an external file.  It generalizes the Unix write(2) function.
206   It matches the streambuf::sys_write virtual function, which is
207   specific to this implementation. */
208typedef _IO_ssize_t (*_IO_write_t) __PMT ((_IO_FILE *,const void *,_IO_ssize_t));
209#define _IO_SYSWRITE(FP, DATA, LEN) JUMP2 (__write, FP, DATA, LEN)
210
211/* The 'sysseek' hook is used to re-position an external file.
212   It generalizes the Unix lseek(2) function.
213   It matches the streambuf::sys_seek virtual function, which is
214   specific to this implementation. */
215#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
216typedef _IO_off64_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off64_t, int));
217#else
218typedef _IO_off_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off_t, int));
219#endif
220#define _IO_SYSSEEK(FP, OFFSET, MODE) JUMP2 (__seek, FP, OFFSET, MODE)
221
222/* The 'sysclose' hook is used to finalize (close, finish up) an
223   external file.  It generalizes the Unix close(2) function.
224   It matches the streambuf::sys_close virtual function, which is
225   specific to this implementation. */
226typedef int (*_IO_close_t) __PMT ((_IO_FILE *)); /* finalize */
227#define _IO_SYSCLOSE(FP) JUMP0 (__close, FP)
228
229/* The 'sysstat' hook is used to get information about an external file
230   into a struct stat buffer.  It generalizes the Unix fstat(2) call.
231   It matches the streambuf::sys_stat virtual function, which is
232   specific to this implementation. */
233typedef int (*_IO_stat_t) __PMT ((_IO_FILE *, void *));
234#define _IO_SYSSTAT(FP, BUF) JUMP1 (__stat, FP, BUF)
235
236#if _G_IO_IO_FILE_VERSION == 0x20001
237/* The 'showmany' hook can be used to get an image how much input is
238   available.  In many cases the answer will be 0 which means unknown
239   but some cases one can provide real information.  */
240typedef int (*_IO_showmanyc_t) __PMT ((_IO_FILE *));
241#define _IO_SHOWMANYC(FP) JUMP0 (__showmanyc, FP)
242
243/* The 'imbue' hook is used to get information about the currently
244   installed locales.  */
245typedef void (*_IO_imbue_t) __PMT ((_IO_FILE *, void *));
246#define _IO_IMBUE(FP, LOCALE) JUMP1 (__imbue, FP, LOCALE)
247#endif
248
249
250#define _IO_CHAR_TYPE char /* unsigned char ? */
251#define _IO_INT_TYPE int
252
253struct _IO_jump_t
254{
255    JUMP_FIELD(_G_size_t, __dummy);
256#ifdef _G_USING_THUNKS
257    JUMP_FIELD(_G_size_t, __dummy2);
258#endif
259    JUMP_FIELD(_IO_finish_t, __finish);
260    JUMP_FIELD(_IO_overflow_t, __overflow);
261    JUMP_FIELD(_IO_underflow_t, __underflow);
262    JUMP_FIELD(_IO_underflow_t, __uflow);
263    JUMP_FIELD(_IO_pbackfail_t, __pbackfail);
264    /* showmany */
265    JUMP_FIELD(_IO_xsputn_t, __xsputn);
266    JUMP_FIELD(_IO_xsgetn_t, __xsgetn);
267    JUMP_FIELD(_IO_seekoff_t, __seekoff);
268    JUMP_FIELD(_IO_seekpos_t, __seekpos);
269    JUMP_FIELD(_IO_setbuf_t, __setbuf);
270    JUMP_FIELD(_IO_sync_t, __sync);
271    JUMP_FIELD(_IO_doallocate_t, __doallocate);
272    JUMP_FIELD(_IO_read_t, __read);
273    JUMP_FIELD(_IO_write_t, __write);
274    JUMP_FIELD(_IO_seek_t, __seek);
275    JUMP_FIELD(_IO_close_t, __close);
276    JUMP_FIELD(_IO_stat_t, __stat);
277#if _G_IO_IO_FILE_VERSION == 0x20001
278    JUMP_FIELD(_IO_showmanyc_t, __showmanyc);
279    JUMP_FIELD(_IO_imbue_t, __imbue);
280#endif
281#if 0
282    get_column;
283    set_column;
284#endif
285};
286
287/* We always allocate an extra word following an _IO_FILE.
288   This contains a pointer to the function jump table used.
289   This is for compatibility with C++ streambuf; the word can
290   be used to smash to a pointer to a virtual function table. */
291
292struct _IO_FILE_plus
293{
294  _IO_FILE file;
295  const struct _IO_jump_t *vtable;
296};
297
298/* Generic functions */
299
300#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
301extern _IO_off64_t _IO_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
302extern _IO_off64_t _IO_seekpos __P ((_IO_FILE *, _IO_off64_t, int));
303#else
304extern _IO_off_t _IO_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
305extern _IO_off_t _IO_seekpos __P ((_IO_FILE *, _IO_off_t, int));
306#endif
307
308extern void _IO_switch_to_main_get_area __P ((_IO_FILE *));
309extern void _IO_switch_to_backup_area __P ((_IO_FILE *));
310extern int _IO_switch_to_get_mode __P ((_IO_FILE *));
311extern void _IO_init __P ((_IO_FILE *, int));
312extern int _IO_sputbackc __P ((_IO_FILE *, int));
313extern int _IO_sungetc __P ((_IO_FILE *));
314extern void _IO_un_link __P ((_IO_FILE *));
315extern void _IO_link_in __P ((_IO_FILE *));
316extern void _IO_doallocbuf __P ((_IO_FILE *));
317extern void _IO_unsave_markers __P ((_IO_FILE *));
318extern void _IO_setb __P ((_IO_FILE *, char *, char *, int));
319extern unsigned _IO_adjust_column __P ((unsigned, const char *, int));
320#define _IO_sputn(__fp, __s, __n) _IO_XSPUTN (__fp, __s, __n)
321
322/* Marker-related function. */
323
324extern void _IO_init_marker __P ((struct _IO_marker *, _IO_FILE *));
325extern void _IO_remove_marker __P ((struct _IO_marker *));
326extern int _IO_marker_difference __P ((struct _IO_marker *,
327				       struct _IO_marker *));
328extern int _IO_marker_delta __P ((struct _IO_marker *));
329extern int _IO_seekmark __P ((_IO_FILE *, struct _IO_marker *, int));
330
331/* Default jumptable functions. */
332
333extern int _IO_default_underflow __P ((_IO_FILE *));
334extern int _IO_default_uflow __P ((_IO_FILE *));
335extern int _IO_default_doallocate __P ((_IO_FILE *));
336extern void _IO_default_finish __P ((_IO_FILE *, int));
337extern int _IO_default_pbackfail __P ((_IO_FILE *, int));
338extern _IO_FILE* _IO_default_setbuf __P ((_IO_FILE *, char *, _IO_ssize_t));
339extern _IO_size_t _IO_default_xsputn __P ((_IO_FILE *, const void *,
340					   _IO_size_t));
341extern _IO_size_t _IO_default_xsgetn __P ((_IO_FILE *, void *, _IO_size_t));
342#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
343extern _IO_off64_t _IO_default_seekoff __P ((_IO_FILE *,
344					      _IO_off64_t, int, int));
345extern _IO_off64_t _IO_default_seekpos __P ((_IO_FILE *,
346					      _IO_off64_t, int));
347#else
348extern _IO_off_t _IO_default_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
349extern _IO_off_t _IO_default_seekpos __P ((_IO_FILE *, _IO_off_t, int));
350#endif
351extern _IO_ssize_t _IO_default_write __P ((_IO_FILE *, const void *,
352					   _IO_ssize_t));
353extern _IO_ssize_t _IO_default_read __P ((_IO_FILE *, void *, _IO_ssize_t));
354extern int _IO_default_stat __P ((_IO_FILE *, void *));
355#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
356extern _IO_off64_t _IO_default_seek __P ((_IO_FILE *, _IO_off64_t, int));
357#else
358extern _IO_off_t _IO_default_seek __P ((_IO_FILE *, _IO_off_t, int));
359#endif
360extern int _IO_default_sync __P ((_IO_FILE *));
361#define _IO_default_close ((_IO_close_t) _IO_default_sync)
362
363extern struct _IO_jump_t _IO_file_jumps;
364extern struct _IO_jump_t _IO_streambuf_jumps;
365extern struct _IO_jump_t _IO_proc_jumps;
366extern struct _IO_jump_t _IO_str_jumps;
367extern int _IO_do_write __P ((_IO_FILE *, const char *, _IO_size_t));
368extern int _IO_flush_all __P ((void));
369extern void _IO_cleanup __P ((void));
370extern void _IO_flush_all_linebuffered __P ((void));
371
372#define _IO_do_flush(_f) \
373  _IO_do_write(_f, (_f)->_IO_write_base, \
374	       (_f)->_IO_write_ptr-(_f)->_IO_write_base)
375#define _IO_in_put_mode(_fp) ((_fp)->_flags & _IO_CURRENTLY_PUTTING)
376#define _IO_mask_flags(fp, f, mask) \
377       ((fp)->_flags = ((fp)->_flags & ~(mask)) | ((f) & (mask)))
378#define _IO_setg(fp, eb, g, eg)  ((fp)->_IO_read_base = (eb),\
379	(fp)->_IO_read_ptr = (g), (fp)->_IO_read_end = (eg))
380#define _IO_setp(__fp, __p, __ep) \
381       ((__fp)->_IO_write_base = (__fp)->_IO_write_ptr = __p, (__fp)->_IO_write_end = (__ep))
382#define _IO_have_backup(fp) ((fp)->_IO_save_base != NULL)
383#define _IO_in_backup(fp) ((fp)->_flags & _IO_IN_BACKUP)
384#define _IO_have_markers(fp) ((fp)->_markers != NULL)
385#define _IO_blen(fp) ((fp)->_IO_buf_end - (fp)->_IO_buf_base)
386
387/* Jumptable functions for files. */
388
389extern int _IO_file_doallocate __P ((_IO_FILE *));
390extern _IO_FILE* _IO_file_setbuf __P ((_IO_FILE *, char *, _IO_ssize_t));
391#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
392extern _IO_off64_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
393extern _IO_off64_t _IO_file_seek __P ((_IO_FILE *, _IO_off64_t, int));
394#else
395extern _IO_off_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
396extern _IO_off_t _IO_file_seek __P ((_IO_FILE *, _IO_off_t, int));
397#endif
398extern _IO_size_t _IO_file_xsputn __P ((_IO_FILE *, const void *, _IO_size_t));
399extern int _IO_file_stat __P ((_IO_FILE *, void *));
400extern int _IO_file_close __P ((_IO_FILE *));
401extern int _IO_file_underflow __P ((_IO_FILE *));
402extern int _IO_file_overflow __P ((_IO_FILE *, int));
403#define _IO_file_is_open(__fp) ((__fp)->_fileno >= 0)
404extern void _IO_file_init __P ((_IO_FILE *));
405extern _IO_FILE* _IO_file_attach __P ((_IO_FILE *, int));
406extern _IO_FILE* _IO_file_open __P ((_IO_FILE *, const char *, int, int,
407				     int, int));
408#if _G_IO_IO_FILE_VERSION == 0x20001
409extern _IO_FILE* _IO_file_fopen __P ((_IO_FILE *, const char *, const char *,
410				      int));
411#else
412extern _IO_FILE* _IO_file_fopen __P ((_IO_FILE *, const char *, const char *));
413#endif
414extern _IO_ssize_t _IO_file_write __P ((_IO_FILE *, const void *,
415					_IO_ssize_t));
416extern _IO_ssize_t _IO_file_read __P ((_IO_FILE *, void *, _IO_ssize_t));
417extern int _IO_file_sync __P ((_IO_FILE *));
418extern int _IO_file_close_it __P ((_IO_FILE *));
419extern void _IO_file_finish __P ((_IO_FILE *, int));
420
421/* Jumptable functions for proc_files. */
422extern _IO_FILE* _IO_proc_open __P ((_IO_FILE *, const char *, const char *));
423extern int _IO_proc_close __P ((_IO_FILE *));
424
425/* Jumptable functions for strfiles. */
426extern int _IO_str_underflow __P ((_IO_FILE *));
427extern int _IO_str_overflow __P ((_IO_FILE *, int));
428extern int _IO_str_pbackfail __P ((_IO_FILE *, int));
429#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
430extern _IO_off64_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
431#else
432extern _IO_off_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
433#endif
434extern void _IO_str_finish __P ((_IO_FILE *, int));
435
436/* Other strfile functions */
437extern void _IO_str_init_static __P ((_IO_FILE *, char *, int, char *));
438extern void _IO_str_init_readonly __P ((_IO_FILE *, const char *, int));
439extern _IO_ssize_t _IO_str_count __P ((_IO_FILE *));
440
441extern int _IO_vasprintf __P ((char **result_ptr, __const char *format,
442			       _IO_va_list args));
443extern int _IO_vdprintf __P ((int d, __const char *format, _IO_va_list arg));
444extern int _IO_vsnprintf __P ((char *string, _IO_size_t maxlen,
445			       __const char *format, _IO_va_list args));
446
447
448extern _IO_size_t _IO_getline __P ((_IO_FILE *,char *, _IO_size_t, int, int));
449extern _IO_size_t _IO_getline_info __P ((_IO_FILE *,char *, _IO_size_t,
450					 int, int, int *));
451extern _IO_ssize_t _IO_getdelim __P ((char **, _IO_size_t *, int, _IO_FILE *));
452extern double _IO_strtod __P ((const char *, char **));
453extern char *_IO_dtoa __P ((double __d, int __mode, int __ndigits,
454			    int *__decpt, int *__sign, char **__rve));
455extern int _IO_outfloat __P ((double __value, _IO_FILE *__sb, int __type,
456			      int __width, int __precision, int __flags,
457			      int __sign_mode, int __fill));
458
459extern _IO_FILE *_IO_list_all;
460extern void (*_IO_cleanup_registration_needed) __PMT ((void));
461
462#ifndef EOF
463# define EOF (-1)
464#endif
465#ifndef NULL
466# if defined __GNUG__ && \
467    (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
468#  define NULL (__null)
469# else
470#  if !defined(__cplusplus)
471#   define NULL ((void*)0)
472#  else
473#   define NULL (0)
474#  endif
475# endif
476#endif
477
478#if _G_HAVE_MMAP
479
480# include <unistd.h>
481# include <fcntl.h>
482# include <sys/mman.h>
483# include <sys/param.h>
484
485# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
486#  define MAP_ANONYMOUS MAP_ANON
487# endif
488
489# if !defined(MAP_ANONYMOUS) || !defined(EXEC_PAGESIZE)
490#  undef _G_HAVE_MMAP
491#  define _G_HAVE_MMAP 0
492# endif
493
494#endif /* _G_HAVE_MMAP */
495
496#if _G_HAVE_MMAP
497
498# ifdef _LIBC
499/* When using this code in the GNU libc we must not pollute the name space.  */
500#  define mmap __mmap
501#  define munmap __munmap
502# endif
503
504# define ROUND_TO_PAGE(_S) \
505       (((_S) + EXEC_PAGESIZE - 1) & ~(EXEC_PAGESIZE - 1))
506
507# define FREE_BUF(_B, _S) \
508       munmap ((_B), ROUND_TO_PAGE (_S))
509# define ALLOC_BUF(_B, _S, _R) \
510       do {								      \
511	  (_B) = (char *) mmap (0, ROUND_TO_PAGE (_S),			      \
512				PROT_READ | PROT_WRITE,			      \
513				MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);	      \
514	  if ((_B) == (char *) -1)					      \
515	    return (_R);						      \
516       } while (0)
517
518#else /* _G_HAVE_MMAP */
519
520# define FREE_BUF(_B, _S) \
521       free(_B)
522# define ALLOC_BUF(_B, _S, _R) \
523       do {								      \
524	  (_B) = (char*)malloc(_S);					      \
525	  if ((_B) == NULL)						      \
526	    return (_R);						      \
527       } while (0)
528
529#endif /* _G_HAVE_MMAP */
530
531#ifndef OS_FSTAT
532# define OS_FSTAT fstat
533#endif
534struct stat;
535extern _IO_ssize_t _IO_read __P ((int, void *, _IO_size_t));
536extern _IO_ssize_t _IO_write __P ((int, const void *, _IO_size_t));
537extern _IO_off_t _IO_lseek __P ((int, _IO_off_t, int));
538extern int _IO_close __P ((int));
539extern int _IO_fstat __P ((int, struct stat *));
540extern int _IO_vscanf __P ((const char *, _IO_va_list));
541
542/* Operations on _IO_fpos_t.
543   Normally, these are trivial, but we provide hooks for configurations
544   where an _IO_fpos_t is a struct.
545   Note that _IO_off_t must be an integral type. */
546
547/* _IO_pos_BAD is an _IO_off_t value indicating error, unknown, or EOF. */
548#ifndef _IO_pos_BAD
549# if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
550#  define _IO_pos_BAD ((_IO_off64_t) -1)
551# else
552#  define _IO_pos_BAD ((_IO_off_t) -1)
553# endif
554#endif
555/* _IO_pos_as_off converts an _IO_fpos_t value to an _IO_off_t value. */
556#ifndef _IO_pos_as_off
557# if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
558#  define _IO_pos_as_off(__pos) ((_IO_off64_t) (__pos))
559# else
560#  define _IO_pos_as_off(__pos) ((_IO_off_t) (__pos))
561# endif
562#endif
563/* _IO_pos_adjust adjust an _IO_fpos_t by some number of bytes. */
564#ifndef _IO_pos_adjust
565# define _IO_pos_adjust(__pos, __delta) ((__pos) += (__delta))
566#endif
567/* _IO_pos_0 is an _IO_fpos_t value indicating beginning of file. */
568#ifndef _IO_pos_0
569# if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
570#  define _IO_pos_0 ((_IO_fpos64_t) 0)
571# else
572#  define _IO_pos_0 ((_IO_fpos_t) 0)
573# endif
574#endif
575
576#ifdef __cplusplus
577}
578#endif
579
580#ifdef _IO_MTSAFE_IO
581/* check following! */
582# define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
583       { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
584         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, FD, \
585	   0, 0, 0, 0, { 0 }, &_IO_stdfile_##FD##_lock }
586#else
587/* check following! */
588# define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
589       { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
590	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, FD }
591#endif
592
593/* VTABLE_LABEL defines NAME as of the CLASS class.
594   CNLENGTH is strlen(#CLASS).  */
595#ifdef __GNUC__
596# if _G_VTABLE_LABEL_HAS_LENGTH
597#  define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
598  extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CNLENGTH #CLASS);
599# else
600#  define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
601  extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CLASS);
602# endif
603#endif /* __GNUC__ */
604
605#if !defined(builtinbuf_vtable) && defined(__cplusplus)
606# ifdef __GNUC__
607VTABLE_LABEL(builtinbuf_vtable, builtinbuf, 10)
608# else
609#  if _G_VTABLE_LABEL_HAS_LENGTH
610#   define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##10builtinbuf
611#  else
612#   define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##builtinbuf
613#  endif
614# endif
615#endif /* !defined(builtinbuf_vtable) && defined(__cplusplus) */
616
617#if defined(__STDC__) || defined(__cplusplus)
618# define _IO_va_start(args, last) va_start(args, last)
619#else
620# define _IO_va_start(args, last) va_start(args)
621#endif
622
623extern struct _IO_fake_stdiobuf _IO_stdin_buf, _IO_stdout_buf, _IO_stderr_buf;
624
625#if 1
626# define COERCE_FILE(FILE) /* Nothing */
627#else
628/* This is part of the kludge for binary compatibility with old stdio. */
629# define COERCE_FILE(FILE) \
630  (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) == _OLD_MAGIC_MASK \
631    && (FILE) = *(FILE**)&((int*)fp)[1])
632#endif
633
634#ifdef EINVAL
635# define MAYBE_SET_EINVAL __set_errno (EINVAL)
636#else
637# define MAYBE_SET_EINVAL /* nothing */
638#endif
639
640#ifdef IO_DEBUG
641# define CHECK_FILE(FILE, RET) \
642	if ((FILE) == NULL) { MAYBE_SET_EINVAL; return RET; } \
643	else { COERCE_FILE(FILE); \
644	       if (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
645	  { MAYBE_SET_EINVAL; return RET; }}
646#else
647# define CHECK_FILE(FILE, RET) COERCE_FILE (FILE)
648#endif
649