1 /* @(#)stdio.h	1.15 19/12/04 Copyright 2009-2019 J. Schilling */
2 /*
3  *	Abstraction from stdio.h
4  *
5  *	Copyright (c) 2009-2019 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef _SCHILY_STDIO_H
22 #define	_SCHILY_STDIO_H
23 #ifndef NO_SCHILY_STDIO_H	/* We #undef _SCHILY_STDIO_H later because */
24 				/* of the ill designed "hdrchk" program    */
25 
26 #ifndef	_SCHILY_MCONFIG_H
27 #include <schily/mconfig.h>
28 #endif
29 
30 #ifdef	INCL_MYSTDIO
31 #ifndef _INCL_MYSTDIO_H
32 #include <mystdio.h>
33 #define	_INCL_MYSTDIO_H
34 #endif
35 
36 #else	/* INCL_MYSTDIO */
37 
38 #ifndef _INCL_STDIO_H
39 #include <stdio.h>
40 #define	_INCL_STDIO_H
41 #endif
42 #endif	/* INCL_MYSTDIO */
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #ifdef	HAVE_LARGEFILES
49 /*
50  * If HAVE_LARGEFILES is defined, it is guaranteed that fseeko()/ftello()
51  * both are available.
52  */
53 #define	fseek	fseeko
54 #define	ftell	ftello
55 #else	/* !HAVE_LARGEFILES */
56 
57 /*
58  * If HAVE_LARGEFILES is not defined, we depend on specific tests for
59  * fseeko()/ftello() which must have been done before the tests for
60  * Large File support have been done.
61  * Note that this only works if the tests used below are really done before
62  * the Large File autoconf test is run. This is because autoconf does no
63  * clean testing but instead cumulatively modifes the envivonment used for
64  * testing.
65  */
66 #ifdef	HAVE_FSEEKO
67 #	define	fseek	fseeko
68 #endif
69 #ifdef	HAVE_FTELLO
70 #	define	ftell	ftello
71 #endif
72 #endif
73 
74 #if	!defined(HAVE_POPEN) && defined(HAVE__POPEN)
75 #define	popen(c, m)	_popen((c), (m))
76 #endif
77 
78 #if	!defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
79 #define	pclose(fp)	_pclose(fp)
80 #endif
81 
82 #ifdef	FAST_GETC_PUTC
83 /*
84  * The following code partially allows libschily to access FILE * as fast as
85  * from inside libc on Solaris.
86  * This makes it possible to implement js_printf() from libschily aprox.
87  * 33% faster than printf() from libc on Solaris. To do this, we
88  * partially unhide the FILE structure in a 64 bit environment on Solaris
89  * to allow to run putc_unlocked() as a marcro.
90  *
91  * If you believe you can do this on onther platforms, send a note.
92  */
93 #if	defined(__SVR4) && defined(__sun) && defined(_LP64) && \
94 	!defined(getc_unlocked)
95 
96 #ifndef	_SCHILY_TYPES_H
97 #include <schily/types.h>	/* Needed for ssize_t */
98 #endif
99 
100 /*
101  * This is how the 64 bit FILE * begins on Solaris.
102  */
103 struct SCHILY__FILE_TAG {
104 	unsigned char	*_ptr;	/* next character from/to here in buffer */
105 	unsigned char	*_base;	/* the buffer */
106 	unsigned char	*_end;	/* the end of the buffer */
107 	ssize_t		_cnt;	/* number of available characters in buffer */
108 };
109 
110 #define	__getc_unlocked(p)	(--(p)->_cnt < 0 \
111 					? __filbuf((FILE *)p) \
112 					: (int)*(p)->_ptr++)
113 
114 #define	getc_unlocked(p)	__getc_unlocked((struct SCHILY__FILE_TAG *)p)
115 
116 #define	__putc_unlocked(x, p)	(--(p)->_cnt < 0 \
117 					? __flsbuf((x), (FILE *)(p)) \
118 					: (int)(*(p)->_ptr++ = \
119 					(unsigned char) (x)))
120 
121 #define	putc_unlocked(x, p)	__putc_unlocked(x, (struct SCHILY__FILE_TAG *)p)
122 
123 extern int	__filbuf __PR((FILE *));
124 extern int	__flsbuf __PR((int, FILE *));
125 
126 #else	/* !defined(__SVR4) && defined(__sun) && defined(_LP64) */
127 #undef	FAST_GETC_PUTC
128 #endif	/* !defined(__SVR4) && defined(__sun) && defined(_LP64) */
129 #endif	/* FAST_GETC_PUTC */
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #else	/* !NO_SCHILY_STDIO_H */
136 #undef	_SCHILY_STDIO_H			/* #undef here to pass "hdrchk" */
137 #endif	/* NO_SCHILY_STDIO_H */
138 #endif	/* _SCHILY_STDIO_H */
139