update from main archive 961009
[glibc.git] / libio / libioP.h
blobd3c871fbebdc34115fce8262859a476ef29d8bef
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
29 #include <libc-lock.h>
31 #include "iolibio.h"
33 #if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(__cplusplus)
34 /* All known AIX compilers implement these things (but don't always
35 define __STDC__). The RISC/OS MIPS compiler defines these things
36 in SVR4 mode, but does not define __STDC__. */
38 #define AND ,
39 #define DEFUN(name, arglist, args) name(args)
40 #define DEFUN_VOID(name) name(void)
42 #else /* Not ANSI C. */
44 #define AND ;
45 #ifndef const /* some systems define it in header files for non-ansi mode */
46 #define const
47 #endif
48 #define DEFUN(name, arglist, args) name arglist args;
49 #define DEFUN_VOID(name) name()
50 #endif /* ANSI C. */
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
56 #define _IO_seek_set 0
57 #define _IO_seek_cur 1
58 #define _IO_seek_end 2
60 typedef int (*_IO_overflow_t) __P((_IO_FILE*, int));
61 typedef int (*_IO_underflow_t) __P((_IO_FILE*));
62 typedef _IO_size_t (*_IO_xsputn_t) __P((_IO_FILE*,const void*,_IO_size_t));
63 typedef _IO_size_t (*_IO_xsgetn_t) __P((_IO_FILE*, void*, _IO_size_t));
64 typedef _IO_ssize_t (*_IO_read_t) __P((_IO_FILE*, void*, _IO_ssize_t));
65 typedef _IO_ssize_t (*_IO_write_t) __P((_IO_FILE*,const void*,_IO_ssize_t));
66 typedef int (*_IO_stat_t) __P((_IO_FILE*, void*));
67 typedef _IO_fpos_t (*_IO_seek_t) __P((_IO_FILE*, _IO_off_t, int));
68 typedef int (*_IO_doallocate_t) __P((_IO_FILE*));
69 typedef int (*_IO_pbackfail_t) __P((_IO_FILE*, int));
70 typedef _IO_FILE* (*_IO_setbuf_t) __P((_IO_FILE*, char *, _IO_ssize_t));
71 typedef int (*_IO_sync_t) __P((_IO_FILE*));
72 typedef void (*_IO_finish_t) __P((_IO_FILE*)); /* finalize */
73 typedef int (*_IO_close_t) __P((_IO_FILE*)); /* finalize */
74 typedef _IO_fpos_t (*_IO_seekoff_t) __P((_IO_FILE*, _IO_off_t, int, int));
76 /* The _IO_seek_cur and _IO_seek_end options are not allowed. */
77 typedef _IO_fpos_t (*_IO_seekpos_t) __P((_IO_FILE*, _IO_fpos_t, int));
79 #if !_IO_UNIFIED_JUMPTABLES
80 #define _IO_JUMPS(THIS) (THIS)->_jumps
81 #else
82 #define _IO_JUMPS(THIS) ((struct _IO_FILE_plus*)(THIS))->vtable
83 #endif
85 #if !_IO_UNIFIED_JUMPTABLES
86 #define JUMP_FIELD(TYPE, NAME) TYPE NAME
87 #define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC(THIS)
88 #define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC(THIS, X1)
89 #define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC(THIS, X1, X2)
90 #define JUMP3(FUNC, THIS, X1, X2, X3) _IO_JUMPS(THIS)->FUNC(THIS, X1, X2, X3)
91 #define JUMP_INIT(NAME, VALUE) VALUE
92 #else
93 #define JUMP_FIELD(TYPE, NAME) struct { short delta1, delta2; TYPE pfn; } NAME
94 #define JUMP0(FUNC, THIS) _IO_JUMPS(THIS)->FUNC.pfn(THIS)
95 #define JUMP1(FUNC, THIS, X1) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1)
96 #define JUMP2(FUNC, THIS, X1, X2) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1, X2)
97 #define JUMP3(FUNC, THIS, X1,X2,X3) _IO_JUMPS(THIS)->FUNC.pfn(THIS, X1,X2, X3)
98 #define JUMP_INIT(NAME, VALUE) {0, 0, VALUE}
99 #endif
100 #define JUMP_INIT_DUMMY JUMP_INIT(dummy, 0)
102 #define _IO_FINISH(FP) JUMP0(__finish, FP)
103 #define _IO_OVERFLOW(FP, CH) JUMP1(__overflow, FP, CH)
104 #define _IO_UNDERFLOW(FP) JUMP0(__underflow, FP)
105 #define _IO_UFLOW(FP) JUMP0(__uflow, FP)
106 #define _IO_PBACKFAIL(FP, CH) JUMP1(__pbackfail, FP, CH)
107 #define _IO_XSPUTN(FP, DATA, N) JUMP2(__xsputn, FP, DATA, N)
108 #define _IO_XSGETN(FP, DATA, N) JUMP2(__xsgetn, FP, DATA, N)
109 #define _IO_SEEKOFF(FP, OFF, DIR, MODE) JUMP3(__seekoff, FP, OFF, DIR, MODE)
110 #define _IO_SEEKPOS(FP, POS, FLAGS) JUMP2(__seekpos, FP, POS, FLAGS)
111 #define _IO_SETBUF(FP, BUFFER, LENGTH) JUMP2(__setbuf, FP, BUFFER, LENGTH)
112 #define _IO_SYNC(FP) JUMP0(__sync, FP)
113 #define _IO_DOALLOCATE(FP) JUMP0(__doallocate, FP)
114 #define _IO_SYSREAD(FP, DATA, LEN) JUMP2(__read, FP, DATA, LEN)
115 #define _IO_SYSWRITE(FP, DATA, LEN) JUMP2(__write, FP, DATA, LEN)
116 #define _IO_SYSSEEK(FP, OFFSET, MODE) JUMP2(__seek, FP, OFFSET, MODE)
117 #define _IO_SYSCLOSE(FP) JUMP0(__close, FP)
118 #define _IO_SYSSTAT(FP, BUF) JUMP1(__stat, FP, BUF)
120 #define _IO_CHAR_TYPE char /* unsigned char ? */
121 #define _IO_INT_TYPE int
123 struct _IO_jump_t {
124 JUMP_FIELD(_G_size_t, __dummy);
125 JUMP_FIELD(_IO_finish_t, __finish);
126 JUMP_FIELD(_IO_overflow_t, __overflow);
127 JUMP_FIELD(_IO_underflow_t, __underflow);
128 JUMP_FIELD(_IO_underflow_t, __uflow);
129 JUMP_FIELD(_IO_pbackfail_t, __pbackfail);
130 /* showmany */
131 JUMP_FIELD(_IO_xsputn_t, __xsputn);
132 JUMP_FIELD(_IO_xsgetn_t, __xsgetn);
133 JUMP_FIELD(_IO_seekoff_t, __seekoff);
134 JUMP_FIELD(_IO_seekpos_t, __seekpos);
135 JUMP_FIELD(_IO_setbuf_t, __setbuf);
136 JUMP_FIELD(_IO_sync_t, __sync);
137 JUMP_FIELD(_IO_doallocate_t, __doallocate);
138 JUMP_FIELD(_IO_read_t, __read);
139 JUMP_FIELD(_IO_write_t, __write);
140 JUMP_FIELD(_IO_seek_t, __seek);
141 JUMP_FIELD(_IO_close_t, __close);
142 JUMP_FIELD(_IO_stat_t, __stat);
143 #if 0
144 get_column;
145 set_column;
146 #endif
149 /* We always allocate an extra word following an _IO_FILE.
150 This is for compatibility with C++ streambuf; the word can
151 be used to smash to a pointer to a virtual function table. */
153 struct _IO_FILE_plus {
154 _IO_FILE file;
155 #if _IO_UNIFIED_JUMPTABLES
156 const struct _IO_jump_t *vtable;
157 #else
158 const void *vtable;
159 #endif
162 /* Generic functions */
164 extern _IO_fpos_t _IO_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
165 extern _IO_fpos_t _IO_seekpos __P((_IO_FILE*, _IO_fpos_t, int));
167 extern int _IO_switch_to_get_mode __P((_IO_FILE*));
168 extern void _IO_init __P((_IO_FILE*, int));
169 extern int _IO_sputbackc __P((_IO_FILE*, int));
170 extern int _IO_sungetc __P((_IO_FILE*));
171 extern void _IO_un_link __P((_IO_FILE*));
172 extern void _IO_link_in __P((_IO_FILE *));
173 extern void _IO_doallocbuf __P((_IO_FILE*));
174 extern void _IO_unsave_markers __P((_IO_FILE*));
175 extern void _IO_setb __P((_IO_FILE*, char*, char*, int));
176 extern unsigned _IO_adjust_column __P((unsigned, const char *, int));
177 #define _IO_sputn(__fp, __s, __n) _IO_XSPUTN(__fp, __s, __n)
179 /* Marker-related function. */
181 extern void _IO_init_marker __P((struct _IO_marker *, _IO_FILE *));
182 extern void _IO_remove_marker __P((struct _IO_marker*));
183 extern int _IO_marker_difference __P((struct _IO_marker *, struct _IO_marker *));
184 extern int _IO_marker_delta __P((struct _IO_marker *));
185 extern int _IO_seekmark __P((_IO_FILE *, struct _IO_marker *, int));
187 /* Default jumptable functions. */
189 extern int _IO_default_underflow __P((_IO_FILE*));
190 extern int _IO_default_uflow __P((_IO_FILE*));
191 extern int _IO_default_doallocate __P((_IO_FILE*));
192 extern void _IO_default_finish __P((_IO_FILE *));
193 extern int _IO_default_pbackfail __P((_IO_FILE*, int));
194 extern _IO_FILE* _IO_default_setbuf __P((_IO_FILE *, char*, _IO_ssize_t));
195 extern _IO_size_t _IO_default_xsputn __P((_IO_FILE *, const void*, _IO_size_t));
196 extern _IO_size_t _IO_default_xsgetn __P((_IO_FILE *, void*, _IO_size_t));
197 extern _IO_fpos_t _IO_default_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
198 extern _IO_fpos_t _IO_default_seekpos __P((_IO_FILE*, _IO_fpos_t, int));
199 extern _IO_ssize_t _IO_default_write __P((_IO_FILE*,const void*,_IO_ssize_t));
200 extern _IO_ssize_t _IO_default_read __P((_IO_FILE*, void*, _IO_ssize_t));
201 extern int _IO_default_stat __P((_IO_FILE*, void*));
202 extern _IO_fpos_t _IO_default_seek __P((_IO_FILE*, _IO_off_t, int));
203 extern int _IO_default_sync __P((_IO_FILE*));
204 #define _IO_default_close ((_IO_close_t)_IO_default_sync)
206 extern struct _IO_jump_t _IO_file_jumps;
207 extern struct _IO_jump_t _IO_streambuf_jumps;
208 extern struct _IO_jump_t _IO_proc_jumps;
209 extern struct _IO_jump_t _IO_str_jumps;
210 extern int _IO_do_write __P((_IO_FILE*, const char*, _IO_size_t));
211 extern int _IO_flush_all __P((void));
212 extern void _IO_cleanup __P((void));
213 extern void _IO_flush_all_linebuffered __P((void));
215 #define _IO_do_flush(_f) \
216 _IO_do_write(_f, (_f)->_IO_write_base, \
217 (_f)->_IO_write_ptr-(_f)->_IO_write_base)
218 #define _IO_in_put_mode(_fp) ((_fp)->_flags & _IO_CURRENTLY_PUTTING)
219 #define _IO_mask_flags(fp, f, mask) \
220 ((fp)->_flags = ((fp)->_flags & ~(mask)) | ((f) & (mask)))
221 #define _IO_setg(fp, eb, g, eg) ((fp)->_IO_read_base = (eb),\
222 (fp)->_IO_read_ptr = (g), (fp)->_IO_read_end = (eg))
223 #define _IO_setp(__fp, __p, __ep) \
224 ((__fp)->_IO_write_base = (__fp)->_IO_write_ptr = __p, (__fp)->_IO_write_end = (__ep))
225 #define _IO_have_backup(fp) ((fp)->_IO_save_base != NULL)
226 #define _IO_in_backup(fp) ((fp)->_flags & _IO_IN_BACKUP)
227 #define _IO_have_markers(fp) ((fp)->_markers != NULL)
228 #define _IO_blen(p) ((fp)->_IO_buf_end - (fp)->_IO_buf_base)
230 /* Jumptable functions for files. */
232 extern int _IO_file_doallocate __P((_IO_FILE*));
233 extern _IO_FILE* _IO_file_setbuf __P((_IO_FILE *, char*, _IO_ssize_t));
234 extern _IO_fpos_t _IO_file_seekoff __P((_IO_FILE*, _IO_off_t, int, int));
235 extern _IO_size_t _IO_file_xsputn __P((_IO_FILE*,const void*,_IO_size_t));
236 extern int _IO_file_stat __P((_IO_FILE*, void*));
237 extern int _IO_file_close __P((_IO_FILE*));
238 extern int _IO_file_underflow __P((_IO_FILE *));
239 extern int _IO_file_overflow __P((_IO_FILE *, int));
240 #define _IO_file_is_open(__fp) ((__fp)->_fileno >= 0)
241 extern void _IO_file_init __P((_IO_FILE*));
242 extern _IO_FILE* _IO_file_attach __P((_IO_FILE*, int));
243 extern _IO_FILE* _IO_file_fopen __P((_IO_FILE*, const char*, const char*));
244 extern _IO_ssize_t _IO_file_write __P((_IO_FILE*,const void*,_IO_ssize_t));
245 extern _IO_ssize_t _IO_file_read __P((_IO_FILE*, void*, _IO_ssize_t));
246 extern int _IO_file_sync __P((_IO_FILE*));
247 extern int _IO_file_close_it __P((_IO_FILE*));
248 extern _IO_fpos_t _IO_file_seek __P((_IO_FILE *, _IO_off_t, int));
249 extern void _IO_file_finish __P((_IO_FILE*));
251 /* Other file functions. */
252 extern _IO_FILE* _IO_file_attach __P((_IO_FILE *, int));
254 /* Jumptable functions for proc_files. */
255 extern _IO_FILE* _IO_proc_open __P((_IO_FILE*, const char*, const char *));
256 extern int _IO_proc_close __P((_IO_FILE*));
258 /* Jumptable functions for strfiles. */
259 extern int _IO_str_underflow __P((_IO_FILE*));
260 extern int _IO_str_overflow __P((_IO_FILE *, int));
261 extern int _IO_str_pbackfail __P((_IO_FILE*, int));
262 extern _IO_fpos_t _IO_str_seekoff __P((_IO_FILE*,_IO_off_t,int,int));
263 extern void _IO_str_finish __P ((_IO_FILE*));
265 /* Other strfile functions */
266 extern void _IO_str_init_static __P((_IO_FILE *, char*, int, char*));
267 extern void _IO_str_init_readonly __P((_IO_FILE *, const char*, int));
268 extern _IO_ssize_t _IO_str_count __P ((_IO_FILE*));
270 extern int _IO_vasprintf __P ((char **result_ptr, __const char *format,
271 _IO_va_list args));
272 extern int _IO_vdprintf __P ((int d, __const char *format, _IO_va_list arg));
273 extern int _IO_vsnprintf __P ((char *string, _IO_size_t maxlen,
274 __const char *format, _IO_va_list args));
277 extern _IO_size_t _IO_getline __P((_IO_FILE*,char*,_IO_size_t,int,int));
278 extern _IO_ssize_t _IO_getdelim __P((char**, _IO_size_t*, int, _IO_FILE*));
279 extern double _IO_strtod __P((const char *, char **));
280 extern char * _IO_dtoa __P((double __d, int __mode, int __ndigits,
281 int *__decpt, int *__sign, char **__rve));
282 extern int _IO_outfloat __P((double __value, _IO_FILE *__sb, int __type,
283 int __width, int __precision, int __flags,
284 int __sign_mode, int __fill));
286 extern _IO_FILE *_IO_list_all;
287 extern void (*_IO_cleanup_registration_needed) __P ((void));
289 #ifndef EOF
290 #define EOF (-1)
291 #endif
292 #ifndef NULL
293 #if !defined(__cplusplus) || defined(__GNUC__)
294 #define NULL ((void*)0)
295 #else
296 #define NULL (0)
297 #endif
298 #endif
300 #define FREE_BUF(_B) free(_B)
301 #define ALLOC_BUF(_S) (char*)malloc(_S)
303 #ifndef OS_FSTAT
304 #define OS_FSTAT fstat
305 #endif
306 struct stat;
307 extern _IO_ssize_t _IO_read __P((int, void*, _IO_size_t));
308 extern _IO_ssize_t _IO_write __P((int, const void*, _IO_size_t));
309 extern _IO_off_t _IO_lseek __P((int, _IO_off_t, int));
310 extern int _IO_close __P((int));
311 extern int _IO_fstat __P((int, struct stat *));
312 extern int _IO_vscanf __P((const char *, _IO_va_list));
314 /* Operations on _IO_fpos_t.
315 Normally, these are trivial, but we provide hooks for configurations
316 where an _IO_fpos_t is a struct.
317 Note that _IO_off_t must be an integral type. */
319 /* _IO_pos_BAD is an _IO_fpos_t value indicating error, unknown, or EOF. */
320 #ifndef _IO_pos_BAD
321 #define _IO_pos_BAD ((_IO_fpos_t)(-1))
322 #endif
323 /* _IO_pos_as_off converts an _IO_fpos_t value to an _IO_off_t value. */
324 #ifndef _IO_pos_as_off
325 #define _IO_pos_as_off(__pos) ((_IO_off_t)(__pos))
326 #endif
327 /* _IO_pos_adjust adjust an _IO_fpos_t by some number of bytes. */
328 #ifndef _IO_pos_adjust
329 #define _IO_pos_adjust(__pos, __delta) ((__pos) += (__delta))
330 #endif
331 /* _IO_pos_0 is an _IO_fpos_t value indicating beginning of file. */
332 #ifndef _IO_pos_0
333 #define _IO_pos_0 ((_IO_fpos_t)0)
334 #endif
336 #ifdef __cplusplus
338 #endif
340 #if _IO_UNIFIED_JUMPTABLES
341 #define _IO_FJUMP /* nothing */
342 #else
343 #define _IO_FJUMP &_IO_file_jumps,
344 #endif
345 #ifdef _IO_MTSAFE_IO
346 /* check following! */
347 #define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
348 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, _IO_FJUMP FD, \
350 0, 0, 0, 0, { 0 }, &_IO_stdfile_##FD##_lock }
351 #else
352 /* check following! */
353 #define FILEBUF_LITERAL(CHAIN, FLAGS, FD) \
354 { _IO_MAGIC+_IO_LINKED+_IO_IS_FILEBUF+FLAGS, \
355 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, CHAIN, _IO_FJUMP FD }
356 #endif
358 /* VTABLE_LABEL defines NAME as of the CLASS class.
359 CNLENGTH is strlen(#CLASS). */
360 #ifdef __GNUC__
361 #if _G_VTABLE_LABEL_HAS_LENGTH
362 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
363 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CNLENGTH #CLASS);
364 #else
365 #define VTABLE_LABEL(NAME, CLASS, CNLENGTH) \
366 extern char NAME[] asm (_G_VTABLE_LABEL_PREFIX #CLASS);
367 #endif
368 #endif /* __GNUC__ */
370 #if !defined(builtinbuf_vtable) && defined(__cplusplus)
371 #ifdef __GNUC__
372 VTABLE_LABEL(builtinbuf_vtable, builtinbuf, 10)
373 #else
374 #if _G_VTABLE_LABEL_HAS_LENGTH
375 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##10builtinbuf
376 #else
377 #define builtinbuf_vtable _G_VTABLE_LABEL_PREFIX_ID##builtinbuf
378 #endif
379 #endif
380 #endif /* !defined(builtinbuf_vtable) && defined(__cplusplus) */
382 #if defined(__STDC__) || defined(__cplusplus)
383 #define _IO_va_start(args, last) va_start(args, last)
384 #else
385 #define _IO_va_start(args, last) va_start(args)
386 #endif
388 extern struct _IO_fake_stdiobuf _IO_stdin_buf, _IO_stdout_buf, _IO_stderr_buf;
390 #if 1
391 #define COERCE_FILE(FILE) /* Nothing */
392 #else
393 /* This is part of the kludge for binary compatibility with old stdio. */
394 #define COERCE_FILE(FILE) \
395 (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) == _OLD_MAGIC_MASK \
396 && (FILE) = *(FILE**)&((int*)fp)[1])
397 #endif
399 #ifdef EINVAL
400 #define MAYBE_SET_EINVAL __set_errno (EINVAL)
401 #else
402 #define MAYBE_SET_EINVAL /* nothing */
403 #endif
405 #ifdef DEBUG
406 #define CHECK_FILE(FILE,RET) \
407 if ((FILE) == NULL) { MAYBE_SET_EINVAL; return RET; } \
408 else { COERCE_FILE(FILE); \
409 if (((FILE)->_IO_file_flags & _IO_MAGIC_MASK) != _IO_MAGIC) \
410 { errno = EINVAL; return RET; }}
411 #else
412 #define CHECK_FILE(FILE,RET) \
413 COERCE_FILE(FILE)
414 #endif