xref: /illumos-gate/usr/src/ucblib/libucb/inc/stdiom.h (revision 8c0b080c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 /*
40  * stdiom.h - shared guts of stdio therefore it doesn't need
41  * a surrounding #ifndef
42  */
43 
44 #ifndef _STDIOM_H
45 #define	_STDIOM_H
46 
47 typedef unsigned char	Uchar;
48 
49 #define	MAXVAL (MAXINT - (MAXINT % BUFSIZ))
50 
51 /*
52  * The number of actual pushback characters is the value
53  * of PUSHBACK plus the first byte of the buffer. The FILE buffer must,
54  * for performance reasons, start on a word aligned boundry so the value
55  * of PUSHBACK should be a multiple of word.
56  * At least 4 bytes of PUSHBACK are needed. If sizeof(int) = 1 this breaks.
57  */
58 
59 #define	PUSHBACK ((int)(((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int)))
60 
61 /* minimum buffer size must be at least 8 or shared library will break */
62 #define	_SMBFSZ (((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
63 
64 extern Uchar *_bufendtab[];
65 
66 #if BUFSIZ == 1024
67 #define	MULTIBFSZ(SZ)	((SZ) & ~0x3ff)
68 #elif BUFSIZ == 512
69 #define	MULTIBFSZ(SZ)    ((SZ) & ~0x1ff)
70 #else
71 #define	MULTIBFSZ(SZ)    ((SZ) - (SZ % BUFSIZ))
72 #endif
73 
74 #define	_bufend(iop)	_realbufend(iop)
75 #define	setbufend(iop, end)	_setbufend(iop, end)
76 
77 	/*
78 	 * Internal routines from _iob.c
79 	 */
80 extern void	_cleanup(void);
81 extern void	_flushlbf(void);
82 extern FILE	*_findiop(void);
83 extern Uchar 	*_realbufend(FILE *iop);
84 extern void	_setbufend(FILE *iop, Uchar *end);
85 extern int	_wrtchk(FILE *iop);
86 
87 	/*
88 	 * Internal routines from flush.c
89 	 */
90 extern void	_bufsync(FILE *iop, Uchar *bufend);
91 extern int	_xflsbuf(FILE *iop);
92 
93 	/*
94 	 * Internal routines from _findbuf.c
95 	 */
96 extern Uchar 	*_findbuf(FILE *iop);
97 
98 #ifndef _LP64
99 extern int _file_set(FILE *, int, const char *);
100 #define	SET_FILE(iop, fd)	(iop)->_magic = (fd); (iop)->__extendedfd = 0
101 #define	_FILE_FD_MAX		255
102 #endif
103 
104 /*
105  * The following macros improve performance of the stdio by reducing the
106  * number of calls to _bufsync and _wrtchk.  _needsync checks whether
107  * or not _bufsync needs to be called.  _WRTCHK has the same effect as
108  * _wrtchk, but often these functions have no effect, and in those cases
109  * the macros avoid the expense of calling the functions.
110  */
111 
112 #define	_needsync(p, bufend)	((bufend - (p)->_ptr) < \
113 				((p)->_cnt < 0 ? 0 : (p)->_cnt))
114 
115 #define	_WRTCHK(iop)	((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
116 				(iop->_base == 0) || \
117 				(iop->_ptr == iop->_base && iop->_cnt == 0 && \
118 				!(iop->_flag & (_IONBF | _IOLBF)))) \
119 			? _wrtchk(iop) : 0)
120 #endif /* _STDIOM_H */
121