1*c2c66affSColin Finck /* @(#)schilyio.h	2.31 16/11/06 Copyright 1986, 1995-2016 J. Schilling */
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck  *	Copyright (c) 1986, 1995-2016 J. Schilling
4*c2c66affSColin Finck  */
5*c2c66affSColin Finck /*
6*c2c66affSColin Finck  * The contents of this file are subject to the terms of the
7*c2c66affSColin Finck  * Common Development and Distribution License, Version 1.0 only
8*c2c66affSColin Finck  * (the "License").  You may not use this file except in compliance
9*c2c66affSColin Finck  * with the License.
10*c2c66affSColin Finck  *
11*c2c66affSColin Finck  * See the file CDDL.Schily.txt in this distribution for details.
12*c2c66affSColin Finck  * A copy of the CDDL is also available via the Internet at
13*c2c66affSColin Finck  * http://www.opensource.org/licenses/cddl1.txt
14*c2c66affSColin Finck  *
15*c2c66affSColin Finck  * When distributing Covered Code, include this CDDL HEADER in each
16*c2c66affSColin Finck  * file and include the License file CDDL.Schily.txt from this distribution.
17*c2c66affSColin Finck  */
18*c2c66affSColin Finck 
19*c2c66affSColin Finck #ifndef	_STDIO_SCHILYIO_H
20*c2c66affSColin Finck #define	_STDIO_SCHILYIO_H
21*c2c66affSColin Finck 
22*c2c66affSColin Finck #include <schily/mconfig.h>
23*c2c66affSColin Finck #include <schily/stdio.h>
24*c2c66affSColin Finck #include <schily/standard.h>
25*c2c66affSColin Finck #include <schily/types.h>
26*c2c66affSColin Finck #include <schily/unistd.h>
27*c2c66affSColin Finck #include <schily/fcntl.h>
28*c2c66affSColin Finck #include <schily/schily.h>
29*c2c66affSColin Finck #include <schily/errno.h>
30*c2c66affSColin Finck 
31*c2c66affSColin Finck #ifdef	NO_USG_STDIO
32*c2c66affSColin Finck #	ifdef	HAVE_USG_STDIO
33*c2c66affSColin Finck #		undef	HAVE_USG_STDIO
34*c2c66affSColin Finck #	endif
35*c2c66affSColin Finck #endif
36*c2c66affSColin Finck 
37*c2c66affSColin Finck #ifdef	HAVE_LARGEFILES
38*c2c66affSColin Finck /*
39*c2c66affSColin Finck  * XXX We may need to put this code to a more global place to allow all
40*c2c66affSColin Finck  * XXX users of fseek()/ftell() to automaticaly use fseeko()/ftello()
41*c2c66affSColin Finck  * XXX if the latter are available.
42*c2c66affSColin Finck  *
43*c2c66affSColin Finck  * If HAVE_LARGEFILES is defined, it is guaranteed that fseeko()/ftello()
44*c2c66affSColin Finck  * both are available.
45*c2c66affSColin Finck  */
46*c2c66affSColin Finck #	define	fseek	fseeko
47*c2c66affSColin Finck #	define	ftell	ftello
48*c2c66affSColin Finck 
49*c2c66affSColin Finck #else	/* !HAVE_LARGEFILES */
50*c2c66affSColin Finck /*
51*c2c66affSColin Finck  * If HAVE_LARGEFILES is not defined, we depend on specific tests for
52*c2c66affSColin Finck  * fseeko()/ftello() which must have been done before the tests for
53*c2c66affSColin Finck  * Large File support have been done.
54*c2c66affSColin Finck  * Note that this only works if the tests used below are really done before
55*c2c66affSColin Finck  * the Large File autoconf test is run. This is because autoconf does no
56*c2c66affSColin Finck  * clean testing but instead cumulatively modifes the envivonment used for
57*c2c66affSColin Finck  * testing.
58*c2c66affSColin Finck  */
59*c2c66affSColin Finck #ifdef	HAVE_FSEEKO
60*c2c66affSColin Finck #	define	fseek	fseeko
61*c2c66affSColin Finck #endif
62*c2c66affSColin Finck #ifdef	HAVE_FTELLO
63*c2c66affSColin Finck #	define	ftell	ftello
64*c2c66affSColin Finck #endif
65*c2c66affSColin Finck 
66*c2c66affSColin Finck #endif
67*c2c66affSColin Finck 
68*c2c66affSColin Finck /*
69*c2c66affSColin Finck  * speed things up...
70*c2c66affSColin Finck  */
71*c2c66affSColin Finck #ifndef	_OPENFD_SRC
72*c2c66affSColin Finck #ifdef	_openfd
73*c2c66affSColin Finck #undef	_openfd
74*c2c66affSColin Finck #endif
75*c2c66affSColin Finck #define	_openfd(name, omode)	(open(name, omode, (mode_t)0666))
76*c2c66affSColin Finck #endif
77*c2c66affSColin Finck 
78*c2c66affSColin Finck #define	DO_MYFLAG		/* use local flags */
79*c2c66affSColin Finck 
80*c2c66affSColin Finck /*
81*c2c66affSColin Finck  * Flags used during fileopen(), ... by _fcons()/ _cvmod()
82*c2c66affSColin Finck  */
83*c2c66affSColin Finck #define	FI_NONE		0x0000	/* no flags defined */
84*c2c66affSColin Finck 
85*c2c66affSColin Finck #define	FI_READ		0x0001	/* open for reading */
86*c2c66affSColin Finck #define	FI_WRITE	0x0002	/* open for writing */
87*c2c66affSColin Finck #define	FI_BINARY	0x0004	/* open in binary mode */
88*c2c66affSColin Finck #define	FI_APPEND	0x0008	/* append on each write */
89*c2c66affSColin Finck 
90*c2c66affSColin Finck #define	FI_CREATE	0x0010	/* create if nessecary */
91*c2c66affSColin Finck #define	FI_TRUNC	0x0020	/* truncate file on open */
92*c2c66affSColin Finck #define	FI_UNBUF	0x0080	/* dont't buffer io */
93*c2c66affSColin Finck #define	FI_CLOSE	0x1000	/* close file on error */
94*c2c66affSColin Finck 
95*c2c66affSColin Finck /*
96*c2c66affSColin Finck  * Additional Schily FILE * flags that are not present with the
97*c2c66affSColin Finck  * standard stdio implementation.
98*c2c66affSColin Finck  */
99*c2c66affSColin Finck #define	_JS_IONORAISE	01	/* do no raisecond() on errors */
100*c2c66affSColin Finck #define	_JS_IOUNBUF	02	/* do unbuffered i/o */
101*c2c66affSColin Finck 
102*c2c66affSColin Finck 
103*c2c66affSColin Finck #ifdef	DO_MYFLAG
104*c2c66affSColin Finck 
105*c2c66affSColin Finck struct _io_flags {
106*c2c66affSColin Finck 	FILE	*fl_io;		/* file pointer */
107*c2c66affSColin Finck 	struct _io_flags	/* pointer to next struct */
108*c2c66affSColin Finck 		*fl_next;	/* if more file pointer to same fd */
109*c2c66affSColin Finck 	int	fl_flags;	/* my flags */
110*c2c66affSColin Finck };
111*c2c66affSColin Finck 
112*c2c66affSColin Finck typedef	struct _io_flags _io_fl;
113*c2c66affSColin Finck 
114*c2c66affSColin Finck extern	int	_io_glflag;	/* global default flag */
115*c2c66affSColin Finck extern	_io_fl	*_io_myfl;	/* array of structs to hold my flags */
116*c2c66affSColin Finck extern	int	_fl_max;	/* max fd currently in _io_myfl */
117*c2c66affSColin Finck 
118*c2c66affSColin Finck /*
119*c2c66affSColin Finck  *	if fileno > max
120*c2c66affSColin Finck  *		expand
121*c2c66affSColin Finck  *	else if map[fileno].pointer == 0
122*c2c66affSColin Finck  *		return 0
123*c2c66affSColin Finck  *	else if map[fileno].pointer == p
124*c2c66affSColin Finck  *		return map[fileno].flags
125*c2c66affSColin Finck  *	else
126*c2c66affSColin Finck  *		search list
127*c2c66affSColin Finck  */
128*c2c66affSColin Finck #define	flp(p)		(&_io_myfl[fileno(p)])
129*c2c66affSColin Finck 
130*c2c66affSColin Finck #ifdef	MY_FLAG_IS_MACRO
131*c2c66affSColin Finck #define	my_flag(p)	((int)fileno(p) >= _fl_max ?			\
132*c2c66affSColin Finck 				_io_get_my_flag(p) :			\
133*c2c66affSColin Finck 			((flp(p)->fl_io == 0 || flp(p)->fl_io == p) ?	\
134*c2c66affSColin Finck 				flp(p)->fl_flags :			\
135*c2c66affSColin Finck 				_io_get_my_flag(p)))
136*c2c66affSColin Finck #else
137*c2c66affSColin Finck #define	my_flag(p)	_io_get_my_flag(p)
138*c2c66affSColin Finck #endif
139*c2c66affSColin Finck 
140*c2c66affSColin Finck #define	set_my_flag(p, v) _io_set_my_flag(p, v)
141*c2c66affSColin Finck #define	add_my_flag(p, v) _io_add_my_flag(p, v)
142*c2c66affSColin Finck 
143*c2c66affSColin Finck extern	int	_io_get_my_flag __PR((FILE *));
144*c2c66affSColin Finck extern	void	_io_set_my_flag __PR((FILE *, int));
145*c2c66affSColin Finck extern	void	_io_add_my_flag __PR((FILE *, int));
146*c2c66affSColin Finck 
147*c2c66affSColin Finck #else	/* DO_MYFLAG */
148*c2c66affSColin Finck 
149*c2c66affSColin Finck #define	my_flag(p)		_JS_IONORAISE	/* Always noraise */
150*c2c66affSColin Finck #define	set_my_flag(p, v)			/* Ignore */
151*c2c66affSColin Finck #define	add_my_flag(p, v)			/* Ignore */
152*c2c66affSColin Finck 
153*c2c66affSColin Finck #endif	/* DO_MYFLAG */
154*c2c66affSColin Finck 
155*c2c66affSColin Finck #if	defined(HAVE_USG_STDIO) || defined(FAST_GETC_PUTC)
156*c2c66affSColin Finck /*
157*c2c66affSColin Finck  * We are on a system with AT&T compatible stdio implementation
158*c2c66affSColin Finck  */
159*c2c66affSColin Finck 
160*c2c66affSColin Finck /*
161*c2c66affSColin Finck  * Use the right filbuf()/flsbuf() function.
162*c2c66affSColin Finck  */
163*c2c66affSColin Finck #ifdef	FAST_GETC_PUTC
164*c2c66affSColin Finck 
165*c2c66affSColin Finck #define	__c	(struct SCHILY__FILE_TAG *)
166*c2c66affSColin Finck 
167*c2c66affSColin Finck #ifndef	HAVE___FILBUF
168*c2c66affSColin Finck #define	HAVE___FILBUF
169*c2c66affSColin Finck #endif
170*c2c66affSColin Finck #endif
171*c2c66affSColin Finck #ifdef	HAVE___FILBUF
172*c2c66affSColin Finck #	define	usg_filbuf(fp)		__filbuf(fp)
173*c2c66affSColin Finck #	define	usg_flsbuf(c, fp)	__flsbuf(c, fp)
174*c2c66affSColin Finck /*
175*c2c66affSColin Finck  * Define prototypes to verify if our interface is right
176*c2c66affSColin Finck  */
177*c2c66affSColin Finck extern	int	__filbuf		__PR((FILE *));
178*c2c66affSColin Finck #else	/* !HAVE___FILBUF */
179*c2c66affSColin Finck #	ifdef	HAVE__FILBUF
180*c2c66affSColin Finck #	define	usg_filbuf(fp)		_filbuf(fp)
181*c2c66affSColin Finck #	define	usg_flsbuf(c, fp)	_flsbuf(c, fp)
182*c2c66affSColin Finck /*
183*c2c66affSColin Finck  * Define prototypes to verify if our interface is right
184*c2c66affSColin Finck  */
185*c2c66affSColin Finck extern	int	_filbuf			__PR((FILE *));
186*c2c66affSColin Finck #	else
187*c2c66affSColin Finck /*
188*c2c66affSColin Finck  * no filbuf() but this will not happen on USG_STDIO systems.
189*c2c66affSColin Finck  */
190*c2c66affSColin Finck #	endif
191*c2c66affSColin Finck #endif	/* !HAVE___FILBUF */
192*c2c66affSColin Finck /*
193*c2c66affSColin Finck  * Do not check this because flsbuf()'s 1st parameter may be
194*c2c66affSColin Finck  * int			SunOS
195*c2c66affSColin Finck  * unsigned int		Apollo
196*c2c66affSColin Finck  * unsigned char	HP-UX-11
197*c2c66affSColin Finck  *
198*c2c66affSColin Finck  * Note that the interface is now checked by autoconf.
199*c2c66affSColin Finck  */
200*c2c66affSColin Finck #else	/* !HAVE_USG_STDIO */
201*c2c66affSColin Finck /*
202*c2c66affSColin Finck  * If we are on a non USG system we cannot down file pointers
203*c2c66affSColin Finck  * and we do not know about the internals of the FILE structure.
204*c2c66affSColin Finck  */
205*c2c66affSColin Finck #undef	DO_DOWN
206*c2c66affSColin Finck #endif	/* !HAVE_USG_STDIO */
207*c2c66affSColin Finck 
208*c2c66affSColin Finck #ifndef	__c
209*c2c66affSColin Finck #define	__c
210*c2c66affSColin Finck #endif
211*c2c66affSColin Finck 
212*c2c66affSColin Finck #ifndef	DO_DOWN
213*c2c66affSColin Finck /*
214*c2c66affSColin Finck  *	No stream checking
215*c2c66affSColin Finck  */
216*c2c66affSColin Finck #define	down(f)
217*c2c66affSColin Finck #define	down1(f, fl1)
218*c2c66affSColin Finck #define	down2(f, fl1, fl2)
219*c2c66affSColin Finck #else
220*c2c66affSColin Finck /*
221*c2c66affSColin Finck  *	Do stream checking (works only on USG stdio)
222*c2c66affSColin Finck  *
223*c2c66affSColin Finck  *	New version of USG stdio.
224*c2c66affSColin Finck  *	_iob[] holds only a small amount of pointers.
225*c2c66affSColin Finck  *	Aditional space is allocated.
226*c2c66affSColin Finck  *	We may check only if the file pointer is != NULL
227*c2c66affSColin Finck  *	and if iop->_flag refers to a stream with appropriate modes.
228*c2c66affSColin Finck  *	If _iob[] gets expanded by malloc() we cannot check upper bound.
229*c2c66affSColin Finck  */
230*c2c66affSColin Finck #define	down(f)		((f) == 0 || (__c f)->_flag == 0 ? \
231*c2c66affSColin Finck 				(raisecond(_badfile, 0L), (FILE *)0) : (f))
232*c2c66affSColin Finck 
233*c2c66affSColin Finck #define	down1(f, fl1)	((f) == 0 || (__c f)->_flag == 0 ? \
234*c2c66affSColin Finck 					(raisecond(_badfile, 0L), (FILE *)0) : \
235*c2c66affSColin Finck 				(((__c f)->_flag & fl1) != fl1 ? \
236*c2c66affSColin Finck 					(raisecond(_badop, 0L), (FILE *)0) : \
237*c2c66affSColin Finck 					(f)))
238*c2c66affSColin Finck 
239*c2c66affSColin Finck #define	down2(f, fl1, fl2)	((f) == 0 || (__c f)->_flag == 0 ? \
240*c2c66affSColin Finck 				(raisecond(_badfile, 0L), (FILE *)0) : \
241*c2c66affSColin Finck 				    (((__c f)->_flag & fl1) != fl1 && \
242*c2c66affSColin Finck 				    ((__c f)->_flag & fl2)  != fl2 ? \
243*c2c66affSColin Finck 				(raisecond(_badop, 0L), (FILE *)0) : \
244*c2c66affSColin Finck 				(f)))
245*c2c66affSColin Finck #endif	/* DO_DOWN */
246*c2c66affSColin Finck 
247*c2c66affSColin Finck extern	char	_badfile[];
248*c2c66affSColin Finck extern	char	_badmode[];
249*c2c66affSColin Finck extern	char	_badop[];
250*c2c66affSColin Finck 
251*c2c66affSColin Finck #endif	/* _STDIO_SCHILYIO_H */
252