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