xref: /titanic_52/usr/src/lib/libc/inc/stdiom.h (revision a574db851cdc636fc3939b68e80d79fe7fbd57f2)
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
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2159e63b6eSraf 
227c478bd9Sstevel@tonic-gate /*
23*a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * stdiom.h - shared guts of stdio
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef	_STDIOM_H
357c478bd9Sstevel@tonic-gate #define	_STDIOM_H
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9 */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <thread.h>
407c478bd9Sstevel@tonic-gate #include <synch.h>
417c478bd9Sstevel@tonic-gate #include <mtlib.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include "file64.h"
447c478bd9Sstevel@tonic-gate #include <wchar.h>
457c478bd9Sstevel@tonic-gate #include "mse.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * The following flags, and the macros that manipulate them, operate upon
507c478bd9Sstevel@tonic-gate  * the FILE structure used by stdio. If new flags are required, they should
517c478bd9Sstevel@tonic-gate  * be created in this file. The values of the flags must be differnt from
527c478bd9Sstevel@tonic-gate  * the currently used values. New macros should be created to use the flags
537c478bd9Sstevel@tonic-gate  * so that the compilation mode dependencies can be isolated here.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #ifdef _LP64
577c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0400
587c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	01000
597c478bd9Sstevel@tonic-gate #define	_IONOLOCK	02000
607c478bd9Sstevel@tonic-gate #define	_SEEKABLE	04000	/* is it seekable? */
617c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->_flag |= _IONOLOCK)
627c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->_flag &= ~_IONOLOCK)
637c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->_flag & _IONOLOCK)
647c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->_flag |= _BYTE_MODE_FLAG)
657c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->_flag &= ~_BYTE_MODE_FLAG)
667c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->_flag & _BYTE_MODE_FLAG)
677c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->_flag |= _WC_MODE_FLAG)
687c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->_flag &= ~_WC_MODE_FLAG)
697c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->_flag & _WC_MODE_FLAG)
707c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->_flag & \
717c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
727c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->_flag |=  _SEEKABLE)
737c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->_flag &= ~_SEEKABLE)
747c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->_flag &   _SEEKABLE)
757c478bd9Sstevel@tonic-gate #else
767c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0001
777c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	0002
787c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->__ionolock = 1)
797c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->__ionolock = 0)
807c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->__ionolock)
817c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->__orientation |= _BYTE_MODE_FLAG)
827c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->__orientation &= ~_BYTE_MODE_FLAG)
837c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->__orientation & _BYTE_MODE_FLAG)
847c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->__orientation |= _WC_MODE_FLAG)
857c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->__orientation &= ~_WC_MODE_FLAG)
867c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->__orientation & _WC_MODE_FLAG)
877c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->__orientation & \
887c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
897c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->__seekable = 1)
907c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->__seekable = 0)
917c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->__seekable)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Is iop a member of the _iob array? */
947c478bd9Sstevel@tonic-gate #define	STDIOP(iop)		((iop) >= &_iob[0] && (iop) < &_iob[_NFILE])
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* Compute the index of an _iob array member */
977c478bd9Sstevel@tonic-gate #define	IOPIND(iop)		((iop) - &_iob[0])
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate typedef unsigned char	Uchar;
1027c478bd9Sstevel@tonic-gate 
103*a574db85Sraf #define	_flockrel(rl)		cancel_safe_mutex_unlock(rl)
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	MAXVAL	(MAXINT - (MAXINT % BUFSIZ))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * The number of actual pushback characters is the value
1097c478bd9Sstevel@tonic-gate  * of PUSHBACK plus the first byte of the buffer. The FILE buffer must,
1107c478bd9Sstevel@tonic-gate  * for performance reasons, start on a word aligned boundry so the value
1117c478bd9Sstevel@tonic-gate  * of PUSHBACK should be a multiple of word.
1127c478bd9Sstevel@tonic-gate  * At least 4 bytes of PUSHBACK are needed. If sizeof (int) = 1 this breaks.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate #define	PUSHBACK	(((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* minimum buffer size must be at least 8 or shared library will break */
1177c478bd9Sstevel@tonic-gate #define	_SMBFSZ	(((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #if BUFSIZ == 1024
1207c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)	((SZ) & ~0x3ff)
1217c478bd9Sstevel@tonic-gate #elif BUFSIZ == 512
1227c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) & ~0x1ff)
1237c478bd9Sstevel@tonic-gate #else
1247c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) - (SZ % BUFSIZ))
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #undef _bufend
1287c478bd9Sstevel@tonic-gate #define	_bufend(iop)	_realbufend(iop)
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Internal data
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate extern Uchar _smbuf[][_SMBFSZ];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Internal routines from flush.c
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate extern void	__cleanup(void);
1407c478bd9Sstevel@tonic-gate extern void	_flushlbf(void);
1417c478bd9Sstevel@tonic-gate extern FILE	*_findiop(void);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * this is to be found in <stdio.h> for 32bit mode
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate #ifdef	_LP64
1477c478bd9Sstevel@tonic-gate extern int	__filbuf(FILE *);
1487c478bd9Sstevel@tonic-gate extern int	__flsbuf(int, FILE *);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Not needed as a function in 64 bit mode.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate #define	_realbufend(iop)	((iop)->_end)
1547c478bd9Sstevel@tonic-gate #else
1557c478bd9Sstevel@tonic-gate extern Uchar 	*_realbufend(FILE *iop);
1567c478bd9Sstevel@tonic-gate extern rmutex_t	*_reallock(FILE *iop);
1577c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate extern void	_setbufend(FILE *iop, Uchar *end);
1607c478bd9Sstevel@tonic-gate extern rmutex_t *_flockget(FILE *iop);
1617c478bd9Sstevel@tonic-gate extern int	_xflsbuf(FILE *iop);
1627c478bd9Sstevel@tonic-gate extern int	_wrtchk(FILE *iop);
1637c478bd9Sstevel@tonic-gate extern void	_bufsync(FILE *iop, Uchar *bufend);
1647c478bd9Sstevel@tonic-gate extern int	_fflush_u(FILE *iop);
1657c478bd9Sstevel@tonic-gate extern int	close_fd(FILE *iop);
1667c478bd9Sstevel@tonic-gate extern int	_doscan(FILE *, const char *, va_list);
1677c478bd9Sstevel@tonic-gate #ifdef	_LP64
1687c478bd9Sstevel@tonic-gate extern void	close_pid(void);
1697c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
172a5f69788Scraigm  * Internal routines from flush.c
173a5f69788Scraigm  */
174a5f69788Scraigm extern int _file_get(FILE *);
175a5f69788Scraigm extern int _file_set(FILE *, int, const char *);
176a5f69788Scraigm 
177a5f69788Scraigm /*
178a5f69788Scraigm  * Macros to aid the extended fd FILE work.
179a5f69788Scraigm  * This helps isolate the changes to only the 32-bit code
180a5f69788Scraigm  * since 64-bit Solaris is not affected by this.
181a5f69788Scraigm  */
182a5f69788Scraigm #ifdef  _LP64
183a5f69788Scraigm #define	GET_FD(iop)		((iop)->_file)
184a5f69788Scraigm #define	SET_FILE(iop, fd)	((iop)->_file = (fd))
185a5f69788Scraigm #else
186a5f69788Scraigm #define	GET_FD(iop)		\
187a5f69788Scraigm 		(((iop)->__extendedfd) ? _file_get(iop) : (iop)->_magic)
188a5f69788Scraigm #define	SET_FILE(iop, fd)	(iop)->_magic = (fd); (iop)->__extendedfd = 0
189a5f69788Scraigm #endif
190a5f69788Scraigm 
191a5f69788Scraigm /*
192a5f69788Scraigm  * Maximum size of the file descriptor stored in the FILE structure.
193a5f69788Scraigm  */
194a5f69788Scraigm 
195a5f69788Scraigm #ifdef _LP64
196a5f69788Scraigm #define	_FILE_FD_MAX	INT_MAX
197a5f69788Scraigm #else
198a5f69788Scraigm #define	_FILE_FD_MAX	255
199a5f69788Scraigm #endif
200a5f69788Scraigm 
201a5f69788Scraigm /*
2027c478bd9Sstevel@tonic-gate  * Internal routines from fileno.c
2037c478bd9Sstevel@tonic-gate  */
20459e63b6eSraf extern int _fileno(FILE *iop);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Internal routines from _findbuf.c
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate extern Uchar 	*_findbuf(FILE *iop);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Internal routine used by fopen.c
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate extern	FILE	*_endopen(const char *, const char *, FILE *, int);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
21759e63b6eSraf  * Internal routine from fwrite.c
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate extern size_t _fwrite_unlocked(const void *, size_t, size_t, FILE *);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Internal routine from getc.c
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate int _getc_unlocked(FILE *);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Internal routine from put.c
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate int _putc_unlocked(int, FILE *);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Internal routine from ungetc.c
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate int _ungetc_unlocked(int, FILE *);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * The following macros improve performance of the stdio by reducing the
2387c478bd9Sstevel@tonic-gate  * number of calls to _bufsync and _wrtchk.  _needsync checks whether
2397c478bd9Sstevel@tonic-gate  * or not _bufsync needs to be called.  _WRTCHK has the same effect as
2407c478bd9Sstevel@tonic-gate  * _wrtchk, but often these functions have no effect, and in those cases
2417c478bd9Sstevel@tonic-gate  * the macros avoid the expense of calling the functions.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #define	_needsync(p, bufend)	((bufend - (p)->_ptr) < \
2457c478bd9Sstevel@tonic-gate 				    ((p)->_cnt < 0 ? 0 : (p)->_cnt))
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate #define	_WRTCHK(iop)	((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
2487c478bd9Sstevel@tonic-gate 			    (iop->_base == 0) ||  \
2497c478bd9Sstevel@tonic-gate 			    (iop->_ptr == iop->_base && iop->_cnt == 0 && \
2507c478bd9Sstevel@tonic-gate 			    !(iop->_flag & (_IONBF | _IOLBF)))) \
2517c478bd9Sstevel@tonic-gate 			? _wrtchk(iop) : 0)
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate #ifdef	_LP64
2547c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(&((iop)->_lock))
2557c478bd9Sstevel@tonic-gate #else
2567c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(STDIOP(iop) ? &_xftab[IOPIND(iop)]._lock \
2577c478bd9Sstevel@tonic-gate 					: _reallock(iop))
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate extern struct xFILEdata	_xftab[];
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate #endif	/* _STDIOM_H */
264