1/* Copyright (C) 1991-1995,1997-2006,2007,2009 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3   Written by Per Bothner <bothner@cygnus.com>.
4
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.
9
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.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, write to the Free
17   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18   02111-1307 USA.
19
20   As a special exception, if you link the code in this file with
21   files compiled with a GNU compiler to produce an executable,
22   that does not cause the resulting executable to be covered by
23   the GNU Lesser General Public License.  This exception does not
24   however invalidate any other reasons why the executable file
25   might be covered by the GNU Lesser General Public License.
26   This exception applies to code released by its copyright holders
27   in files containing the exception.  */
28
29#ifndef _IO_STDIO_H
30#define _IO_STDIO_H
31
32#include <_G_config.h>
33/* ALL of these should be defined in _G_config.h */
34#define _IO_pos_t _G_fpos_t /* obsolete */
35#define _IO_fpos_t _G_fpos_t
36#define _IO_fpos64_t _G_fpos64_t
37#define _IO_size_t _G_size_t
38#define _IO_ssize_t _G_ssize_t
39#define _IO_off_t _G_off_t
40#define _IO_off64_t _G_off64_t
41#define _IO_pid_t _G_pid_t
42#define _IO_uid_t _G_uid_t
43#define _IO_iconv_t _G_iconv_t
44#define _IO_HAVE_SYS_WAIT _G_HAVE_SYS_WAIT
45#define _IO_HAVE_ST_BLKSIZE _G_HAVE_ST_BLKSIZE
46#define _IO_BUFSIZ _G_BUFSIZ
47#define _IO_va_list _G_va_list
48#define _IO_wint_t _G_wint_t
49
50#ifdef _G_NEED_STDARG_H
51/* This define avoids name pollution if we're using GNU stdarg.h */
52# define __need___va_list
53# include <stdarg.h>
54# ifdef __GNUC_VA_LIST
55#  undef _IO_va_list
56#  define _IO_va_list __gnuc_va_list
57# endif /* __GNUC_VA_LIST */
58#endif
59
60#ifndef __P
61# if _G_HAVE_SYS_CDEFS
62#  include <sys/cdefs.h>
63# else
64#  ifdef __STDC__
65#   define __P(p) p
66#   define __PMT(p) p
67#  else
68#   define __P(p) ()
69#   define __PMT(p) ()
70#  endif
71# endif
72#endif /*!__P*/
73
74/* For backward compatibility */
75#ifndef _PARAMS
76# define _PARAMS(protos) __P(protos)
77#endif /*!_PARAMS*/
78
79#ifndef __STDC__
80# ifndef const
81#  define const
82# endif
83#endif
84#define _IO_UNIFIED_JUMPTABLES 1
85#ifndef _G_HAVE_PRINTF_FP
86# define _IO_USE_DTOA 1
87#endif
88
89#ifndef EOF
90# define EOF (-1)
91#endif
92#ifndef NULL
93# if defined __GNUG__ && \
94    (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
95#  define NULL (__null)
96# else
97#  if !defined(__cplusplus)
98#   define NULL ((void*)0)
99#  else
100#   define NULL (0)
101#  endif
102# endif
103#endif
104
105#define _IOS_INPUT	1
106#define _IOS_OUTPUT	2
107#define _IOS_ATEND	4
108#define _IOS_APPEND	8
109#define _IOS_TRUNC	16
110#define _IOS_NOCREATE	32
111#define _IOS_NOREPLACE	64
112#define _IOS_BIN	128
113
114/* Magic numbers and bits for the _flags field.
115   The magic numbers use the high-order bits of _flags;
116   the remaining bits are available for variable flags.
117   Note: The magic numbers must all be negative if stdio
118   emulation is desired. */
119
120#define _IO_MAGIC 0xFBAD0000 /* Magic number */
121#define _OLD_STDIO_MAGIC 0xFABC0000 /* Emulate old stdio. */
122#define _IO_MAGIC_MASK 0xFFFF0000
123#define _IO_USER_BUF 1 /* User owns buffer; don't delete it on close. */
124#define _IO_UNBUFFERED 2
125#define _IO_NO_READS 4 /* Reading not allowed */
126#define _IO_NO_WRITES 8 /* Writing not allowd */
127#define _IO_EOF_SEEN 0x10
128#define _IO_ERR_SEEN 0x20
129#define _IO_DELETE_DONT_CLOSE 0x40 /* Don't call close(_fileno) on cleanup. */
130#define _IO_LINKED 0x80 /* Set if linked (using _chain) to streambuf::_list_all.*/
131#define _IO_IN_BACKUP 0x100
132#define _IO_LINE_BUF 0x200
133#define _IO_TIED_PUT_GET 0x400 /* Set if put and get pointer logicly tied. */
134#define _IO_CURRENTLY_PUTTING 0x800
135#define _IO_IS_APPENDING 0x1000
136#define _IO_IS_FILEBUF 0x2000
137#define _IO_BAD_SEEN 0x4000
138#define _IO_USER_LOCK 0x8000
139
140#define _IO_FLAGS2_MMAP 1
141#define _IO_FLAGS2_NOTCANCEL 2
142#ifdef _LIBC
143# define _IO_FLAGS2_FORTIFY 4
144#endif
145#define _IO_FLAGS2_USER_WBUF 8
146#ifdef _LIBC
147# define _IO_FLAGS2_SCANF_STD 16
148#endif
149
150/* These are "formatting flags" matching the iostream fmtflags enum values. */
151#define _IO_SKIPWS 01
152#define _IO_LEFT 02
153#define _IO_RIGHT 04
154#define _IO_INTERNAL 010
155#define _IO_DEC 020
156#define _IO_OCT 040
157#define _IO_HEX 0100
158#define _IO_SHOWBASE 0200
159#define _IO_SHOWPOINT 0400
160#define _IO_UPPERCASE 01000
161#define _IO_SHOWPOS 02000
162#define _IO_SCIENTIFIC 04000
163#define _IO_FIXED 010000
164#define _IO_UNITBUF 020000
165#define _IO_STDIO 040000
166#define _IO_DONT_CLOSE 0100000
167#define _IO_BOOLALPHA 0200000
168
169
170struct _IO_jump_t;  struct _IO_FILE;
171
172/* Handle lock.  */
173#ifdef _IO_MTSAFE_IO
174# if defined __GLIBC__ && __GLIBC__ >= 2
175#  include <bits/stdio-lock.h>
176# else
177/*# include <comthread.h>*/
178# endif
179#else
180typedef void _IO_lock_t;
181#endif
182
183
184/* A streammarker remembers a position in a buffer. */
185
186struct _IO_marker {
187  struct _IO_marker *_next;
188  struct _IO_FILE *_sbuf;
189  /* If _pos >= 0
190 it points to _buf->Gbase()+_pos. FIXME comment */
191  /* if _pos < 0, it points to _buf->eBptr()+_pos. FIXME comment */
192  int _pos;
193#if 0
194    void set_streampos(streampos sp) { _spos = sp; }
195    void set_offset(int offset) { _pos = offset; _spos = (streampos)(-2); }
196  public:
197    streammarker(streambuf *sb);
198    ~streammarker();
199    int saving() { return  _spos == -2; }
200    int delta(streammarker&);
201    int delta();
202#endif
203};
204
205/* This is the structure from the libstdc++ codecvt class.  */
206enum __codecvt_result
207{
208  __codecvt_ok,
209  __codecvt_partial,
210  __codecvt_error,
211  __codecvt_noconv
212};
213
214#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
215/* The order of the elements in the following struct must match the order
216   of the virtual functions in the libstdc++ codecvt class.  */
217struct _IO_codecvt
218{
219  void (*__codecvt_destr) (struct _IO_codecvt *);
220  enum __codecvt_result (*__codecvt_do_out) (struct _IO_codecvt *,
221					     __mbstate_t *,
222					     const wchar_t *,
223					     const wchar_t *,
224					     const wchar_t **, char *,
225					     char *, char **);
226  enum __codecvt_result (*__codecvt_do_unshift) (struct _IO_codecvt *,
227						 __mbstate_t *, char *,
228						 char *, char **);
229  enum __codecvt_result (*__codecvt_do_in) (struct _IO_codecvt *,
230					    __mbstate_t *,
231					    const char *, const char *,
232					    const char **, wchar_t *,
233					    wchar_t *, wchar_t **);
234  int (*__codecvt_do_encoding) (struct _IO_codecvt *);
235  int (*__codecvt_do_always_noconv) (struct _IO_codecvt *);
236  int (*__codecvt_do_length) (struct _IO_codecvt *, __mbstate_t *,
237			      const char *, const char *, _IO_size_t);
238  int (*__codecvt_do_max_length) (struct _IO_codecvt *);
239
240  _IO_iconv_t __cd_in;
241  _IO_iconv_t __cd_out;
242};
243
244/* Extra data for wide character streams.  */
245struct _IO_wide_data
246{
247  wchar_t *_IO_read_ptr;	/* Current read pointer */
248  wchar_t *_IO_read_end;	/* End of get area. */
249  wchar_t *_IO_read_base;	/* Start of putback+get area. */
250  wchar_t *_IO_write_base;	/* Start of put area. */
251  wchar_t *_IO_write_ptr;	/* Current put pointer. */
252  wchar_t *_IO_write_end;	/* End of put area. */
253  wchar_t *_IO_buf_base;	/* Start of reserve area. */
254  wchar_t *_IO_buf_end;		/* End of reserve area. */
255  /* The following fields are used to support backing up and undo. */
256  wchar_t *_IO_save_base;	/* Pointer to start of non-current get area. */
257  wchar_t *_IO_backup_base;	/* Pointer to first valid character of
258				   backup area */
259  wchar_t *_IO_save_end;	/* Pointer to end of non-current get area. */
260
261  __mbstate_t _IO_state;
262  __mbstate_t _IO_last_state;
263  struct _IO_codecvt _codecvt;
264
265  wchar_t _shortbuf[1];
266
267  const struct _IO_jump_t *_wide_vtable;
268};
269#endif
270
271struct _IO_FILE {
272  int _flags;		/* High-order word is _IO_MAGIC; rest is flags. */
273#define _IO_file_flags _flags
274
275  /* The following pointers correspond to the C++ streambuf protocol. */
276  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
277  char* _IO_read_ptr;	/* Current read pointer */
278  char* _IO_read_end;	/* End of get area. */
279  char* _IO_read_base;	/* Start of putback+get area. */
280  char* _IO_write_base;	/* Start of put area. */
281  char* _IO_write_ptr;	/* Current put pointer. */
282  char* _IO_write_end;	/* End of put area. */
283  char* _IO_buf_base;	/* Start of reserve area. */
284  char* _IO_buf_end;	/* End of reserve area. */
285  /* The following fields are used to support backing up and undo. */
286  char *_IO_save_base; /* Pointer to start of non-current get area. */
287  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
288  char *_IO_save_end; /* Pointer to end of non-current get area. */
289
290  struct _IO_marker *_markers;
291
292  struct _IO_FILE *_chain;
293
294  int _fileno;
295#if 0
296  int _blksize;
297#else
298  int _flags2;
299#endif
300  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */
301
302#define __HAVE_COLUMN /* temporary */
303  /* 1+column number of pbase(); 0 is unknown. */
304  unsigned short _cur_column;
305  signed char _vtable_offset;
306  char _shortbuf[1];
307
308  /*  char* _save_gptr;  char* _save_egptr; */
309
310  _IO_lock_t *_lock;
311#ifdef _IO_USE_OLD_IO_FILE
312};
313
314struct _IO_FILE_complete
315{
316  struct _IO_FILE _file;
317#endif
318#if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
319  _IO_off64_t _offset;
320# if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
321  /* Wide character stream stuff.  */
322  struct _IO_codecvt *_codecvt;
323  struct _IO_wide_data *_wide_data;
324  struct _IO_FILE *_freeres_list;
325  void *_freeres_buf;
326  size_t _freeres_size;
327# else
328  void *__pad1;
329  void *__pad2;
330  void *__pad3;
331  void *__pad4;
332  size_t __pad5;
333# endif
334  int _mode;
335  /* Make sure we don't get into trouble again.  */
336  char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
337#endif
338};
339
340#ifndef __cplusplus
341typedef struct _IO_FILE _IO_FILE;
342#endif
343
344struct _IO_FILE_plus;
345
346extern struct _IO_FILE_plus _IO_2_1_stdin_;
347extern struct _IO_FILE_plus _IO_2_1_stdout_;
348extern struct _IO_FILE_plus _IO_2_1_stderr_;
349#ifndef _LIBC
350#define _IO_stdin ((_IO_FILE*)(&_IO_2_1_stdin_))
351#define _IO_stdout ((_IO_FILE*)(&_IO_2_1_stdout_))
352#define _IO_stderr ((_IO_FILE*)(&_IO_2_1_stderr_))
353#else
354extern _IO_FILE *_IO_stdin attribute_hidden;
355extern _IO_FILE *_IO_stdout attribute_hidden;
356extern _IO_FILE *_IO_stderr attribute_hidden;
357#endif
358
359
360/* Functions to do I/O and file management for a stream.  */
361
362/* Read NBYTES bytes from COOKIE into a buffer pointed to by BUF.
363   Return number of bytes read.  */
364typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
365
366/* Write N bytes pointed to by BUF to COOKIE.  Write all N bytes
367   unless there is an error.  Return number of bytes written, or -1 if
368   there is an error without writing anything.  If the file has been
369   opened for append (__mode.__append set), then set the file pointer
370   to the end of the file and then do the write; if not, just write at
371   the current file pointer.  */
372typedef __ssize_t __io_write_fn (void *__cookie, __const char *__buf,
373				 size_t __n);
374
375/* Move COOKIE's file position to *POS bytes from the
376   beginning of the file (if W is SEEK_SET),
377   the current position (if W is SEEK_CUR),
378   or the end of the file (if W is SEEK_END).
379   Set *POS to the new file position.
380   Returns zero if successful, nonzero if not.  */
381typedef int __io_seek_fn (void *__cookie, _IO_off64_t *__pos, int __w);
382
383/* Close COOKIE.  */
384typedef int __io_close_fn (void *__cookie);
385
386
387#ifdef _GNU_SOURCE
388/* User-visible names for the above.  */
389typedef __io_read_fn cookie_read_function_t;
390typedef __io_write_fn cookie_write_function_t;
391typedef __io_seek_fn cookie_seek_function_t;
392typedef __io_close_fn cookie_close_function_t;
393
394/* The structure with the cookie function pointers.  */
395typedef struct
396{
397  __io_read_fn *read;		/* Read bytes.  */
398  __io_write_fn *write;		/* Write bytes.  */
399  __io_seek_fn *seek;		/* Seek/tell file position.  */
400  __io_close_fn *close;		/* Close file.  */
401} _IO_cookie_io_functions_t;
402typedef _IO_cookie_io_functions_t cookie_io_functions_t;
403
404struct _IO_cookie_file;
405
406/* Initialize one of those.  */
407extern void _IO_cookie_init (struct _IO_cookie_file *__cfile, int __read_write,
408			     void *__cookie, _IO_cookie_io_functions_t __fns);
409#endif
410
411
412#ifdef __cplusplus
413extern "C" {
414#endif
415
416extern int __underflow (_IO_FILE *);
417extern int __uflow (_IO_FILE *);
418extern int __overflow (_IO_FILE *, int);
419#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
420extern _IO_wint_t __wunderflow (_IO_FILE *);
421extern _IO_wint_t __wuflow (_IO_FILE *);
422extern _IO_wint_t __woverflow (_IO_FILE *, _IO_wint_t);
423#endif
424
425#if  __GNUC__ >= 3
426# define _IO_BE(expr, res) __builtin_expect ((expr), res)
427#else
428# define _IO_BE(expr, res) (expr)
429#endif
430
431#define _IO_getc_unlocked(_fp) \
432       (_IO_BE ((_fp)->_IO_read_ptr >= (_fp)->_IO_read_end, 0) \
433	? __uflow (_fp) : *(unsigned char *) (_fp)->_IO_read_ptr++)
434#define _IO_peekc_unlocked(_fp) \
435       (_IO_BE ((_fp)->_IO_read_ptr >= (_fp)->_IO_read_end, 0) \
436	  && __underflow (_fp) == EOF ? EOF \
437	: *(unsigned char *) (_fp)->_IO_read_ptr)
438#define _IO_putc_unlocked(_ch, _fp) \
439   (_IO_BE ((_fp)->_IO_write_ptr >= (_fp)->_IO_write_end, 0) \
440    ? __overflow (_fp, (unsigned char) (_ch)) \
441    : (unsigned char) (*(_fp)->_IO_write_ptr++ = (_ch)))
442
443#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
444# define _IO_getwc_unlocked(_fp) \
445  (_IO_BE ((_fp)->_wide_data == NULL					\
446	   || ((_fp)->_wide_data->_IO_read_ptr				\
447	       >= (_fp)->_wide_data->_IO_read_end), 0)			\
448   ? __wuflow (_fp) : (_IO_wint_t) *(_fp)->_wide_data->_IO_read_ptr++)
449# define _IO_putwc_unlocked(_wch, _fp) \
450  (_IO_BE ((_fp)->_wide_data == NULL					\
451	   || ((_fp)->_wide_data->_IO_write_ptr				\
452	       >= (_fp)->_wide_data->_IO_write_end), 0)			\
453   ? __woverflow (_fp, _wch)						\
454   : (_IO_wint_t) (*(_fp)->_wide_data->_IO_write_ptr++ = (_wch)))
455#endif
456
457#define _IO_feof_unlocked(__fp) (((__fp)->_flags & _IO_EOF_SEEN) != 0)
458#define _IO_ferror_unlocked(__fp) (((__fp)->_flags & _IO_ERR_SEEN) != 0)
459
460extern int _IO_getc (_IO_FILE *__fp);
461extern int _IO_putc (int __c, _IO_FILE *__fp);
462extern int _IO_feof (_IO_FILE *__fp) __THROW;
463extern int _IO_ferror (_IO_FILE *__fp) __THROW;
464
465extern int _IO_peekc_locked (_IO_FILE *__fp);
466
467/* This one is for Emacs. */
468#define _IO_PENDING_OUTPUT_COUNT(_fp)	\
469	((_fp)->_IO_write_ptr - (_fp)->_IO_write_base)
470
471extern void _IO_flockfile (_IO_FILE *) __THROW;
472extern void _IO_funlockfile (_IO_FILE *) __THROW;
473extern int _IO_ftrylockfile (_IO_FILE *) __THROW;
474
475#ifdef _IO_MTSAFE_IO
476# define _IO_peekc(_fp) _IO_peekc_locked (_fp)
477# define _IO_flockfile(_fp) \
478  if (((_fp)->_flags & _IO_USER_LOCK) == 0) _IO_flockfile (_fp)
479# define _IO_funlockfile(_fp) \
480  if (((_fp)->_flags & _IO_USER_LOCK) == 0) _IO_funlockfile (_fp)
481#else
482# define _IO_peekc(_fp) _IO_peekc_unlocked (_fp)
483# define _IO_flockfile(_fp) /**/
484# define _IO_funlockfile(_fp) /**/
485# define _IO_ftrylockfile(_fp) /**/
486# define _IO_cleanup_region_start(_fct, _fp) /**/
487# define _IO_cleanup_region_end(_Doit) /**/
488#endif /* !_IO_MTSAFE_IO */
489
490extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
491			_IO_va_list, int *__restrict);
492extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
493			 _IO_va_list);
494extern _IO_ssize_t _IO_padn (_IO_FILE *, int, _IO_ssize_t);
495extern _IO_size_t _IO_sgetn (_IO_FILE *, void *, _IO_size_t);
496
497extern _IO_off64_t _IO_seekoff (_IO_FILE *, _IO_off64_t, int, int);
498extern _IO_off64_t _IO_seekpos (_IO_FILE *, _IO_off64_t, int);
499
500extern void _IO_free_backup_area (_IO_FILE *) __THROW;
501
502#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
503extern _IO_wint_t _IO_getwc (_IO_FILE *__fp);
504extern _IO_wint_t _IO_putwc (wchar_t __wc, _IO_FILE *__fp);
505extern int _IO_fwide (_IO_FILE *__fp, int __mode) __THROW;
506# if __GNUC__ >= 2
507/* While compiling glibc we have to handle compatibility with very old
508   versions.  */
509#  if defined _LIBC && defined SHARED
510#   include <shlib-compat.h>
511#   if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
512#    define _IO_fwide_maybe_incompatible \
513  (__builtin_expect (&_IO_stdin_used == NULL, 0))
514extern const int _IO_stdin_used;
515weak_extern (_IO_stdin_used);
516#   endif
517#  endif
518#  ifndef _IO_fwide_maybe_incompatible
519#   define _IO_fwide_maybe_incompatible (0)
520#  endif
521/* A special optimized version of the function above.  It optimizes the
522   case of initializing an unoriented byte stream.  */
523#  define _IO_fwide(__fp, __mode) \
524  ({ int __result = (__mode);						      \
525     if (__result < 0 && ! _IO_fwide_maybe_incompatible)		      \
526       {								      \
527	 if ((__fp)->_mode == 0)					      \
528	   /* We know that all we have to do is to set the flag.  */	      \
529	   (__fp)->_mode = -1;						      \
530	 __result = (__fp)->_mode;					      \
531       }								      \
532     else if (__builtin_constant_p (__mode) && (__mode) == 0)		      \
533       __result = _IO_fwide_maybe_incompatible ? -1 : (__fp)->_mode;	      \
534     else								      \
535       __result = _IO_fwide (__fp, __result);				      \
536     __result; })
537# endif
538
539extern int _IO_vfwscanf (_IO_FILE * __restrict, const wchar_t * __restrict,
540			 _IO_va_list, int *__restrict);
541extern int _IO_vfwprintf (_IO_FILE *__restrict, const wchar_t *__restrict,
542			  _IO_va_list);
543extern _IO_ssize_t _IO_wpadn (_IO_FILE *, wint_t, _IO_ssize_t);
544extern void _IO_free_wbackup_area (_IO_FILE *) __THROW;
545#endif
546
547#ifdef __LDBL_COMPAT
548# include <bits/libio-ldbl.h>
549#endif
550
551#ifdef __cplusplus
552}
553#endif
554
555#endif /* _IO_STDIO_H */
556