xref: /freebsd/include/stdio.h (revision 9240031ac655f4e3cd4bcf840f2efc5222d6fdbc)
159deaec5SRodney W. Grimes /*-
259deaec5SRodney W. Grimes  * Copyright (c) 1990, 1993
359deaec5SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
459deaec5SRodney W. Grimes  *
559deaec5SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
659deaec5SRodney W. Grimes  * Chris Torek.
759deaec5SRodney W. Grimes  *
859deaec5SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
959deaec5SRodney W. Grimes  * modification, are permitted provided that the following conditions
1059deaec5SRodney W. Grimes  * are met:
1159deaec5SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1259deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1359deaec5SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1459deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1559deaec5SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16f2556687SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1759deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1859deaec5SRodney W. Grimes  *    without specific prior written permission.
1959deaec5SRodney W. Grimes  *
2059deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2159deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2259deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2359deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2459deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2559deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2659deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2759deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2859deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2959deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3059deaec5SRodney W. Grimes  * SUCH DAMAGE.
3159deaec5SRodney W. Grimes  *
32161f948dSPeter Wemm  *	@(#)stdio.h	8.5 (Berkeley) 4/29/95
33a4add9a9SPeter Wemm  * $FreeBSD$
3459deaec5SRodney W. Grimes  */
3559deaec5SRodney W. Grimes 
3659deaec5SRodney W. Grimes #ifndef	_STDIO_H_
3759deaec5SRodney W. Grimes #define	_STDIO_H_
3859deaec5SRodney W. Grimes 
3959deaec5SRodney W. Grimes #include <sys/cdefs.h>
4012eb46c8SMarcel Moolenaar #include <sys/_null.h>
41abbd8902SMike Barcroft #include <sys/_types.h>
4215aa51b0SBruce Evans 
435dfca833SMike Barcroft typedef	__off_t		fpos_t;
445dfca833SMike Barcroft 
45abbd8902SMike Barcroft #ifndef _SIZE_T_DECLARED
46abbd8902SMike Barcroft typedef	__size_t	size_t;
47abbd8902SMike Barcroft #define	_SIZE_T_DECLARED
4859deaec5SRodney W. Grimes #endif
4959deaec5SRodney W. Grimes 
5069099ba2SDavid Schultz #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
5169099ba2SDavid Schultz #ifndef _OFF_T_DECLARED
5269099ba2SDavid Schultz #define	_OFF_T_DECLARED
5369099ba2SDavid Schultz typedef	__off_t		off_t;
5469099ba2SDavid Schultz #endif
5569099ba2SDavid Schultz #ifndef _SSIZE_T_DECLARED
5669099ba2SDavid Schultz #define	_SSIZE_T_DECLARED
5769099ba2SDavid Schultz typedef	__ssize_t	ssize_t;
5869099ba2SDavid Schultz #endif
5969099ba2SDavid Schultz #endif
6069099ba2SDavid Schultz 
615dfca833SMike Barcroft #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
625dfca833SMike Barcroft #ifndef _VA_LIST_DECLARED
635dfca833SMike Barcroft typedef	__va_list	va_list;
645dfca833SMike Barcroft #define	_VA_LIST_DECLARED
655dfca833SMike Barcroft #endif
665dfca833SMike Barcroft #endif
675dfca833SMike Barcroft 
6859deaec5SRodney W. Grimes #define	_FSTDIO			/* Define for new stdio with functions. */
6959deaec5SRodney W. Grimes 
7019e03ca8SJohn Baldwin /*
7119e03ca8SJohn Baldwin  * NB: to fit things in six character monocase externals, the stdio
7219e03ca8SJohn Baldwin  * code uses the prefix `__s' for stdio objects, typically followed
7319e03ca8SJohn Baldwin  * by a three-character attempt at a mnemonic.
7419e03ca8SJohn Baldwin  */
7519e03ca8SJohn Baldwin 
7619e03ca8SJohn Baldwin /* stdio buffers */
7719e03ca8SJohn Baldwin struct __sbuf {
7819e03ca8SJohn Baldwin 	unsigned char *_base;
7919e03ca8SJohn Baldwin 	int	_size;
8019e03ca8SJohn Baldwin };
8119e03ca8SJohn Baldwin 
8219e03ca8SJohn Baldwin /*
8319e03ca8SJohn Baldwin  * stdio state variables.
8419e03ca8SJohn Baldwin  *
8519e03ca8SJohn Baldwin  * The following always hold:
8619e03ca8SJohn Baldwin  *
8719e03ca8SJohn Baldwin  *	if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
8819e03ca8SJohn Baldwin  *		_lbfsize is -_bf._size, else _lbfsize is 0
8919e03ca8SJohn Baldwin  *	if _flags&__SRD, _w is 0
9019e03ca8SJohn Baldwin  *	if _flags&__SWR, _r is 0
9119e03ca8SJohn Baldwin  *
9219e03ca8SJohn Baldwin  * This ensures that the getc and putc macros (or inline functions) never
9319e03ca8SJohn Baldwin  * try to write or read from a file that is in `read' or `write' mode.
9419e03ca8SJohn Baldwin  * (Moreover, they can, and do, automatically switch from read mode to
9519e03ca8SJohn Baldwin  * write mode, and back, on "r+" and "w+" files.)
9619e03ca8SJohn Baldwin  *
9719e03ca8SJohn Baldwin  * _lbfsize is used only to make the inline line-buffered output stream
9819e03ca8SJohn Baldwin  * code as compact as possible.
9919e03ca8SJohn Baldwin  *
10019e03ca8SJohn Baldwin  * _ub, _up, and _ur are used when ungetc() pushes back more characters
10119e03ca8SJohn Baldwin  * than fit in the current _bf, or when ungetc() pushes back a character
10219e03ca8SJohn Baldwin  * that does not match the previous one in _bf.  When this happens,
10319e03ca8SJohn Baldwin  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
10419e03ca8SJohn Baldwin  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
10519e03ca8SJohn Baldwin  *
10619e03ca8SJohn Baldwin  * Certain members of __sFILE are accessed directly via macros or
10719e03ca8SJohn Baldwin  * inline functions.  To preserve ABI compat, these members must not
10819e03ca8SJohn Baldwin  * be disturbed.  These members are marked below with (*).
10919e03ca8SJohn Baldwin  */
110c9e7ce2fSDavid Chisnall struct __sFILE {
11119e03ca8SJohn Baldwin 	unsigned char *_p;	/* (*) current position in (some) buffer */
11219e03ca8SJohn Baldwin 	int	_r;		/* (*) read space left for getc() */
11319e03ca8SJohn Baldwin 	int	_w;		/* (*) write space left for putc() */
11419e03ca8SJohn Baldwin 	short	_flags;		/* (*) flags, below; this FILE is free if 0 */
11519e03ca8SJohn Baldwin 	short	_file;		/* (*) fileno, if Unix descriptor, else -1 */
11641a5b871SJohn Baldwin 	struct	__sbuf _bf;	/* (*) the buffer (at least 1 byte, if !NULL) */
11719e03ca8SJohn Baldwin 	int	_lbfsize;	/* (*) 0 or -_bf._size, for inline putc */
11819e03ca8SJohn Baldwin 
11919e03ca8SJohn Baldwin 	/* operations */
120a1b482adSJohn Baldwin 	void	*_cookie;	/* (*) cookie passed to io functions */
12119e03ca8SJohn Baldwin 	int	(*_close)(void *);
12219e03ca8SJohn Baldwin 	int	(*_read)(void *, char *, int);
12319e03ca8SJohn Baldwin 	fpos_t	(*_seek)(void *, fpos_t, int);
12419e03ca8SJohn Baldwin 	int	(*_write)(void *, const char *, int);
12519e03ca8SJohn Baldwin 
12619e03ca8SJohn Baldwin 	/* separate buffer for long sequences of ungetc() */
12719e03ca8SJohn Baldwin 	struct	__sbuf _ub;	/* ungetc buffer */
12819e03ca8SJohn Baldwin 	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
12919e03ca8SJohn Baldwin 	int	_ur;		/* saved _r when _r is counting ungetc data */
13019e03ca8SJohn Baldwin 
13119e03ca8SJohn Baldwin 	/* tricks to meet minimum requirements even when malloc() fails */
13219e03ca8SJohn Baldwin 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
13319e03ca8SJohn Baldwin 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
13419e03ca8SJohn Baldwin 
13519e03ca8SJohn Baldwin 	/* separate buffer for fgetln() when line crosses buffer boundary */
13619e03ca8SJohn Baldwin 	struct	__sbuf _lb;	/* buffer for fgetln() */
13719e03ca8SJohn Baldwin 
13819e03ca8SJohn Baldwin 	/* Unix stdio files get aligned to block boundaries on fseek() */
13919e03ca8SJohn Baldwin 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
14019e03ca8SJohn Baldwin 	fpos_t	_offset;	/* current lseek offset */
14119e03ca8SJohn Baldwin 
14219e03ca8SJohn Baldwin 	struct pthread_mutex *_fl_mutex;	/* used for MT-safety */
14319e03ca8SJohn Baldwin 	struct pthread *_fl_owner;	/* current owner */
14419e03ca8SJohn Baldwin 	int	_fl_count;	/* recursive lock count */
14519e03ca8SJohn Baldwin 	int	_orientation;	/* orientation for fwide() */
14619e03ca8SJohn Baldwin 	__mbstate_t _mbstate;	/* multibyte conversion state */
147c9e7ce2fSDavid Chisnall };
148c9e7ce2fSDavid Chisnall #ifndef _STDFILE_DECLARED
149c9e7ce2fSDavid Chisnall #define _STDFILE_DECLARED
150c9e7ce2fSDavid Chisnall typedef struct __sFILE FILE;
151c9e7ce2fSDavid Chisnall #endif
15284bb9aaaSTim J. Robbins #ifndef _STDSTREAM_DECLARED
15359deaec5SRodney W. Grimes __BEGIN_DECLS
1542b618987SPeter Wemm extern FILE *__stdinp;
1552b618987SPeter Wemm extern FILE *__stdoutp;
1562b618987SPeter Wemm extern FILE *__stderrp;
15759deaec5SRodney W. Grimes __END_DECLS
15884bb9aaaSTim J. Robbins #define	_STDSTREAM_DECLARED
15984bb9aaaSTim J. Robbins #endif
16059deaec5SRodney W. Grimes 
16119e03ca8SJohn Baldwin #define	__SLBF	0x0001		/* line buffered */
16219e03ca8SJohn Baldwin #define	__SNBF	0x0002		/* unbuffered */
16319e03ca8SJohn Baldwin #define	__SRD	0x0004		/* OK to read */
16419e03ca8SJohn Baldwin #define	__SWR	0x0008		/* OK to write */
16519e03ca8SJohn Baldwin 	/* RD and WR are never simultaneously asserted */
16619e03ca8SJohn Baldwin #define	__SRW	0x0010		/* open for reading & writing */
16719e03ca8SJohn Baldwin #define	__SEOF	0x0020		/* found EOF */
16819e03ca8SJohn Baldwin #define	__SERR	0x0040		/* found error */
16919e03ca8SJohn Baldwin #define	__SMBF	0x0080		/* _buf is from malloc */
17019e03ca8SJohn Baldwin #define	__SAPP	0x0100		/* fdopen()ed in append mode */
17119e03ca8SJohn Baldwin #define	__SSTR	0x0200		/* this is an sprintf/snprintf string */
17219e03ca8SJohn Baldwin #define	__SOPT	0x0400		/* do fseek() optimization */
17319e03ca8SJohn Baldwin #define	__SNPT	0x0800		/* do not do fseek() optimization */
17419e03ca8SJohn Baldwin #define	__SOFF	0x1000		/* set iff _offset is in fact correct */
17519e03ca8SJohn Baldwin #define	__SMOD	0x2000		/* true => fgetln modified _p text */
17619e03ca8SJohn Baldwin #define	__SALC	0x4000		/* allocate string space dynamically */
17719e03ca8SJohn Baldwin #define	__SIGN	0x8000		/* ignore this file in _fwalk */
17819e03ca8SJohn Baldwin 
17959deaec5SRodney W. Grimes /*
18059deaec5SRodney W. Grimes  * The following three definitions are for ANSI C, which took them
18159deaec5SRodney W. Grimes  * from System V, which brilliantly took internal interface macros and
18259deaec5SRodney W. Grimes  * made them official arguments to setvbuf(), without renaming them.
18359deaec5SRodney W. Grimes  * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
18419e03ca8SJohn Baldwin  *
18519e03ca8SJohn Baldwin  * Although numbered as their counterparts above, the implementation
18619e03ca8SJohn Baldwin  * does not rely on this.
18759deaec5SRodney W. Grimes  */
18859deaec5SRodney W. Grimes #define	_IOFBF	0		/* setvbuf should set fully buffered */
18959deaec5SRodney W. Grimes #define	_IOLBF	1		/* setvbuf should set line buffered */
19059deaec5SRodney W. Grimes #define	_IONBF	2		/* setvbuf should set unbuffered */
19159deaec5SRodney W. Grimes 
19259deaec5SRodney W. Grimes #define	BUFSIZ	1024		/* size of buffer used by setbuf */
19359deaec5SRodney W. Grimes #define	EOF	(-1)
19459deaec5SRodney W. Grimes 
19559deaec5SRodney W. Grimes /*
19659deaec5SRodney W. Grimes  * FOPEN_MAX is a minimum maximum, and is the number of streams that
19759deaec5SRodney W. Grimes  * stdio can provide without attempting to allocate further resources
19859deaec5SRodney W. Grimes  * (which could fail).  Do not use this for anything.
19959deaec5SRodney W. Grimes  */
20059deaec5SRodney W. Grimes 				/* must be == _POSIX_STREAM_MAX <limits.h> */
2012c2f4a60SJohn Birrell #ifndef FOPEN_MAX
20259deaec5SRodney W. Grimes #define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
2032c2f4a60SJohn Birrell #endif
20459deaec5SRodney W. Grimes #define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
20559deaec5SRodney W. Grimes 
20659deaec5SRodney W. Grimes /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
207702b2a72SGarrett Wollman #if __XSI_VISIBLE
208c8fb747dSXin LI #define	P_tmpdir	"/tmp/"
20959deaec5SRodney W. Grimes #endif
21059deaec5SRodney W. Grimes #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
21159deaec5SRodney W. Grimes #define	TMP_MAX		308915776
21259deaec5SRodney W. Grimes 
21359deaec5SRodney W. Grimes #ifndef SEEK_SET
21459deaec5SRodney W. Grimes #define	SEEK_SET	0	/* set file offset to offset */
21559deaec5SRodney W. Grimes #endif
21659deaec5SRodney W. Grimes #ifndef SEEK_CUR
21759deaec5SRodney W. Grimes #define	SEEK_CUR	1	/* set file offset to current plus offset */
21859deaec5SRodney W. Grimes #endif
21959deaec5SRodney W. Grimes #ifndef SEEK_END
22059deaec5SRodney W. Grimes #define	SEEK_END	2	/* set file offset to EOF plus offset */
22159deaec5SRodney W. Grimes #endif
22259deaec5SRodney W. Grimes 
2232b618987SPeter Wemm #define	stdin	__stdinp
2242b618987SPeter Wemm #define	stdout	__stdoutp
2252b618987SPeter Wemm #define	stderr	__stderrp
22659deaec5SRodney W. Grimes 
227702b2a72SGarrett Wollman __BEGIN_DECLS
2283ac9d659SDavid Chisnall #ifdef _XLOCALE_H_
2293ac9d659SDavid Chisnall #include <xlocale/_stdio.h>
2303ac9d659SDavid Chisnall #endif
23159deaec5SRodney W. Grimes /*
23259deaec5SRodney W. Grimes  * Functions defined in ANSI C standard.
23359deaec5SRodney W. Grimes  */
234bb28f3c2SWarner Losh void	 clearerr(FILE *);
235bb28f3c2SWarner Losh int	 fclose(FILE *);
236bb28f3c2SWarner Losh int	 feof(FILE *);
237bb28f3c2SWarner Losh int	 ferror(FILE *);
238bb28f3c2SWarner Losh int	 fflush(FILE *);
239bb28f3c2SWarner Losh int	 fgetc(FILE *);
24054e4e385SMike Barcroft int	 fgetpos(FILE * __restrict, fpos_t * __restrict);
24154e4e385SMike Barcroft char	*fgets(char * __restrict, int, FILE * __restrict);
24254e4e385SMike Barcroft FILE	*fopen(const char * __restrict, const char * __restrict);
24371a00a44SRobert Drehmel int	 fprintf(FILE * __restrict, const char * __restrict, ...);
244bb28f3c2SWarner Losh int	 fputc(int, FILE *);
24554e4e385SMike Barcroft int	 fputs(const char * __restrict, FILE * __restrict);
24654e4e385SMike Barcroft size_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
24754e4e385SMike Barcroft FILE	*freopen(const char * __restrict, const char * __restrict, FILE * __restrict);
24854e4e385SMike Barcroft int	 fscanf(FILE * __restrict, const char * __restrict, ...);
249bb28f3c2SWarner Losh int	 fseek(FILE *, long, int);
250bb28f3c2SWarner Losh int	 fsetpos(FILE *, const fpos_t *);
251bb28f3c2SWarner Losh long	 ftell(FILE *);
25254e4e385SMike Barcroft size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
253bb28f3c2SWarner Losh int	 getc(FILE *);
254bb28f3c2SWarner Losh int	 getchar(void);
255bb28f3c2SWarner Losh char	*gets(char *);
256bb28f3c2SWarner Losh void	 perror(const char *);
25771a00a44SRobert Drehmel int	 printf(const char * __restrict, ...);
258bb28f3c2SWarner Losh int	 putc(int, FILE *);
259bb28f3c2SWarner Losh int	 putchar(int);
260bb28f3c2SWarner Losh int	 puts(const char *);
261bb28f3c2SWarner Losh int	 remove(const char *);
262bb28f3c2SWarner Losh int	 rename(const char *, const char *);
263bb28f3c2SWarner Losh void	 rewind(FILE *);
26454e4e385SMike Barcroft int	 scanf(const char * __restrict, ...);
2653248d0a5SRobert Drehmel void	 setbuf(FILE * __restrict, char * __restrict);
2663248d0a5SRobert Drehmel int	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
26771a00a44SRobert Drehmel int	 sprintf(char * __restrict, const char * __restrict, ...);
26854e4e385SMike Barcroft int	 sscanf(const char * __restrict, const char * __restrict, ...);
269bb28f3c2SWarner Losh FILE	*tmpfile(void);
270bb28f3c2SWarner Losh char	*tmpnam(char *);
271bb28f3c2SWarner Losh int	 ungetc(int, FILE *);
272f8418db7SRobert Drehmel int	 vfprintf(FILE * __restrict, const char * __restrict,
273abbd8902SMike Barcroft 	    __va_list);
274abbd8902SMike Barcroft int	 vprintf(const char * __restrict, __va_list);
275f8418db7SRobert Drehmel int	 vsprintf(char * __restrict, const char * __restrict,
276abbd8902SMike Barcroft 	    __va_list);
277702b2a72SGarrett Wollman 
278702b2a72SGarrett Wollman #if __ISO_C_VISIBLE >= 1999
279f8418db7SRobert Drehmel int	 snprintf(char * __restrict, size_t, const char * __restrict,
280f8418db7SRobert Drehmel 	    ...) __printflike(3, 4);
281af1c9c0eSTim J. Robbins int	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
282af1c9c0eSTim J. Robbins 	    __scanflike(2, 0);
28354e4e385SMike Barcroft int	 vscanf(const char * __restrict, __va_list) __scanflike(1, 0);
284f8418db7SRobert Drehmel int	 vsnprintf(char * __restrict, size_t, const char * __restrict,
285abbd8902SMike Barcroft 	    __va_list) __printflike(3, 0);
28654e4e385SMike Barcroft int	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
2875dfca833SMike Barcroft 	    __scanflike(2, 0);
288702b2a72SGarrett Wollman #endif
28959deaec5SRodney W. Grimes 
29059deaec5SRodney W. Grimes /*
291702b2a72SGarrett Wollman  * Functions defined in all versions of POSIX 1003.1.
29259deaec5SRodney W. Grimes  */
2935dfca833SMike Barcroft #if __BSD_VISIBLE || __POSIX_VISIBLE <= 199506
294cdd337c2SEd Schouten #define	L_cuserid	17	/* size for cuserid(3); MAXLOGNAME, legacy */
2955dfca833SMike Barcroft #endif
2968c372bd8SWolfram Schneider 
2975dfca833SMike Barcroft #if __POSIX_VISIBLE
2988c372bd8SWolfram Schneider #define	L_ctermid	1024	/* size for ctermid(3); PATH_MAX */
29959deaec5SRodney W. Grimes 
300bb28f3c2SWarner Losh char	*ctermid(char *);
301bb28f3c2SWarner Losh FILE	*fdopen(int, const char *);
302bb28f3c2SWarner Losh int	 fileno(FILE *);
303702b2a72SGarrett Wollman #endif /* __POSIX_VISIBLE */
304702b2a72SGarrett Wollman 
305702b2a72SGarrett Wollman #if __POSIX_VISIBLE >= 199209
306702b2a72SGarrett Wollman int	 pclose(FILE *);
307702b2a72SGarrett Wollman FILE	*popen(const char *, const char *);
308702b2a72SGarrett Wollman #endif
309702b2a72SGarrett Wollman 
310702b2a72SGarrett Wollman #if __POSIX_VISIBLE >= 199506
311bb28f3c2SWarner Losh int	 ftrylockfile(FILE *);
312bb28f3c2SWarner Losh void	 flockfile(FILE *);
313bb28f3c2SWarner Losh void	 funlockfile(FILE *);
314702b2a72SGarrett Wollman 
315702b2a72SGarrett Wollman /*
31619e03ca8SJohn Baldwin  * These are normally used through macros as defined below, but POSIX
31719e03ca8SJohn Baldwin  * requires functions as well.
318702b2a72SGarrett Wollman  */
319702b2a72SGarrett Wollman int	 getc_unlocked(FILE *);
320702b2a72SGarrett Wollman int	 getchar_unlocked(void);
321702b2a72SGarrett Wollman int	 putc_unlocked(int, FILE *);
322702b2a72SGarrett Wollman int	 putchar_unlocked(int);
323702b2a72SGarrett Wollman #endif
324e8065dc4STim J. Robbins #if __BSD_VISIBLE
325e8065dc4STim J. Robbins void	 clearerr_unlocked(FILE *);
326e8065dc4STim J. Robbins int	 feof_unlocked(FILE *);
327e8065dc4STim J. Robbins int	 ferror_unlocked(FILE *);
328e8065dc4STim J. Robbins int	 fileno_unlocked(FILE *);
329e8065dc4STim J. Robbins #endif
330702b2a72SGarrett Wollman 
331702b2a72SGarrett Wollman #if __POSIX_VISIBLE >= 200112
332abbd8902SMike Barcroft int	 fseeko(FILE *, __off_t, int);
333abbd8902SMike Barcroft __off_t	 ftello(FILE *);
334702b2a72SGarrett Wollman #endif
335702b2a72SGarrett Wollman 
336702b2a72SGarrett Wollman #if __BSD_VISIBLE || __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600
337702b2a72SGarrett Wollman int	 getw(FILE *);
338702b2a72SGarrett Wollman int	 putw(int, FILE *);
339702b2a72SGarrett Wollman #endif /* BSD or X/Open before issue 6 */
340702b2a72SGarrett Wollman 
341702b2a72SGarrett Wollman #if __XSI_VISIBLE
342702b2a72SGarrett Wollman char	*tempnam(const char *, const char *);
343702b2a72SGarrett Wollman #endif
344702b2a72SGarrett Wollman 
34569099ba2SDavid Schultz #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
34696c95412SPietro Cerutti FILE	*fmemopen(void * __restrict, size_t, const char * __restrict);
34769099ba2SDavid Schultz ssize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
34869099ba2SDavid Schultz 	    FILE * __restrict);
349*9240031aSJohn Baldwin FILE	*open_memstream(char **, size_t *);
35038953375SDavid Schultz int	 renameat(int, const char *, int, const char *);
351ad760e6fSDavid Schultz int	 vdprintf(int, const char * __restrict, __va_list);
35269099ba2SDavid Schultz 
35369099ba2SDavid Schultz /*
354ad760e6fSDavid Schultz  * Every programmer and his dog wrote functions called getline() and dprintf()
355ad760e6fSDavid Schultz  * before POSIX.1-2008 came along and decided to usurp the names, so we
356ad760e6fSDavid Schultz  * don't prototype them by default unless one of the following is true:
357ad760e6fSDavid Schultz  *   a) the app has requested them specifically by defining _WITH_GETLINE or
358ad760e6fSDavid Schultz  *      _WITH_DPRINTF, respectively
35969099ba2SDavid Schultz  *   b) the app has requested a POSIX.1-2008 environment via _POSIX_C_SOURCE
36069099ba2SDavid Schultz  *   c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE
36169099ba2SDavid Schultz  */
36269099ba2SDavid Schultz #ifndef _WITH_GETLINE
36369099ba2SDavid Schultz #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
36469099ba2SDavid Schultz #define	_WITH_GETLINE
36569099ba2SDavid Schultz #elif defined(_POSIX_C_SOURCE)
36644bf9512SDavid Schultz #if _POSIX_C_SOURCE >= 200809
36769099ba2SDavid Schultz #define	_WITH_GETLINE
36869099ba2SDavid Schultz #endif
36969099ba2SDavid Schultz #endif
37069099ba2SDavid Schultz #endif
37169099ba2SDavid Schultz 
37269099ba2SDavid Schultz #ifdef _WITH_GETLINE
37369099ba2SDavid Schultz ssize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
37469099ba2SDavid Schultz #endif
37569099ba2SDavid Schultz 
376ad760e6fSDavid Schultz #ifndef _WITH_DPRINTF
377ad760e6fSDavid Schultz #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
378ad760e6fSDavid Schultz #define	_WITH_DPRINTF
379ad760e6fSDavid Schultz #elif defined(_POSIX_C_SOURCE)
38044bf9512SDavid Schultz #if _POSIX_C_SOURCE >= 200809
381ad760e6fSDavid Schultz #define	_WITH_DPRINTF
382ad760e6fSDavid Schultz #endif
383ad760e6fSDavid Schultz #endif
384ad760e6fSDavid Schultz #endif
385ad760e6fSDavid Schultz 
386ad760e6fSDavid Schultz #ifdef _WITH_DPRINTF
387bccfb078SDavid Schultz int	 (dprintf)(int, const char * __restrict, ...);
388ad760e6fSDavid Schultz #endif
389ad760e6fSDavid Schultz 
39069099ba2SDavid Schultz #endif /* __BSD_VISIBLE || __POSIX_VISIBLE >= 200809 */
39169099ba2SDavid Schultz 
392702b2a72SGarrett Wollman /*
393702b2a72SGarrett Wollman  * Routines that are purely local.
394702b2a72SGarrett Wollman  */
395702b2a72SGarrett Wollman #if __BSD_VISIBLE
396702b2a72SGarrett Wollman int	 asprintf(char **, const char *, ...) __printflike(2, 3);
397702b2a72SGarrett Wollman char	*ctermid_r(char *);
39828c94ec4SDaniel Eischen void	 fcloseall(void);
399702b2a72SGarrett Wollman char	*fgetln(FILE *, size_t *);
400efa952cdSXin LI const char *fmtcheck(const char *, const char *) __format_arg(2);
401702b2a72SGarrett Wollman int	 fpurge(FILE *);
402702b2a72SGarrett Wollman void	 setbuffer(FILE *, char *, int);
403702b2a72SGarrett Wollman int	 setlinebuf(FILE *);
404abbd8902SMike Barcroft int	 vasprintf(char **, const char *, __va_list)
405702b2a72SGarrett Wollman 	    __printflike(2, 0);
406702b2a72SGarrett Wollman 
407702b2a72SGarrett Wollman /*
408702b2a72SGarrett Wollman  * The system error table contains messages for the first sys_nerr
409702b2a72SGarrett Wollman  * positive errno values.  Use strerror() or strerror_r() from <string.h>
410702b2a72SGarrett Wollman  * instead.
411702b2a72SGarrett Wollman  */
412f6ab8089SEd Schouten extern const int sys_nerr;
413f6ab8089SEd Schouten extern const char * const sys_errlist[];
414702b2a72SGarrett Wollman 
415702b2a72SGarrett Wollman /*
416702b2a72SGarrett Wollman  * Stdio function-access interface.
417702b2a72SGarrett Wollman  */
418702b2a72SGarrett Wollman FILE	*funopen(const void *,
419702b2a72SGarrett Wollman 	    int (*)(void *, char *, int),
420702b2a72SGarrett Wollman 	    int (*)(void *, const char *, int),
421702b2a72SGarrett Wollman 	    fpos_t (*)(void *, fpos_t, int),
422702b2a72SGarrett Wollman 	    int (*)(void *));
423702b2a72SGarrett Wollman #define	fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
424702b2a72SGarrett Wollman #define	fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
42559deaec5SRodney W. Grimes 
42659deaec5SRodney W. Grimes /*
42715aa51b0SBruce Evans  * Portability hacks.  See <sys/types.h>.
42815aa51b0SBruce Evans  */
42915aa51b0SBruce Evans #ifndef _FTRUNCATE_DECLARED
43015aa51b0SBruce Evans #define	_FTRUNCATE_DECLARED
431abbd8902SMike Barcroft int	 ftruncate(int, __off_t);
43215aa51b0SBruce Evans #endif
43315aa51b0SBruce Evans #ifndef _LSEEK_DECLARED
43415aa51b0SBruce Evans #define	_LSEEK_DECLARED
435abbd8902SMike Barcroft __off_t	 lseek(int, __off_t, int);
43615aa51b0SBruce Evans #endif
43715aa51b0SBruce Evans #ifndef _MMAP_DECLARED
43815aa51b0SBruce Evans #define	_MMAP_DECLARED
439abbd8902SMike Barcroft void	*mmap(void *, size_t, int, int, int, __off_t);
44015aa51b0SBruce Evans #endif
44115aa51b0SBruce Evans #ifndef _TRUNCATE_DECLARED
44215aa51b0SBruce Evans #define	_TRUNCATE_DECLARED
443abbd8902SMike Barcroft int	 truncate(const char *, __off_t);
44415aa51b0SBruce Evans #endif
445702b2a72SGarrett Wollman #endif /* __BSD_VISIBLE */
44659deaec5SRodney W. Grimes 
44719e03ca8SJohn Baldwin /*
44819e03ca8SJohn Baldwin  * Functions internal to the implementation.
44919e03ca8SJohn Baldwin  */
45019e03ca8SJohn Baldwin int	__srget(FILE *);
45119e03ca8SJohn Baldwin int	__swbuf(int, FILE *);
45219e03ca8SJohn Baldwin 
45319e03ca8SJohn Baldwin /*
45419e03ca8SJohn Baldwin  * The __sfoo macros are here so that we can
45519e03ca8SJohn Baldwin  * define function versions in the C library.
45619e03ca8SJohn Baldwin  */
45719e03ca8SJohn Baldwin #define	__sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))
45819e03ca8SJohn Baldwin #if defined(__GNUC__) && defined(__STDC__)
45919e03ca8SJohn Baldwin static __inline int __sputc(int _c, FILE *_p) {
46019e03ca8SJohn Baldwin 	if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
46119e03ca8SJohn Baldwin 		return (*_p->_p++ = _c);
46219e03ca8SJohn Baldwin 	else
46319e03ca8SJohn Baldwin 		return (__swbuf(_c, _p));
46419e03ca8SJohn Baldwin }
46519e03ca8SJohn Baldwin #else
46619e03ca8SJohn Baldwin /*
46719e03ca8SJohn Baldwin  * This has been tuned to generate reasonable code on the vax using pcc.
46819e03ca8SJohn Baldwin  */
46919e03ca8SJohn Baldwin #define	__sputc(c, p) \
47019e03ca8SJohn Baldwin 	(--(p)->_w < 0 ? \
47119e03ca8SJohn Baldwin 		(p)->_w >= (p)->_lbfsize ? \
47219e03ca8SJohn Baldwin 			(*(p)->_p = (c)), *(p)->_p != '\n' ? \
47319e03ca8SJohn Baldwin 				(int)*(p)->_p++ : \
47419e03ca8SJohn Baldwin 				__swbuf('\n', p) : \
47519e03ca8SJohn Baldwin 			__swbuf((int)(c), p) : \
47619e03ca8SJohn Baldwin 		(*(p)->_p = (c), (int)*(p)->_p++))
47719e03ca8SJohn Baldwin #endif
478eb1f614bSDavid Chisnall 
479eb1f614bSDavid Chisnall extern int __isthreaded;
480eb1f614bSDavid Chisnall 
481c9e7ce2fSDavid Chisnall #ifndef __cplusplus
48219e03ca8SJohn Baldwin 
48319e03ca8SJohn Baldwin #define	__sfeof(p)	(((p)->_flags & __SEOF) != 0)
48419e03ca8SJohn Baldwin #define	__sferror(p)	(((p)->_flags & __SERR) != 0)
48519e03ca8SJohn Baldwin #define	__sclearerr(p)	((void)((p)->_flags &= ~(__SERR|__SEOF)))
48619e03ca8SJohn Baldwin #define	__sfileno(p)	((p)->_file)
48719e03ca8SJohn Baldwin 
48819e03ca8SJohn Baldwin 
48919e03ca8SJohn Baldwin #define	feof(p)		(!__isthreaded ? __sfeof(p) : (feof)(p))
49019e03ca8SJohn Baldwin #define	ferror(p)	(!__isthreaded ? __sferror(p) : (ferror)(p))
49119e03ca8SJohn Baldwin #define	clearerr(p)	(!__isthreaded ? __sclearerr(p) : (clearerr)(p))
49219e03ca8SJohn Baldwin 
49319e03ca8SJohn Baldwin #if __POSIX_VISIBLE
49419e03ca8SJohn Baldwin #define	fileno(p)	(!__isthreaded ? __sfileno(p) : (fileno)(p))
49519e03ca8SJohn Baldwin #endif
49619e03ca8SJohn Baldwin 
49719e03ca8SJohn Baldwin #define	getc(fp)	(!__isthreaded ? __sgetc(fp) : (getc)(fp))
49819e03ca8SJohn Baldwin #define	putc(x, fp)	(!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
49919e03ca8SJohn Baldwin 
50019e03ca8SJohn Baldwin #define	getchar()	getc(stdin)
50119e03ca8SJohn Baldwin #define	putchar(x)	putc(x, stdout)
50219e03ca8SJohn Baldwin 
50319e03ca8SJohn Baldwin #if __BSD_VISIBLE
50419e03ca8SJohn Baldwin /*
50519e03ca8SJohn Baldwin  * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
50619e03ca8SJohn Baldwin  * B.8.2.7 for the rationale behind the *_unlocked() macros.
50719e03ca8SJohn Baldwin  */
50819e03ca8SJohn Baldwin #define	feof_unlocked(p)	__sfeof(p)
50919e03ca8SJohn Baldwin #define	ferror_unlocked(p)	__sferror(p)
51019e03ca8SJohn Baldwin #define	clearerr_unlocked(p)	__sclearerr(p)
51119e03ca8SJohn Baldwin #define	fileno_unlocked(p)	__sfileno(p)
51219e03ca8SJohn Baldwin #endif
51319e03ca8SJohn Baldwin #if __POSIX_VISIBLE >= 199506
51419e03ca8SJohn Baldwin #define	getc_unlocked(fp)	__sgetc(fp)
51519e03ca8SJohn Baldwin #define	putc_unlocked(x, fp)	__sputc(x, fp)
51619e03ca8SJohn Baldwin 
51719e03ca8SJohn Baldwin #define	getchar_unlocked()	getc_unlocked(stdin)
51819e03ca8SJohn Baldwin #define	putchar_unlocked(x)	putc_unlocked(x, stdout)
51919e03ca8SJohn Baldwin #endif
520c9e7ce2fSDavid Chisnall #endif /* __cplusplus */
52119e03ca8SJohn Baldwin 
522702b2a72SGarrett Wollman __END_DECLS
52315aa51b0SBruce Evans #endif /* !_STDIO_H_ */
524