11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
8227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
9227753Stheraven * All rights reserved.
10227753Stheraven * Portions of this software were developed by David Chisnall
11227753Stheraven * under sponsorship from the FreeBSD Foundation.
12227753Stheraven *
131573Srgrimes * Redistribution and use in source and binary forms, with or without
141573Srgrimes * modification, are permitted provided that the following conditions
151573Srgrimes * are met:
161573Srgrimes * 1. Redistributions of source code must retain the above copyright
171573Srgrimes *    notice, this list of conditions and the following disclaimer.
181573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
191573Srgrimes *    notice, this list of conditions and the following disclaimer in the
201573Srgrimes *    documentation and/or other materials provided with the distribution.
211573Srgrimes * 4. Neither the name of the University nor the names of its contributors
221573Srgrimes *    may be used to endorse or promote products derived from this software
231573Srgrimes *    without specific prior written permission.
241573Srgrimes *
251573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351573Srgrimes * SUCH DAMAGE.
361573Srgrimes *
3723658Speter *	@(#)local.h	8.3 (Berkeley) 7/3/94
3871579Sdeischen * $FreeBSD$
391573Srgrimes */
401573Srgrimes
4171579Sdeischen#include <sys/types.h>	/* for off_t */
4273254Sdeischen#include <pthread.h>
43101776Stjr#include <string.h>
44101776Stjr#include <wchar.h>
45227753Stheraven#include <locale.h>
4671579Sdeischen
471573Srgrimes/*
48178778Sjhb * Information local to this implementation of stdio,
49178778Sjhb * in particular, macros and private variables.
501573Srgrimes */
511573Srgrimes
5292905Sobrienextern int	_sread(FILE *, char *, int);
5392905Sobrienextern int	_swrite(FILE *, char const *, int);
5492905Sobrienextern fpos_t	_sseek(FILE *, fpos_t, int);
5592905Sobrienextern int	_ftello(FILE *, fpos_t *);
5692905Sobrienextern int	_fseeko(FILE *, off_t, int, int);
5792905Sobrienextern int	__fflush(FILE *fp);
58157959Sdeischenextern void	__fcloseall(void);
59234799Sdasextern wint_t	__fgetwc_mbs(FILE *, mbstate_t *, int *, locale_t);
60227753Stheravenextern wint_t	__fputwc(wchar_t, FILE *, locale_t);
6192905Sobrienextern int	__sflush(FILE *);
6292905Sobrienextern FILE	*__sfp(void);
63132241Stjrextern int	__slbexpand(FILE *, size_t);
6492905Sobrienextern int	__srefill(FILE *);
6592905Sobrienextern int	__sread(void *, char *, int);
6692905Sobrienextern int	__swrite(void *, char const *, int);
6792905Sobrienextern fpos_t	__sseek(void *, fpos_t, int);
6892905Sobrienextern int	__sclose(void *);
6992905Sobrienextern void	__sinit(void);
7092905Sobrienextern void	_cleanup(void);
7192905Sobrienextern void	__smakebuf(FILE *);
7292905Sobrienextern int	__swhatbuf(FILE *, size_t *, int *);
7392905Sobrienextern int	_fwalk(int (*)(FILE *));
74227753Stheravenextern int	__svfscanf(FILE *, locale_t, const char *, __va_list);
7592905Sobrienextern int	__swsetup(FILE *);
7692905Sobrienextern int	__sflags(const char *, int *);
7792905Sobrienextern int	__ungetc(int, FILE *);
78227753Stheravenextern wint_t	__ungetwc(wint_t, FILE *, locale_t);
79227753Stheravenextern int	__vfprintf(FILE *, locale_t, const char *, __va_list);
80105098Stjrextern int	__vfscanf(FILE *, const char *, __va_list);
81227753Stheravenextern int	__vfwprintf(FILE *, locale_t, const wchar_t *, __va_list);
82227753Stheravenextern int	__vfwscanf(FILE * __restrict, locale_t, const wchar_t * __restrict,
83103856Stjr		    __va_list);
84153467Sdavidxuextern size_t	__fread(void * __restrict buf, size_t size, size_t count,
85153467Sdavidxu		FILE * __restrict fp);
8623658Speterextern int	__sdidinit;
871573Srgrimes
88234799Sdasstatic inline wint_t
89234799Sdas__fgetwc(FILE *fp, locale_t locale)
90234799Sdas{
91234799Sdas	int nread;
9273254Sdeischen
93234799Sdas	return (__fgetwc_mbs(fp, &fp->_mbstate, &nread, locale));
94234799Sdas}
95234799Sdas
961573Srgrimes/*
97130231Sdas * Prepare the given FILE for writing, and return 0 iff it
98130231Sdas * can be written now.  Otherwise, return EOF and set errno.
991573Srgrimes */
100130231Sdas#define	prepwrite(fp) \
10178400Sassar 	((((fp)->_flags & __SWR) == 0 || \
10278400Sassar 	    ((fp)->_bf._base == NULL && ((fp)->_flags & __SSTR) == 0)) && \
1031573Srgrimes	 __swsetup(fp))
1041573Srgrimes
1051573Srgrimes/*
1061573Srgrimes * Test whether the given stdio file has an active ungetc buffer;
1071573Srgrimes * release such a buffer, without restoring ordinary unread data.
1081573Srgrimes */
1091573Srgrimes#define	HASUB(fp) ((fp)->_ub._base != NULL)
1101573Srgrimes#define	FREEUB(fp) { \
1111573Srgrimes	if ((fp)->_ub._base != (fp)->_ubuf) \
1121573Srgrimes		free((char *)(fp)->_ub._base); \
1131573Srgrimes	(fp)->_ub._base = NULL; \
1141573Srgrimes}
1151573Srgrimes
1161573Srgrimes/*
1171573Srgrimes * test for an fgetln() buffer.
1181573Srgrimes */
1191573Srgrimes#define	HASLB(fp) ((fp)->_lb._base != NULL)
1201573Srgrimes#define	FREELB(fp) { \
1211573Srgrimes	free((char *)(fp)->_lb._base); \
1221573Srgrimes	(fp)->_lb._base = NULL; \
1231573Srgrimes}
12477686Sdeischen
125101776Stjr/*
126205021Sjhb * Structure initializations for 'fake' FILE objects.
127205021Sjhb */
128205021Sjhb#define	FAKE_FILE {				\
129205021Sjhb	._file = -1,				\
130205021Sjhb	._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \
131205021Sjhb}
132205021Sjhb
133205021Sjhb/*
134101776Stjr * Set the orientation for a stream. If o > 0, the stream has wide-
135101776Stjr * orientation. If o < 0, the stream has byte-orientation.
136101776Stjr */
137101776Stjr#define	ORIENT(fp, o)	do {				\
138178287Sjhb	if ((fp)->_orientation == 0)			\
139178287Sjhb		(fp)->_orientation = (o);		\
140101776Stjr} while (0)
141