xref: /illumos-gate/usr/src/lib/libc/inc/stdiom.h (revision d7269947)
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 /*
23a574db85Sraf  * 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	*/
28*d7269947SRobert Mustacchi /*	  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 #include <thread.h>
387c478bd9Sstevel@tonic-gate #include <synch.h>
397c478bd9Sstevel@tonic-gate #include <mtlib.h>
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #include "file64.h"
427c478bd9Sstevel@tonic-gate #include <wchar.h>
437c478bd9Sstevel@tonic-gate #include "mse.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * The following flags, and the macros that manipulate them, operate upon
487c478bd9Sstevel@tonic-gate  * the FILE structure used by stdio. If new flags are required, they should
497c478bd9Sstevel@tonic-gate  * be created in this file. The values of the flags must be differnt from
507c478bd9Sstevel@tonic-gate  * the currently used values. New macros should be created to use the flags
517c478bd9Sstevel@tonic-gate  * so that the compilation mode dependencies can be isolated here.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef _LP64
557c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0400
567c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	01000
577c478bd9Sstevel@tonic-gate #define	_IONOLOCK	02000
587c478bd9Sstevel@tonic-gate #define	_SEEKABLE	04000	/* is it seekable? */
597c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->_flag |= _IONOLOCK)
607c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->_flag &= ~_IONOLOCK)
617c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->_flag & _IONOLOCK)
627c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->_flag |= _BYTE_MODE_FLAG)
637c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->_flag &= ~_BYTE_MODE_FLAG)
647c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->_flag & _BYTE_MODE_FLAG)
657c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->_flag |= _WC_MODE_FLAG)
667c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->_flag &= ~_WC_MODE_FLAG)
677c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->_flag & _WC_MODE_FLAG)
687c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->_flag & \
697c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
707c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->_flag |=  _SEEKABLE)
717c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->_flag &= ~_SEEKABLE)
727c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->_flag &   _SEEKABLE)
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate #define	_BYTE_MODE_FLAG	0001
757c478bd9Sstevel@tonic-gate #define	_WC_MODE_FLAG	0002
767c478bd9Sstevel@tonic-gate #define	SET_IONOLOCK(iop)	((iop)->__ionolock = 1)
777c478bd9Sstevel@tonic-gate #define	CLEAR_IONOLOCK(iop)	((iop)->__ionolock = 0)
787c478bd9Sstevel@tonic-gate #define	GET_IONOLOCK(iop)	((iop)->__ionolock)
797c478bd9Sstevel@tonic-gate #define	SET_BYTE_MODE(iop)	((iop)->__orientation |= _BYTE_MODE_FLAG)
807c478bd9Sstevel@tonic-gate #define	CLEAR_BYTE_MODE(iop)	((iop)->__orientation &= ~_BYTE_MODE_FLAG)
817c478bd9Sstevel@tonic-gate #define	GET_BYTE_MODE(iop)	((iop)->__orientation & _BYTE_MODE_FLAG)
827c478bd9Sstevel@tonic-gate #define	SET_WC_MODE(iop)	((iop)->__orientation |= _WC_MODE_FLAG)
837c478bd9Sstevel@tonic-gate #define	CLEAR_WC_MODE(iop)	((iop)->__orientation &= ~_WC_MODE_FLAG)
847c478bd9Sstevel@tonic-gate #define	GET_WC_MODE(iop)	((iop)->__orientation & _WC_MODE_FLAG)
857c478bd9Sstevel@tonic-gate #define	GET_NO_MODE(iop)	(!((iop)->__orientation & \
867c478bd9Sstevel@tonic-gate 					(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
877c478bd9Sstevel@tonic-gate #define	SET_SEEKABLE(iop)	((iop)->__seekable = 1)
887c478bd9Sstevel@tonic-gate #define	CLEAR_SEEKABLE(iop)	((iop)->__seekable = 0)
897c478bd9Sstevel@tonic-gate #define	GET_SEEKABLE(iop)	((iop)->__seekable)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* Is iop a member of the _iob array? */
927c478bd9Sstevel@tonic-gate #define	STDIOP(iop)		((iop) >= &_iob[0] && (iop) < &_iob[_NFILE])
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Compute the index of an _iob array member */
957c478bd9Sstevel@tonic-gate #define	IOPIND(iop)		((iop) - &_iob[0])
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef unsigned char	Uchar;
1007c478bd9Sstevel@tonic-gate 
101a574db85Sraf #define	_flockrel(rl)		cancel_safe_mutex_unlock(rl)
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	MAXVAL	(MAXINT - (MAXINT % BUFSIZ))
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * The number of actual pushback characters is the value
1077c478bd9Sstevel@tonic-gate  * of PUSHBACK plus the first byte of the buffer. The FILE buffer must,
1087c478bd9Sstevel@tonic-gate  * for performance reasons, start on a word aligned boundry so the value
1097c478bd9Sstevel@tonic-gate  * of PUSHBACK should be a multiple of word.
1107c478bd9Sstevel@tonic-gate  * At least 4 bytes of PUSHBACK are needed. If sizeof (int) = 1 this breaks.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate #define	PUSHBACK	(((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* minimum buffer size must be at least 8 or shared library will break */
1157c478bd9Sstevel@tonic-gate #define	_SMBFSZ	(((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #if BUFSIZ == 1024
1187c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)	((SZ) & ~0x3ff)
1197c478bd9Sstevel@tonic-gate #elif BUFSIZ == 512
1207c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) & ~0x1ff)
1217c478bd9Sstevel@tonic-gate #else
1227c478bd9Sstevel@tonic-gate #define	MULTIBFSZ(SZ)    ((SZ) - (SZ % BUFSIZ))
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #undef _bufend
1267c478bd9Sstevel@tonic-gate #define	_bufend(iop)	_realbufend(iop)
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Internal data
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate extern Uchar _smbuf[][_SMBFSZ];
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Internal routines from flush.c
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate extern void	__cleanup(void);
1387c478bd9Sstevel@tonic-gate extern void	_flushlbf(void);
1397c478bd9Sstevel@tonic-gate extern FILE	*_findiop(void);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * this is to be found in <stdio.h> for 32bit mode
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate #ifdef	_LP64
1457c478bd9Sstevel@tonic-gate extern int	__filbuf(FILE *);
1467c478bd9Sstevel@tonic-gate extern int	__flsbuf(int, FILE *);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * Not needed as a function in 64 bit mode.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate #define	_realbufend(iop)	((iop)->_end)
1527c478bd9Sstevel@tonic-gate #else
153*d7269947SRobert Mustacchi extern Uchar	*_realbufend(FILE *iop);
1547c478bd9Sstevel@tonic-gate extern rmutex_t	*_reallock(FILE *iop);
1557c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate extern void	_setbufend(FILE *iop, Uchar *end);
1587c478bd9Sstevel@tonic-gate extern rmutex_t *_flockget(FILE *iop);
1597c478bd9Sstevel@tonic-gate extern int	_xflsbuf(FILE *iop);
1607c478bd9Sstevel@tonic-gate extern int	_wrtchk(FILE *iop);
1617c478bd9Sstevel@tonic-gate extern void	_bufsync(FILE *iop, Uchar *bufend);
1627c478bd9Sstevel@tonic-gate extern int	_fflush_u(FILE *iop);
1637c478bd9Sstevel@tonic-gate extern int	close_fd(FILE *iop);
1647c478bd9Sstevel@tonic-gate extern int	_doscan(FILE *, const char *, va_list);
1657c478bd9Sstevel@tonic-gate #ifdef	_LP64
1667c478bd9Sstevel@tonic-gate extern void	close_pid(void);
1677c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
1687c478bd9Sstevel@tonic-gate 
169a5f69788Scraigm /*
170a5f69788Scraigm  * Internal routines from flush.c
171a5f69788Scraigm  */
172a5f69788Scraigm extern int _file_get(FILE *);
173a5f69788Scraigm extern int _file_set(FILE *, int, const char *);
174a5f69788Scraigm 
175a5f69788Scraigm /*
176a5f69788Scraigm  * Macros to aid the extended fd FILE work.
177a5f69788Scraigm  * This helps isolate the changes to only the 32-bit code
178a5f69788Scraigm  * since 64-bit Solaris is not affected by this.
179a5f69788Scraigm  */
180a5f69788Scraigm #ifdef  _LP64
181a5f69788Scraigm #define	GET_FD(iop)		((iop)->_file)
182a5f69788Scraigm #define	SET_FILE(iop, fd)	((iop)->_file = (fd))
183a5f69788Scraigm #else
184a5f69788Scraigm #define	GET_FD(iop)		\
185a5f69788Scraigm 		(((iop)->__extendedfd) ? _file_get(iop) : (iop)->_magic)
186a5f69788Scraigm #define	SET_FILE(iop, fd)	(iop)->_magic = (fd); (iop)->__extendedfd = 0
187a5f69788Scraigm #endif
188a5f69788Scraigm 
189a5f69788Scraigm /*
190a5f69788Scraigm  * Maximum size of the file descriptor stored in the FILE structure.
191a5f69788Scraigm  */
192a5f69788Scraigm 
193a5f69788Scraigm #ifdef _LP64
194a5f69788Scraigm #define	_FILE_FD_MAX	INT_MAX
195a5f69788Scraigm #else
196a5f69788Scraigm #define	_FILE_FD_MAX	255
197a5f69788Scraigm #endif
198a5f69788Scraigm 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * Internal routines from fileno.c
2017c478bd9Sstevel@tonic-gate  */
20259e63b6eSraf extern int _fileno(FILE *iop);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Internal routines from _findbuf.c
2067c478bd9Sstevel@tonic-gate  */
207*d7269947SRobert Mustacchi extern Uchar	*_findbuf(FILE *iop);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Internal routine used by fopen.c
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate extern	FILE	*_endopen(const char *, const char *, FILE *, int);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
21559e63b6eSraf  * Internal routine from fwrite.c
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate extern size_t _fwrite_unlocked(const void *, size_t, size_t, FILE *);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Internal routine from getc.c
2217c478bd9Sstevel@tonic-gate  */
222*d7269947SRobert Mustacchi int _getc_internal(FILE *);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Internal routine from put.c
2267c478bd9Sstevel@tonic-gate  */
227*d7269947SRobert Mustacchi int _putc_internal(int, FILE *);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Internal routine from ungetc.c
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate int _ungetc_unlocked(int, FILE *);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * The following macros improve performance of the stdio by reducing the
2367c478bd9Sstevel@tonic-gate  * number of calls to _bufsync and _wrtchk.  _needsync checks whether
2377c478bd9Sstevel@tonic-gate  * or not _bufsync needs to be called.  _WRTCHK has the same effect as
2387c478bd9Sstevel@tonic-gate  * _wrtchk, but often these functions have no effect, and in those cases
2397c478bd9Sstevel@tonic-gate  * the macros avoid the expense of calling the functions.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #define	_needsync(p, bufend)	((bufend - (p)->_ptr) < \
2437c478bd9Sstevel@tonic-gate 				    ((p)->_cnt < 0 ? 0 : (p)->_cnt))
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #define	_WRTCHK(iop)	((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
2467c478bd9Sstevel@tonic-gate 			    (iop->_base == 0) ||  \
2477c478bd9Sstevel@tonic-gate 			    (iop->_ptr == iop->_base && iop->_cnt == 0 && \
2487c478bd9Sstevel@tonic-gate 			    !(iop->_flag & (_IONBF | _IOLBF)))) \
2497c478bd9Sstevel@tonic-gate 			? _wrtchk(iop) : 0)
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #ifdef	_LP64
2527c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(&((iop)->_lock))
2537c478bd9Sstevel@tonic-gate #else
2547c478bd9Sstevel@tonic-gate #define	IOB_LCK(iop)	(STDIOP(iop) ? &_xftab[IOPIND(iop)]._lock \
2557c478bd9Sstevel@tonic-gate 					: _reallock(iop))
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate extern struct xFILEdata	_xftab[];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate #endif	/* _STDIOM_H */
262