Upadte.
[glibc.git] / libio / libioP.h
blob8525b718277697e7985c8c98fe16e6069e17bfdd
1 /*
2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 #include <errno.h>
26 #ifndef errno
27 extern int errno;
28 #endif
30 #include "iolibio.h"
32 #if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(__cplusplus)
33 /* All known AIX compilers implement these things (but don't always
34 define __STDC__). The RISC/OS MIPS compiler defines these things
35 in SVR4 mode, but does not define __STDC__. */
37 #define AND ,
38 #define DEFUN(name, arglist, args) name(args)
39 #define DEFUN_VOID(name) name(void)
41 #else /* Not ANSI C. */
43 #define AND ;
44 #ifndef const /* some systems define it in header files for non-ansi mode */
45 #define const
46 #endif
47 #define DEFUN(name, arglist, args) name arglist args;
48 #define DEFUN_VOID(name) name()
49 #endif /* ANSI C. */
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
55 #define _IO_seek_set 0
56 #define _IO_seek_cur 1
57 #define _IO_seek_end 2
59 typedef int (*_IO_overflow_t) __P((_IO_FILE*, int));
60 typedef int (*_IO_underflow_t) __P((_IO_FILE*));
61 typedef _IO_size_t (*_IO_xsputn_t) __P((_IO_FILE*,const void*,_IO_size_t));
62 typedef _IO_size_t (*_IO_xsgetn_t) __P((_IO_FILE*, void*, _IO_size_t));
63 typedef _IO_ssize_t (*_IO_read_t) __P((_IO_FILE*, void*, _IO_ssize_t));
64 typedef _IO_ssize_t (*_IO_write_t) __P((_IO_FILE*,const void*,_IO_ssize_t));
65 typedef int (*_IO_stat_t) __P((_IO_FILE*, void*));
66 typedef _IO_fpos_t (*_IO_seek_t) __P((_IO_FILE*, _IO_off_t, int));
67 typedef int (*_IO_doallocate_t) __P((_IO_FILE*));
68 typedef int (*_IO_pbackfail_t) __P((_IO_FILE*, int));
69 typedef _IO_FILE* (*_IO_setbuf_t) __P((_IO_FILE*, char *, _IO_ssize_t));
70 typedef int (*_IO_sync_t) __P((_IO_FILE*));
71 typedef void (*_IO_finish_t) __P((_IO_FILE*)); /* finalize */
72 typedef int (*_IO_close_t) __P((_IO_FILE*)); /* finalize */
73 typedef _IO_fpos_t (*_IO_seekoff_t) __P((_IO_FILE*, _IO_off_t, int, int));
75 /* The _IO_seek_cur and _IO_seek_end options are not allowed. */
76 typedef _IO_fpos_t (*_IO_seekpos_t) __P((_IO_FILE*, _IO_fpos_t, int));
78 #if !_IO_UNIFIED_JUMPTABLES
79 #define _IO_JUMPS(THIS) (THIS)->_jumps
80 #else
81 #define _IO_JUMPS(THIS) ((struct _IO_FILE_plus*)(THIS))->vtable
82 #endif
84 #if !_IO_UNIFIED_JUMPTABLES
85 #define JUMP_FIELD(TYPE, NAME) TYPE NAME
86 #define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC(THIS)
87 #define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC(THIS, X1)
88 #define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC(THIS, X1, X2)
89 #define JUMP3(FUNC, THIS, X1, X2, X3) _IO_JUMPS(THIS)->FUNC(THIS, X1, X2, X3)
90 #define JUMP_INIT(NAME, VALUE) VALUE
91 #else
92 #define JUMP_FIELD(TYPE, NAME) struct { short delta1, delta2; TYPE pfn; } NAME
93 #define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC.pfn(THIS)
94 #define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1)
95 #define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1, X2)
96 #define JUMP3(FUNC, THIS, X1,X2,X3) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1,X2, X3)
97 #define JUMP_INIT(NAME, VALUE) {0, 0, VALUE}
98 #endif
99 #define JUMP_INIT_DUMMY JUMP_INIT(dummy, 0)
101 #define _IO_FINISH(FP) JUMP0(__finish, FP)
102 #define _IO_OVERFLOW(FP, CH) JUMP1(__overflow, FP, CH)
103 #define _IO_UNDERFLOW(FP) JUMP0(__underflow, FP)
104 #define _IO_UFLOW(FP) JUMP0(__uflow, FP)
105 #define _IO_PBACKFAIL(FP, CH) JUMP1(__pbackfail, FP, CH)
106 #define _IO_XSPUTN(FP, DATA, N) JUMP2(__xsputn, FP, DATA, N)
107 #define _IO_XSGETN(FP, DATA, N) JUMP2(__xsgetn, FP, DATA, N)
108 #define _IO_SEEKOFF(FP, OFF, DIR, MODE) JUMP3(__seekoff, FP, OFF, DIR, MODE)
109 #define _IO_SEEKPOS(FP, POS, FLAGS) JUMP2(__seekpos, FP, POS, FLAGS)
110 #define _IO_SETBUF(FP, BUFFER, LENGTH) JUMP2(__setbuf, FP, BUFFER, LENGTH)
111 #define _IO_SYNC(FP) JUMP0(__sync, FP)
112 #define _IO_DOALLOCATE(FP) JUMP0(__doallocate, FP)
113 #define _IO_SYSREAD(FP, DATA, LEN) JUMP2(__read, FP, DATA, LEN)
114 #define _IO_SYSWRITE(FP, DATA, LEN) JUMP2(__write, FP, DATA, LEN)
115 #define _IO_SYSSEEK(FP, OFFSET, MODE) JUMP2(__seek, FP, OFFSET, MODE)
116 #define _IO_SYSCLOSE(FP) JUMP0(__close, FP)
117 #define _IO_SYSSTAT(FP, BUF) JUMP1(__stat, FP, BUF)
119 #define _IO_CHAR_TYPE char /* unsigned char ? */
120 #define _IO_INT_TYPE int
122 struct _IO_jump_t {
123 JUMP_FIELD(_G_size_t, __dummy);
124 JUMP_FIELD(_IO_finish_t, __finish);
125 JUMP_FIELD(_IO_overflow_t, __overflow);
126 JUMP_FIELD(_IO_underflow_t, __underflow);
127 JUMP_FIELD(_IO_underflow_t, __uflow);
128 JUMP_FIELD(_IO_pbackfail_t, __pbackfail);
129 /* showmany */
130 JUMP_FIELD(_IO_xsputn_t, __xsputn);
131 JUMP_FIELD(_IO_xsgetn_t, __xsgetn);
132 JUMP_FIELD(_IO_seekoff_t, __seekoff);
133 JUMP_FIELD(_IO_seekpos_t, __seekpos);
134 JUMP_FIELD(_IO_setbuf_t, __setbuf);
135 JUMP_FIELD(_IO_sync_t, __sync);
136 JUMP_FIELD(_IO_doallocate_t, __doallocate);
137 JUMP_FIELD(_IO_read_t, __read);
138 JUMP_FIELD(_IO_write_t, __write);
139 JUMP_FIELD(_IO_seek_t, __seek);
140 JUMP_FIELD(_IO_close_t, __close);
141 JUMP_FIELD(_IO_stat_t, __stat);
142 #if 0
143 get_column;
144 set_column;
145 #endif
148 /* We always allocate an extra word following an _IO_FILE.
149 This is for compatibility with C++ streambuf; the word can
150 be used to smash to a pointer to a virtual function table. */
152 struct _IO_FILE_plus {
153 _IO_FILE file;
154 #if _IO_UNIFIED_JUMPTABLES
155 const struct _IO_jump_t *vtable;
156 #else
157 const void *vtable;
158 #endif
161 /* Generic functions */
163 extern _IO_fpos_t _IO_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
164 extern _IO_fpos_t _IO_seekpos __P((_IO_FILE*, _IO_fpos_t, int));
166 extern int _IO_switch_to_get_mode __P((_IO_FILE*));
167 extern void _IO_init __P((_IO_FILE*, int));
168 extern int _IO_sputbackc __P((_IO_FILE*, int));
169 extern int _IO_sungetc __P((_IO_FILE*));
170 extern void _IO_un_link __P((_IO_FILE*));
171 extern void _IO_link_in __P((_IO_FILE *));
172 extern void _IO_doallocbuf __P((_IO_FILE*));
173 extern void _IO_unsave_markers __P((_IO_FILE*));
174 extern void _IO_setb __P((_IO_FILE*, char*, char*, int));
175 extern unsigned _IO_adjust_column __P((unsigned, const char *, int));
176 #define _IO_sputn(__fp, __s, __n) _IO_XSPUTN(__fp, __s, __n)
178 /* Marker-related function. */
180 extern void _IO_init_marker __P((struct _IO_marker *, _IO_FILE *));
181 extern void _IO_remove_marker __P((struct _IO_marker*));
182 extern int _IO_marker_difference __P((struct _IO_marker *, struct _IO_marker *));
183 extern int _IO_marker_delta __P((struct _IO_marker *));
184 extern int _IO_seekmark __P((_IO_FILE *, struct _IO_marker *, int));
186 /* Default jumptable functions. */
188 extern int _IO_default_underflow __P((_IO_FILE*));
189 extern int _IO_default_uflow __P((_IO_FILE*));
190 extern int _IO_default_doallocate __P((_IO_FILE*));
191 extern void _IO_default_finish __P((_IO_FILE *));
192 extern int _IO_default_pbackfail __P((_IO_FILE*, int));
193 extern _IO_FILE* _IO_default_setbuf __P((_IO_FILE *, char*, _IO_ssize_t));
194 extern _IO_size_t _IO_default_xsputn __P((_IO_FILE *, const void*, _IO_size_t));
195 extern _IO_size_t _IO_default_xsgetn __P((_IO_FILE *, void*, _IO_size_t));
196 extern _IO_fpos_t _IO_default_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
197 extern _IO_fpos_t _IO_default_seekpos __P((_IO_FILE*, _IO_fpos_t, int));
198 extern _IO_ssize_t _IO_default_write __P((_IO_FILE*,const void*,_IO_ssize_t));
199 extern _IO_ssize_t _IO_default_read __P((_IO_FILE*, void*, _IO_ssize_t));
200 extern int _IO_default_stat __P((_IO_FILE*, void*));
201 extern _IO_fpos_t _IO_default_seek __P((_IO_FILE*, _IO_off_t, int));
202 extern int _IO_default_sync __P((_IO_FILE*));
203 #define _IO_default_close ((_IO_close_t)_IO_default_sync)
205 extern struct _IO_jump_t _IO_file_jumps;
206 extern struct _IO_jump_t _IO_streambuf_jumps;
207 extern struct _IO_jump_t _IO_proc_jumps;
208 extern struct _IO_jump_t _IO_str_jumps;
209 extern int _IO_do_write __P((_IO_FILE*, const char*, _IO_size_t));
210 extern int _IO_flush_all __P((void));
211 extern void _IO_cleanup __P((void));
212 extern void _IO_flush_all_linebuffered __P((void));
214 #define _IO_do_flush(_f) \
215 _IO_do_write(_f, (_f)->_IO_write_base, \
216 (_f)->_IO_write_ptr-(_f)->_IO_write_base)
217 #define _IO_in_put_mode(_fp) ((_fp)->_flags & _IO_CURRENTLY_PUTTING)
218 #define _IO_mask_flags(fp, f, mask) \
219 ((fp)->_flags = ((fp)->_flags & ~(mask)) | ((f) & (mask)))
220 #define _IO_setg(fp, eb, g, eg) ((fp)->_IO_read_base = (eb),\
221 (fp)->_IO_read_ptr = (g), (fp)->_IO_read_end = (eg))
222 #define _IO_setp(__fp, __p, __ep) \
223 ((__fp)->_IO_write_base = (__fp)->_IO_write_ptr = __p, (__fp)->_IO_write_end = (__ep))
224 #define _IO_have_backup(fp) ((fp)->_IO_save_base != NULL)
225 #define _IO_in_backup(fp) ((fp)->_flags & _IO_IN_BACKUP)
226 #define _IO_have_markers(fp) ((fp)->_markers != NULL)
227 #define _IO_blen(p) ((fp)->_IO_buf_end - (fp)->_IO_buf_base)
229 /* Jumptable functions for files. */
231 extern int _IO_file_doallocate __P((_IO_FILE*));
232 extern _IO_FILE* _IO_file_setbuf __P((_IO_FILE *, char*, _IO_ssize_t));
233 extern _IO_fpos_t _IO_file_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
234 extern _IO_size_t _IO_file_xsputn __P((_IO_FILE*,const void*,_IO_size_t));
235 extern int _IO_file_stat __P((_IO_FILE*, void*));
236 extern int _IO_file_close __P((_IO_FILE*));
237 extern int _IO_file_underflow __P((_IO_FILE *));
238 extern int _IO_file_overflow __P((_IO_FILE *, int));
239 #define _IO_file_is_open(__fp) ((__fp)->_fileno >= 0)
240 extern void _IO_file_init __P((_IO_FILE*));
241 extern _IO_FILE* _IO_file_attach __P((_IO_FILE*, int));
242 extern _IO_FILE* _IO_file_fopen __P((_IO_FILE*, const char*, const char*));
243 extern _IO_ssize_t _IO_file_write __P((_IO_FILE*,const void*,_IO_ssize_t));
244 extern _IO_ssize_t _IO_file_read __P((_IO_FILE*, void*, _IO_ssize_t));
245 extern int _IO_file_sync __P((_IO_FILE*));
246 extern int _IO_file_close_it __P((_IO_FILE*));
247 extern _IO_fpos_t _IO_file_seek __P((_IO_FILE *, _IO_off_t, int));
248 extern void _IO_file_finish __P((_IO_FILE*));
250 /* Other file functions. */
251 extern _IO_FILE* _IO_file_attach __P((_IO_FILE *, int));
253 /* Jumptable functions for proc_files. */
254 extern _IO_FILE* _IO_proc_open __P((_IO_FILE*, const char*, const char *));
255 extern int _IO_proc_close __P((_IO_FILE*));
257 /* Jumptable functions for strfiles. */
258 extern int _IO_str_underflow __P((_IO_FILE*));
259 extern int _IO_str_overflow __P((_IO_FILE *, int));
260 extern int _IO_str_pbackfail __P((_IO_FILE*, int));
261 extern _IO_fpos_t _IO_str_seekoff __P((_IO_FILE*,_IO_off_t,int,int));
262 extern void _IO_str_finish __P ((_IO_FILE*));
264 /* Other strfile functions */
265 extern void _IO_str_init_static __P((_IO_FILE *, char*, int, char*));
266 extern void _IO_str_init_readonly __P((_IO_FILE *, const char*, int));
267 extern _IO_ssize_t _IO_str_count __P ((_IO_FILE*));
269 extern int _IO_vasprintf __P ((char **result_ptr, __const char *format,
270 _IO_va_list args));
271 extern int _IO_vdprintf __P ((int d, __const char *format, _IO_va_list arg));
272 extern int _IO_vsnprintf __P ((char *string, _IO_size_t maxlen,
273 __const char *format, _IO_va_list args));
276 extern _IO_size_t _IO_getline __P((_IO_FILE*,char*,_IO_size_t,int,int));
277 extern _IO_ssize_t _IO_getdelim __P((char**, _IO_size_t*, int, _IO_FILE*));
278 extern double _IO_strtod __P((const char *, char **));
279 extern char * _IO_dtoa __P((double __d, int __mode, int __ndigits,
280 int *__decpt, int *__sign, char **__rve));
281 extern int _IO_outfloat __P((double __value, _IO_FILE *__sb, int __type,
282 int __width, int __precision, int __flags,
283 int __sign_mode, int __fill));
285 extern _IO_FILE *_IO_list_all;
286 extern void (*_IO_cleanup_registration_needed) __P ((void));
288 #ifndef EOF
289 #define EOF (-1)
290 #endif
291 #ifndef NULL
292 #if !defined(__cplusplus) || defined(__GNUC__)
293 #define NULL ((void*)0)
294 #else
295 #define NULL (0)
296 #endif
297 #endif
299 #define FREE_BUF(_B) free(_B)
300 #define ALLOC_BUF(_S) (char*)malloc(_S)
302 #ifndef OS_FSTAT
303 #define OS_FSTAT fstat
304 #endif
305 struct stat;
306 extern _IO_ssize_t _IO_read __P((int, void*, _IO_size_t));
307 extern _IO_ssize_t _IO_write __P((int, const void*, _IO_size_t));
308 extern _IO_off_t _IO_lseek __P((int, _IO_off_t, int));
309 extern int _IO_close __P((int));
310 extern int _IO_fstat __P((int, struct stat *));
311 extern int _IO_vscanf __P((const char *, _IO_va_list));
313 /* Operations on _IO_fpos_t.
314 Normally, these are trivial, but we provide hooks for configurations
315 where an _IO_fpos_t is a struct.
316 Note that _IO_off_t must be an integral type. */
318 /* _IO_pos_BAD is an _IO_fpos_t value indicating error, unknown, or EOF. */
319 #ifndef _IO_pos_BAD
320 #define _IO_pos_BAD ((_IO_fpos_t)(-1))
321 #endif
322 /* _IO_pos_as_off converts an _IO_fpos_t value to an _IO_off_t value. */
323 #ifndef _IO_pos_as_off
324 #define _IO_pos_as_off(__pos) ((_IO_off_t)(__pos))
325 #endif
326 /* _IO_pos_adjust adjust an _IO_fpos_t by some number of bytes. */
327 #ifndef _IO_pos_adjust
328 #define _IO_pos_adjust(__pos, __delta) ((__pos) += (__delta))
329 #endif
330 /* _IO_pos_0 is an _IO_fpos_t value indicating beginning of file. */
331 #ifndef _IO_pos_0
332 #define _IO_pos_0 ((_IO_fpos_t)0)
333 #endif
335 #ifdef __cplusplus
337 #endif
339 #if _IO_UNIFIED_JUMPTABLES
340 #define _IO_FJUMP /* nothing */
341 #else
342 #define _IO_FJUMP &_IO_file_jumps,
343 #endif
344 /* check following! */
345 #define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
346 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, _IO_FJUMP FD}
349 /* VTABLE_LABEL defines NAME as of the CLASS class.
350 CNLENGTH is strlen(#CLASS). */
351 #ifdef __GNUC__
352 #if _G_VTABLE_LABEL_HAS_LENGTH
353 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
354 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CNLENGTH #CLASS);
355 #else
356 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
357 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CLASS);
358 #endif
359 #endif /* __GNUC__ */
361 #if !defined(builtinbuf_vtable) && defined(__cplusplus)
362 #ifdef __GNUC__
363 VTABLE_LABEL(builtinbuf_vtable, builtinbuf, 10)
364 #else
365 #if _G_VTABLE_LABEL_HAS_LENGTH
366 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##10builtinbuf
367 #else
368 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##builtinbuf
369 #endif
370 #endif
371 #endif /* !defined(builtinbuf_vtable) && defined(__cplusplus) */
373 #if defined(__STDC__) || defined(__cplusplus)
374 #define _IO_va_start(args, last) va_start(args, last)
375 #else
376 #define _IO_va_start(args, last) va_start(args)
377 #endif
379 extern struct _IO_fake_stdiobuf _IO_stdin_buf, _IO_stdout_buf, _IO_stderr_buf;
381 #if 1
382 #define COERCE_FILE(FILE) /* Nothing */
383 #else
384 /* This is part of the kludge for binary compatibility with old stdio. */
385 #define COERCE_FILE(FILE) \
386 (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) == _OLD_MAGIC_MASK \
387 && (FILE) = *(FILE**)&((int*)fp)[1])
388 #endif
390 #ifdef EINVAL
391 #define MAYBE_SET_EINVAL errno = EINVAL
392 #else
393 #define MAYBE_SET_EINVAL /* nothing */
394 #endif
396 #ifdef DEBUG
397 #define CHECK_FILE(FILE,RET) \
398 if ((FILE) == NULL) { MAYBE_SET_EINVAL; return RET; } \
399 else { COERCE_FILE(FILE); \
400 if (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
401 { errno = EINVAL; return RET; }}
402 #else
403 #define CHECK_FILE(FILE,RET) \
404 COERCE_FILE(FILE)
405 #endif