xref: /illumos-gate/usr/src/lib/libc/inc/stdiom.h (revision 59e63b6e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*59e63b6eSraf 
237c478bd9Sstevel@tonic-gate /*
24*59e63b6eSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * stdiom.h - shared guts of stdio
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifndef	_STDIOM_H
367c478bd9Sstevel@tonic-gate #define	_STDIOM_H
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9 */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <thread.h>
417c478bd9Sstevel@tonic-gate #include <synch.h>
427c478bd9Sstevel@tonic-gate #include <mtlib.h>
437c478bd9Sstevel@tonic-gate #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include "file64.h"
457c478bd9Sstevel@tonic-gate #include <wchar.h>
467c478bd9Sstevel@tonic-gate #include "mse.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * The following flags, and the macros that manipulate them, operate upon
517c478bd9Sstevel@tonic-gate  * the FILE structure used by stdio. If new flags are required, they should
527c478bd9Sstevel@tonic-gate  * be created in this file. The values of the flags must be differnt from
537c478bd9Sstevel@tonic-gate  * the currently used values. New macros should be created to use the flags
547c478bd9Sstevel@tonic-gate  * so that the compilation mode dependencies can be isolated here.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef _LP64
587c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0400
597c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	01000
607c478bd9Sstevel@tonic-gate #define	_IONOLOCK	02000
617c478bd9Sstevel@tonic-gate #define	_SEEKABLE	04000	/* is it seekable? */
627c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->_flag |= _IONOLOCK)
637c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->_flag &= ~_IONOLOCK)
647c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->_flag & _IONOLOCK)
657c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->_flag |= _BYTE_MODE_FLAG)
667c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->_flag &= ~_BYTE_MODE_FLAG)
677c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->_flag & _BYTE_MODE_FLAG)
687c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->_flag |= _WC_MODE_FLAG)
697c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->_flag &= ~_WC_MODE_FLAG)
707c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->_flag & _WC_MODE_FLAG)
717c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->_flag & \
727c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
737c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->_flag |=  _SEEKABLE)
747c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->_flag &= ~_SEEKABLE)
757c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->_flag &   _SEEKABLE)
767c478bd9Sstevel@tonic-gate #else
777c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0001
787c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	0002
797c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->__ionolock = 1)
807c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->__ionolock = 0)
817c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->__ionolock)
827c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->__orientation |= _BYTE_MODE_FLAG)
837c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->__orientation &= ~_BYTE_MODE_FLAG)
847c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->__orientation & _BYTE_MODE_FLAG)
857c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->__orientation |= _WC_MODE_FLAG)
867c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->__orientation &= ~_WC_MODE_FLAG)
877c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->__orientation & _WC_MODE_FLAG)
887c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->__orientation & \
897c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
907c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->__seekable = 1)
917c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->__seekable = 0)
927c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->__seekable)
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Is iop a member of the _iob array? */
957c478bd9Sstevel@tonic-gate #define	STDIOP(iop)		((iop) >= &_iob[0] && (iop) < &_iob[_NFILE])
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* Compute the index of an _iob array member */
987c478bd9Sstevel@tonic-gate #define	IOPIND(iop)		((iop) - &_iob[0])
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate typedef unsigned char	Uchar;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	_flockrel(rl)		rmutex_unlock(rl)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	MAXVAL	(MAXINT - (MAXINT % BUFSIZ))
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * The number of actual pushback characters is the value
1107c478bd9Sstevel@tonic-gate  * of PUSHBACK plus the first byte of the buffer. The FILE buffer must,
1117c478bd9Sstevel@tonic-gate  * for performance reasons, start on a word aligned boundry so the value
1127c478bd9Sstevel@tonic-gate  * of PUSHBACK should be a multiple of word.
1137c478bd9Sstevel@tonic-gate  * At least 4 bytes of PUSHBACK are needed. If sizeof (int) = 1 this breaks.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate #define	PUSHBACK	(((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* minimum buffer size must be at least 8 or shared library will break */
1187c478bd9Sstevel@tonic-gate #define	_SMBFSZ	(((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #if BUFSIZ == 1024
1217c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)	((SZ) & ~0x3ff)
1227c478bd9Sstevel@tonic-gate #elif BUFSIZ == 512
1237c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) & ~0x1ff)
1247c478bd9Sstevel@tonic-gate #else
1257c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) - (SZ % BUFSIZ))
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #undef _bufend
1297c478bd9Sstevel@tonic-gate #define	_bufend(iop)	_realbufend(iop)
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Internal data
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate extern Uchar _smbuf[][_SMBFSZ];
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Internal routines from flush.c
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate extern void	__cleanup(void);
1417c478bd9Sstevel@tonic-gate extern void	_flushlbf(void);
1427c478bd9Sstevel@tonic-gate extern FILE	*_findiop(void);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * this is to be found in <stdio.h> for 32bit mode
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate #ifdef	_LP64
1487c478bd9Sstevel@tonic-gate extern int	__filbuf(FILE *);
1497c478bd9Sstevel@tonic-gate extern int	__flsbuf(int, FILE *);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * Not needed as a function in 64 bit mode.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate #define	_realbufend(iop)	((iop)->_end)
1557c478bd9Sstevel@tonic-gate #else
1567c478bd9Sstevel@tonic-gate extern Uchar 	*_realbufend(FILE *iop);
1577c478bd9Sstevel@tonic-gate extern rmutex_t	*_reallock(FILE *iop);
1587c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate extern void	_setbufend(FILE *iop, Uchar *end);
1617c478bd9Sstevel@tonic-gate extern rmutex_t *_flockget(FILE *iop);
1627c478bd9Sstevel@tonic-gate extern int	_xflsbuf(FILE *iop);
1637c478bd9Sstevel@tonic-gate extern int	_wrtchk(FILE *iop);
1647c478bd9Sstevel@tonic-gate extern void	_bufsync(FILE *iop, Uchar *bufend);
1657c478bd9Sstevel@tonic-gate extern int	_fflush_u(FILE *iop);
1667c478bd9Sstevel@tonic-gate extern int	close_fd(FILE *iop);
1677c478bd9Sstevel@tonic-gate extern int	_doscan(FILE *, const char *, va_list);
1687c478bd9Sstevel@tonic-gate #ifdef	_LP64
1697c478bd9Sstevel@tonic-gate extern void	close_pid(void);
1707c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Internal routines from fileno.c
1747c478bd9Sstevel@tonic-gate  */
175*59e63b6eSraf extern int _fileno(FILE *iop);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Internal routines from _findbuf.c
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate extern Uchar 	*_findbuf(FILE *iop);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Internal routine used by fopen.c
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate extern	FILE	*_endopen(const char *, const char *, FILE *, int);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
188*59e63b6eSraf  * Internal routine from fwrite.c
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate extern size_t _fwrite_unlocked(const void *, size_t, size_t, FILE *);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * Internal routine from getc.c
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate int _getc_unlocked(FILE *);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Internal routine from put.c
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate int _putc_unlocked(int, FILE *);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Internal routine from ungetc.c
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate int _ungetc_unlocked(int, FILE *);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * The following macros improve performance of the stdio by reducing the
2097c478bd9Sstevel@tonic-gate  * number of calls to _bufsync and _wrtchk.  _needsync checks whether
2107c478bd9Sstevel@tonic-gate  * or not _bufsync needs to be called.  _WRTCHK has the same effect as
2117c478bd9Sstevel@tonic-gate  * _wrtchk, but often these functions have no effect, and in those cases
2127c478bd9Sstevel@tonic-gate  * the macros avoid the expense of calling the functions.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate #define	_needsync(p, bufend)	((bufend - (p)->_ptr) < \
2167c478bd9Sstevel@tonic-gate 				    ((p)->_cnt < 0 ? 0 : (p)->_cnt))
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #define	_WRTCHK(iop)	((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
2197c478bd9Sstevel@tonic-gate 			    (iop->_base == 0) ||  \
2207c478bd9Sstevel@tonic-gate 			    (iop->_ptr == iop->_base && iop->_cnt == 0 && \
2217c478bd9Sstevel@tonic-gate 			    !(iop->_flag & (_IONBF | _IOLBF)))) \
2227c478bd9Sstevel@tonic-gate 			? _wrtchk(iop) : 0)
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #ifdef	_LP64
2257c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(&((iop)->_lock))
2267c478bd9Sstevel@tonic-gate #else
2277c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(STDIOP(iop) ? &_xftab[IOPIND(iop)]._lock \
2287c478bd9Sstevel@tonic-gate 					: _reallock(iop))
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate extern struct xFILEdata	_xftab[];
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #endif	/* _STDIOM_H */
235