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