Sun Jan 21 00:55:25 1996 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / libio / libioP.h
bloba955151ade03f017f5a2e4e6443dd98da7dda517
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 _IO_size_t _IO_getline __P((_IO_FILE*,char*,_IO_size_t,int,int));
270 extern _IO_ssize_t _IO_getdelim __P((char**, _IO_size_t*, int, _IO_FILE*));
271 extern double _IO_strtod __P((const char *, char **));
272 extern char * _IO_dtoa __P((double __d, int __mode, int __ndigits,
273 int *__decpt, int *__sign, char **__rve));
274 extern int _IO_outfloat __P((double __value, _IO_FILE *__sb, int __type,
275 int __width, int __precision, int __flags,
276 int __sign_mode, int __fill));
278 extern _IO_FILE *_IO_list_all;
279 extern void (*_IO_cleanup_registration_needed) __P ((void));
281 #ifndef EOF
282 #define EOF (-1)
283 #endif
284 #ifndef NULL
285 #if !defined(__cplusplus) || defined(__GNUC__)
286 #define NULL ((void*)0)
287 #else
288 #define NULL (0)
289 #endif
290 #endif
292 #define FREE_BUF(_B) free(_B)
293 #define ALLOC_BUF(_S) (char*)malloc(_S)
295 #ifndef OS_FSTAT
296 #define OS_FSTAT fstat
297 #endif
298 struct stat;
299 extern _IO_ssize_t _IO_read __P((int, void*, _IO_size_t));
300 extern _IO_ssize_t _IO_write __P((int, const void*, _IO_size_t));
301 extern _IO_off_t _IO_lseek __P((int, _IO_off_t, int));
302 extern int _IO_close __P((int));
303 extern int _IO_fstat __P((int, struct stat *));
305 /* Operations on _IO_fpos_t.
306 Normally, these are trivial, but we provide hooks for configurations
307 where an _IO_fpos_t is a struct.
308 Note that _IO_off_t must be an integral type. */
310 /* _IO_pos_BAD is an _IO_fpos_t value indicating error, unknown, or EOF. */
311 #ifndef _IO_pos_BAD
312 #define _IO_pos_BAD ((_IO_fpos_t)(-1))
313 #endif
314 /* _IO_pos_as_off converts an _IO_fpos_t value to an _IO_off_t value. */
315 #ifndef _IO_pos_as_off
316 #define _IO_pos_as_off(__pos) ((_IO_off_t)(__pos))
317 #endif
318 /* _IO_pos_adjust adjust an _IO_fpos_t by some number of bytes. */
319 #ifndef _IO_pos_adjust
320 #define _IO_pos_adjust(__pos, __delta) ((__pos) += (__delta))
321 #endif
322 /* _IO_pos_0 is an _IO_fpos_t value indicating beginning of file. */
323 #ifndef _IO_pos_0
324 #define _IO_pos_0 ((_IO_fpos_t)0)
325 #endif
327 #ifdef __cplusplus
329 #endif
331 #if _IO_UNIFIED_JUMPTABLES
332 #define _IO_FJUMP /* nothing */
333 #else
334 #define _IO_FJUMP &_IO_file_jumps,
335 #endif
336 /* check following! */
337 #define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
338 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, _IO_FJUMP FD}
341 /* VTABLE_LABEL defines NAME as of the CLASS class.
342 CNLENGTH is strlen(#CLASS). */
343 #ifdef __GNUC__
344 #if _G_VTABLE_LABEL_HAS_LENGTH
345 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
346 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CNLENGTH #CLASS);
347 #else
348 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
349 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CLASS);
350 #endif
351 #endif /* __GNUC__ */
353 #if !defined(builtinbuf_vtable) && defined(__cplusplus)
354 #ifdef __GNUC__
355 VTABLE_LABEL(builtinbuf_vtable, builtinbuf, 10)
356 #else
357 #if _G_VTABLE_LABEL_HAS_LENGTH
358 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##10builtinbuf
359 #else
360 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##builtinbuf
361 #endif
362 #endif
363 #endif /* !defined(builtinbuf_vtable) && defined(__cplusplus) */
365 #if defined(__STDC__) || defined(__cplusplus)
366 #define _IO_va_start(args, last) va_start(args, last)
367 #else
368 #define _IO_va_start(args, last) va_start(args)
369 #endif
371 extern struct _IO_fake_stdiobuf _IO_stdin_buf, _IO_stdout_buf, _IO_stderr_buf;
373 #if 1
374 #define COERCE_FILE(FILE) /* Nothing */
375 #else
376 /* This is part of the kludge for binary compatibility with old stdio. */
377 #define COERCE_FILE(FILE) \
378 (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) == _OLD_MAGIC_MASK \
379 && (FILE) = *(FILE**)&((int*)fp)[1])
380 #endif
382 #ifdef EINVAL
383 #define MAYBE_SET_EINVAL errno = EINVAL
384 #else
385 #define MAYBE_SET_EINVAL /* nothing */
386 #endif
388 #ifdef DEBUG
389 #define CHECK_FILE(FILE,RET) \
390 if ((FILE) == NULL) { MAYBE_SET_EINVAL; return RET; } \
391 else { COERCE_FILE(FILE); \
392 if (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
393 { errno = EINVAL; return RET; }}
394 #else
395 #define CHECK_FILE(FILE,RET) \
396 COERCE_FILE(FILE)
397 #endif