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