xref: /titanic_52/usr/src/lib/libc/inc/stdiom.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * stdiom.h - shared guts of stdio
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifndef	_STDIOM_H
37*7c478bd9Sstevel@tonic-gate #define	_STDIOM_H
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9 */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <thread.h>
42*7c478bd9Sstevel@tonic-gate #include <synch.h>
43*7c478bd9Sstevel@tonic-gate #include <mtlib.h>
44*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
45*7c478bd9Sstevel@tonic-gate #include "file64.h"
46*7c478bd9Sstevel@tonic-gate #include <wchar.h>
47*7c478bd9Sstevel@tonic-gate #include "mse.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * The following flags, and the macros that manipulate them, operate upon
52*7c478bd9Sstevel@tonic-gate  * the FILE structure used by stdio. If new flags are required, they should
53*7c478bd9Sstevel@tonic-gate  * be created in this file. The values of the flags must be differnt from
54*7c478bd9Sstevel@tonic-gate  * the currently used values. New macros should be created to use the flags
55*7c478bd9Sstevel@tonic-gate  * so that the compilation mode dependencies can be isolated here.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #ifdef _LP64
59*7c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0400
60*7c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	01000
61*7c478bd9Sstevel@tonic-gate #define	_IONOLOCK	02000
62*7c478bd9Sstevel@tonic-gate #define	_SEEKABLE	04000	/* is it seekable? */
63*7c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->_flag |= _IONOLOCK)
64*7c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->_flag &= ~_IONOLOCK)
65*7c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->_flag & _IONOLOCK)
66*7c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->_flag |= _BYTE_MODE_FLAG)
67*7c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->_flag &= ~_BYTE_MODE_FLAG)
68*7c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->_flag & _BYTE_MODE_FLAG)
69*7c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->_flag |= _WC_MODE_FLAG)
70*7c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->_flag &= ~_WC_MODE_FLAG)
71*7c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->_flag & _WC_MODE_FLAG)
72*7c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->_flag & \
73*7c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
74*7c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->_flag |=  _SEEKABLE)
75*7c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->_flag &= ~_SEEKABLE)
76*7c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->_flag &   _SEEKABLE)
77*7c478bd9Sstevel@tonic-gate #else
78*7c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0001
79*7c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	0002
80*7c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->__ionolock = 1)
81*7c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->__ionolock = 0)
82*7c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->__ionolock)
83*7c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->__orientation |= _BYTE_MODE_FLAG)
84*7c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->__orientation &= ~_BYTE_MODE_FLAG)
85*7c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->__orientation & _BYTE_MODE_FLAG)
86*7c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->__orientation |= _WC_MODE_FLAG)
87*7c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->__orientation &= ~_WC_MODE_FLAG)
88*7c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->__orientation & _WC_MODE_FLAG)
89*7c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->__orientation & \
90*7c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
91*7c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->__seekable = 1)
92*7c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->__seekable = 0)
93*7c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->__seekable)
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /* Is iop a member of the _iob array? */
96*7c478bd9Sstevel@tonic-gate #define	STDIOP(iop)		((iop) >= &_iob[0] && (iop) < &_iob[_NFILE])
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /* Compute the index of an _iob array member */
99*7c478bd9Sstevel@tonic-gate #define	IOPIND(iop)		((iop) - &_iob[0])
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate #endif
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate typedef unsigned char	Uchar;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #define	_flockrel(rl)		rmutex_unlock(rl)
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate #define	MAXVAL	(MAXINT - (MAXINT % BUFSIZ))
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate  * The number of actual pushback characters is the value
111*7c478bd9Sstevel@tonic-gate  * of PUSHBACK plus the first byte of the buffer. The FILE buffer must,
112*7c478bd9Sstevel@tonic-gate  * for performance reasons, start on a word aligned boundry so the value
113*7c478bd9Sstevel@tonic-gate  * of PUSHBACK should be a multiple of word.
114*7c478bd9Sstevel@tonic-gate  * At least 4 bytes of PUSHBACK are needed. If sizeof (int) = 1 this breaks.
115*7c478bd9Sstevel@tonic-gate  */
116*7c478bd9Sstevel@tonic-gate #define	PUSHBACK	(((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /* minimum buffer size must be at least 8 or shared library will break */
119*7c478bd9Sstevel@tonic-gate #define	_SMBFSZ	(((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate #if BUFSIZ == 1024
122*7c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)	((SZ) & ~0x3ff)
123*7c478bd9Sstevel@tonic-gate #elif BUFSIZ == 512
124*7c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) & ~0x1ff)
125*7c478bd9Sstevel@tonic-gate #else
126*7c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) - (SZ % BUFSIZ))
127*7c478bd9Sstevel@tonic-gate #endif
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #undef _bufend
130*7c478bd9Sstevel@tonic-gate #define	_bufend(iop)	_realbufend(iop)
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate  * Internal data
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate extern Uchar _smbuf[][_SMBFSZ];
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate /*
139*7c478bd9Sstevel@tonic-gate  * Internal routines from flush.c
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate extern void	__cleanup(void);
142*7c478bd9Sstevel@tonic-gate extern void	_flushlbf(void);
143*7c478bd9Sstevel@tonic-gate extern FILE	*_findiop(void);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate  * this is to be found in <stdio.h> for 32bit mode
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
149*7c478bd9Sstevel@tonic-gate extern int	__filbuf(FILE *);
150*7c478bd9Sstevel@tonic-gate extern int	__flsbuf(int, FILE *);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * Not needed as a function in 64 bit mode.
154*7c478bd9Sstevel@tonic-gate  */
155*7c478bd9Sstevel@tonic-gate #define	_realbufend(iop)	((iop)->_end)
156*7c478bd9Sstevel@tonic-gate #else
157*7c478bd9Sstevel@tonic-gate extern Uchar 	*_realbufend(FILE *iop);
158*7c478bd9Sstevel@tonic-gate extern rmutex_t	*_reallock(FILE *iop);
159*7c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate extern void	_setbufend(FILE *iop, Uchar *end);
162*7c478bd9Sstevel@tonic-gate extern rmutex_t *_flockget(FILE *iop);
163*7c478bd9Sstevel@tonic-gate extern int	_xflsbuf(FILE *iop);
164*7c478bd9Sstevel@tonic-gate extern int	_wrtchk(FILE *iop);
165*7c478bd9Sstevel@tonic-gate extern void	_bufsync(FILE *iop, Uchar *bufend);
166*7c478bd9Sstevel@tonic-gate extern int	_fflush_u(FILE *iop);
167*7c478bd9Sstevel@tonic-gate extern int	close_fd(FILE *iop);
168*7c478bd9Sstevel@tonic-gate extern int	_doscan(FILE *, const char *, va_list);
169*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
170*7c478bd9Sstevel@tonic-gate extern void	close_pid(void);
171*7c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Internal routines from fileno.c
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate extern int _fileno_unlocked(FILE *iop);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate /*
179*7c478bd9Sstevel@tonic-gate  * Internal routines from _findbuf.c
180*7c478bd9Sstevel@tonic-gate  */
181*7c478bd9Sstevel@tonic-gate extern Uchar 	*_findbuf(FILE *iop);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * Internal routine used by fopen.c
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate extern	FILE	*_endopen(const char *, const char *, FILE *, int);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate /*
189*7c478bd9Sstevel@tonic-gate  * Internal routine from ferror.c
190*7c478bd9Sstevel@tonic-gate  */
191*7c478bd9Sstevel@tonic-gate extern int _ferror_unlocked(FILE *);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate  * Internal routine from ferror.c
195*7c478bd9Sstevel@tonic-gate  */
196*7c478bd9Sstevel@tonic-gate extern size_t _fwrite_unlocked(const void *, size_t, size_t, FILE *);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate /*
199*7c478bd9Sstevel@tonic-gate  * Internal routine from getc.c
200*7c478bd9Sstevel@tonic-gate  */
201*7c478bd9Sstevel@tonic-gate int _getc_unlocked(FILE *);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate /*
204*7c478bd9Sstevel@tonic-gate  * Internal routine from put.c
205*7c478bd9Sstevel@tonic-gate  */
206*7c478bd9Sstevel@tonic-gate int _putc_unlocked(int, FILE *);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate /*
209*7c478bd9Sstevel@tonic-gate  * Internal routine from ungetc.c
210*7c478bd9Sstevel@tonic-gate  */
211*7c478bd9Sstevel@tonic-gate int _ungetc_unlocked(int, FILE *);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate /*
214*7c478bd9Sstevel@tonic-gate  * The following macros improve performance of the stdio by reducing the
215*7c478bd9Sstevel@tonic-gate  * number of calls to _bufsync and _wrtchk.  _needsync checks whether
216*7c478bd9Sstevel@tonic-gate  * or not _bufsync needs to be called.  _WRTCHK has the same effect as
217*7c478bd9Sstevel@tonic-gate  * _wrtchk, but often these functions have no effect, and in those cases
218*7c478bd9Sstevel@tonic-gate  * the macros avoid the expense of calling the functions.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate #define	_needsync(p, bufend)	((bufend - (p)->_ptr) < \
222*7c478bd9Sstevel@tonic-gate 				    ((p)->_cnt < 0 ? 0 : (p)->_cnt))
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate #define	_WRTCHK(iop)	((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
225*7c478bd9Sstevel@tonic-gate 			    (iop->_base == 0) ||  \
226*7c478bd9Sstevel@tonic-gate 			    (iop->_ptr == iop->_base && iop->_cnt == 0 && \
227*7c478bd9Sstevel@tonic-gate 			    !(iop->_flag & (_IONBF | _IOLBF)))) \
228*7c478bd9Sstevel@tonic-gate 			? _wrtchk(iop) : 0)
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
231*7c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(&((iop)->_lock))
232*7c478bd9Sstevel@tonic-gate #else
233*7c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(STDIOP(iop) ? &_xftab[IOPIND(iop)]._lock \
234*7c478bd9Sstevel@tonic-gate 					: _reallock(iop))
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate extern struct xFILEdata	_xftab[];
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate #endif	/* _STDIOM_H */
241