xref: /illumos-gate/usr/src/head/iso/stdio_iso.h (revision b4203d75)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24  * Copyright 2014 PALO, Richard.
25  *
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*	Copyright (c) 1988 AT&T	*/
31 /*	  All Rights Reserved	*/
32 
33 /*
34  * An application should not include this header directly.  Instead it
35  * should be included only through the inclusion of other Sun headers.
36  *
37  * The contents of this header is limited to identifiers specified in the
38  * C Standard.  Any new identifiers specified in future amendments to the
39  * C Standard must be placed in this header.  If these new identifiers
40  * are required to also be in the C++ Standard "std" namespace, then for
41  * anything other than macro definitions, corresponding "using" directives
42  * must also be added to <stdio.h>.
43  */
44 
45 /*
46  * User-visible pieces of the ANSI C standard I/O package.
47  */
48 
49 #ifndef _ISO_STDIO_ISO_H
50 #define	_ISO_STDIO_ISO_H
51 
52 #include <sys/feature_tests.h>
53 #include <sys/null.h>
54 #include <sys/va_list.h>
55 #include <stdio_tag.h>
56 #include <stdio_impl.h>
57 
58 /*
59  * If feature test macros are set that enable interfaces that use types
60  * defined in <sys/types.h>, get those types by doing the include.
61  *
62  * Note that in asking for the interfaces associated with this feature test
63  * macro one also asks for definitions of the POSIX types.
64  */
65 
66 #ifdef	__cplusplus
67 extern "C" {
68 #endif
69 
70 #if !defined(_LP64) && (_FILE_OFFSET_BITS == 64 || defined(_LARGEFILE64_SOURCE))
71 /*
72  * The following typedefs are adopted from ones in <sys/types.h> (with leading
73  * underscores added to avoid polluting the ANSI C name space).  See the
74  * commentary there for further explanation.
75  */
76 #if defined(_LONGLONG_TYPE)
77 typedef	long long	__longlong_t;
78 #else
79 /* used to reserve space and generate alignment */
80 typedef union {
81 	double	_d;
82 	int	_l[2];
83 } __longlong_t;
84 #endif
85 #endif  /* !_LP64 && _FILE_OFFSET_BITS == 64 || defined(_LARGEFILE64_SOURCE) */
86 
87 #if __cplusplus >= 199711L
88 namespace std {
89 #endif
90 
91 #if !defined(_FILEDEFED) || __cplusplus >= 199711L
92 #define	_FILEDEFED
93 typedef	__FILE FILE;
94 #endif
95 
96 #if !defined(_SIZE_T) || __cplusplus >= 199711L
97 #define	_SIZE_T
98 #if defined(_LP64) || defined(_I32LPx)
99 typedef unsigned long	size_t;		/* size of something in bytes */
100 #else
101 typedef unsigned int	size_t;		/* (historical version) */
102 #endif
103 #endif	/* !_SIZE_T */
104 
105 #if defined(_LP64) || _FILE_OFFSET_BITS == 32
106 typedef long		fpos_t;
107 #else
108 typedef	__longlong_t	fpos_t;
109 #endif
110 
111 #if __cplusplus >= 199711L
112 }
113 #endif /* end of namespace std */
114 
115 #define	BUFSIZ	1024
116 
117 /*
118  * The value of _NFILE is defined in the Processor Specific ABI.  The value
119  * is chosen for historical reasons rather than for truly processor related
120  * attribute.  Note that the SPARC Processor Specific ABI uses the common
121  * UNIX historical value of 20 so it is allowed to fall through.
122  */
123 #if defined(__i386)
124 #define	_NFILE	60	/* initial number of streams: Intel x86 ABI */
125 #else
126 #define	_NFILE	20	/* initial number of streams: SPARC ABI and default */
127 #endif
128 
129 #define	_SBFSIZ	8	/* compatibility with shared libs */
130 
131 #define	_IOFBF		0000	/* full buffered */
132 #define	_IOLBF		0100	/* line buffered */
133 #define	_IONBF		0004	/* not buffered */
134 #define	_IOEOF		0020	/* EOF reached on read */
135 #define	_IOERR		0040	/* I/O error from system */
136 
137 #define	_IOREAD		0001	/* currently reading */
138 #define	_IOWRT		0002	/* currently writing */
139 #define	_IORW		0200	/* opened for reading and writing */
140 #define	_IOMYBUF	0010	/* stdio malloc()'d buffer */
141 
142 #ifndef EOF
143 #define	EOF	(-1)
144 #endif
145 
146 #define	FOPEN_MAX	_NFILE
147 #define	FILENAME_MAX    1024	/* max # of characters in a path name */
148 
149 #define	SEEK_SET	0
150 #define	SEEK_CUR	1
151 #define	SEEK_END	2
152 #define	TMP_MAX		17576	/* 26 * 26 * 26 */
153 
154 #define	L_tmpnam	25	/* (sizeof(P_tmpdir) + 15) */
155 
156 extern __FILE	__iob[_NFILE];
157 #define	stdin	(&__iob[0])
158 #define	stdout	(&__iob[1])
159 #define	stderr	(&__iob[2])
160 
161 #if __cplusplus >= 199711L
162 namespace std {
163 #endif
164 
165 #if !defined(_LP64) && !defined(_LONGLONG_TYPE)
166 
167 #ifdef __PRAGMA_REDEFINE_EXTNAME
168 #pragma redefine_extname fprintf	_fprintf_c89
169 #pragma redefine_extname printf		_printf_c89
170 #pragma redefine_extname sprintf	_sprintf_c89
171 #pragma redefine_extname vfprintf	_vfprintf_c89
172 #pragma redefine_extname vprintf	_vprintf_c89
173 #pragma redefine_extname vsprintf	_vsprintf_c89
174 #pragma redefine_extname fscanf		_fscanf_c89
175 #pragma redefine_extname scanf		_scanf_c89
176 #pragma redefine_extname sscanf		_sscanf_c89
177 #else
178 #define	fprintf		_fprintf_c89
179 #define	printf		_printf_c89
180 #define	sprintf		_sprintf_c89
181 #define	vfprintf	_vfprintf_c89
182 #define	vprintf		_vprintf_c89
183 #define	vsprintf	_vsprintf_c89
184 #define	fscanf		_fscanf_c89
185 #define	scanf		_scanf_c89
186 #define	sscanf		_sscanf_c89
187 #endif
188 
189 #endif /* !defined(_LP64) && !defined(_LONGLONG_TYPE) */
190 
191 extern int	remove(const char *);
192 extern int	rename(const char *, const char *);
193 extern FILE	*tmpfile(void);
194 extern char	*tmpnam(char *);
195 extern int	fclose(FILE *);
196 extern int	fflush(FILE *);
197 extern FILE	*fopen(const char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD);
198 extern FILE	*freopen(const char *_RESTRICT_KYWD,
199 			const char *_RESTRICT_KYWD, FILE *_RESTRICT_KYWD);
200 extern void	setbuf(FILE *_RESTRICT_KYWD, char *_RESTRICT_KYWD);
201 extern int	setvbuf(FILE *_RESTRICT_KYWD, char *_RESTRICT_KYWD, int,
202 			size_t);
203 /* PRINTFLIKE2 */
204 extern int	fprintf(FILE *_RESTRICT_KYWD, const char *_RESTRICT_KYWD, ...);
205 /* SCANFLIKE2 */
206 extern int	fscanf(FILE *_RESTRICT_KYWD, const char *_RESTRICT_KYWD, ...);
207 /* PRINTFLIKE1 */
208 extern int	printf(const char *_RESTRICT_KYWD, ...);
209 /* SCANFLIKE1 */
210 extern int	scanf(const char *_RESTRICT_KYWD, ...);
211 /* PRINTFLIKE2 */
212 extern int	sprintf(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD, ...);
213 /* SCANFLIKE2 */
214 extern int	sscanf(const char *_RESTRICT_KYWD,
215 			const char *_RESTRICT_KYWD, ...);
216 extern int	vfprintf(FILE *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
217 			__va_list);
218 extern int	vprintf(const char *_RESTRICT_KYWD, __va_list);
219 extern int	vsprintf(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
220 			__va_list);
221 extern int	fgetc(FILE *);
222 extern char	*fgets(char *_RESTRICT_KYWD, int, FILE *_RESTRICT_KYWD);
223 extern int	fputc(int, FILE *);
224 extern int	fputs(const char *_RESTRICT_KYWD, FILE *_RESTRICT_KYWD);
225 #if (__cplusplus >= 199711L && (defined(_LP64) || defined(_REENTRANT))) || \
226 	__cplusplus < 199711L
227 extern int	getc(FILE *);
228 extern int	putc(int, FILE *);
229 #endif
230 #if (__cplusplus >= 199711L && defined(_REENTRANT)) || \
231 	__cplusplus < 199711L
232 extern int	getchar(void);
233 extern int	putchar(int);
234 #endif
235 
236 /*
237  * ISO/IEC C11 removed gets from the standard library. Therefore if a strict C11
238  * environment has been requested, we remove it.
239  */
240 #if !defined(_STDC_C11) || defined(__EXTENSIONS__)
241 extern char	*gets(char *);
242 #endif
243 extern int	puts(const char *);
244 extern int	ungetc(int, FILE *);
245 extern size_t	fread(void *_RESTRICT_KYWD, size_t, size_t,
246 	FILE *_RESTRICT_KYWD);
247 extern size_t	fwrite(const void *_RESTRICT_KYWD, size_t, size_t,
248 	FILE *_RESTRICT_KYWD);
249 #if !defined(__lint) || defined(_LP64) || _FILE_OFFSET_BITS == 32
250 extern int	fgetpos(FILE *_RESTRICT_KYWD, fpos_t *_RESTRICT_KYWD);
251 extern int	fsetpos(FILE *, const fpos_t *);
252 #endif
253 extern int	fseek(FILE *, long, int);
254 extern long	ftell(FILE *);
255 extern void	rewind(FILE *);
256 #if (__cplusplus >= 199711L && (defined(_LP64) || defined(_REENTRANT))) || \
257 	__cplusplus < 199711L
258 extern void	clearerr(FILE *);
259 extern int	feof(FILE *);
260 extern int	ferror(FILE *);
261 #endif
262 extern void	perror(const char *);
263 
264 #ifndef	_LP64
265 extern int	__filbuf(FILE *);
266 extern int	__flsbuf(int, FILE *);
267 #endif	/*	_LP64	*/
268 
269 #if __cplusplus >= 199711L
270 }
271 #endif /* end of namespace std */
272 
273 #if !defined(__lint)
274 
275 #if	!defined(_REENTRANT) && !defined(_LP64)
276 
277 #if __cplusplus >= 199711L
278 namespace std {
getc(FILE * _p)279 inline int getc(FILE *_p) {
280 	return (--_p->_cnt < 0 ? __filbuf(_p) : (int)*_p->_ptr++); }
putc(int _x,FILE * _p)281 inline int putc(int _x, FILE *_p) {
282 	return (--_p->_cnt < 0 ? __flsbuf(_x, _p)
283 		: (int)(*_p->_ptr++ = (unsigned char) _x)); }
284 }
285 #else /* __cplusplus >= 199711L */
286 #define	getc(p)		(--(p)->_cnt < 0 ? __filbuf(p) : (int)*(p)->_ptr++)
287 #define	putc(x, p)	(--(p)->_cnt < 0 ? __flsbuf((x), (p)) \
288 				: (int)(*(p)->_ptr++ = (unsigned char) (x)))
289 #endif /* __cplusplus >= 199711L */
290 
291 #endif /* !defined(_REENTRANT) && !defined(_LP64) */
292 
293 #ifndef	_REENTRANT
294 
295 #if __cplusplus >= 199711L
296 namespace std {
getchar()297 inline int getchar() { return getc(stdin); }
putchar(int _x)298 inline int putchar(int _x) { return putc(_x, stdout); }
299 }
300 #else
301 #define	getchar()	getc(stdin)
302 #define	putchar(x)	putc((x), stdout)
303 #endif /* __cplusplus >= 199711L */
304 
305 #ifndef	_LP64
306 #if __cplusplus >= 199711L
307 namespace std {
clearerr(FILE * _p)308 inline void clearerr(FILE *_p) { _p->_flag &= ~(_IOERR | _IOEOF); }
feof(FILE * _p)309 inline int feof(FILE *_p) { return _p->_flag & _IOEOF; }
ferror(FILE * _p)310 inline int ferror(FILE *_p) { return _p->_flag & _IOERR; }
311 }
312 #else /* __cplusplus >= 199711L */
313 #define	clearerr(p)	((void)((p)->_flag &= ~(_IOERR | _IOEOF)))
314 #define	feof(p)		((p)->_flag & _IOEOF)
315 #define	ferror(p)	((p)->_flag & _IOERR)
316 #endif /* __cplusplus >= 199711L */
317 #endif	/* _LP64 */
318 
319 #endif	/* _REENTRANT */
320 
321 #endif	/* !defined(__lint) */
322 
323 #ifdef	__cplusplus
324 }
325 #endif
326 
327 #endif	/* _ISO_STDIO_ISO_H */
328