xref: /freebsd/lib/libc/stdio/local.h (revision 74f1007fcc838501c74a633792c3f01833bf65e1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Chris Torek.
9  *
10  * Copyright (c) 2011 The FreeBSD Foundation
11  *
12  * Portions of this software were developed by David Chisnall
13  * under sponsorship from the FreeBSD Foundation.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #ifndef _STDIO_LOCAL_H
41 #define	_STDIO_LOCAL_H
42 
43 #include <sys/types.h>	/* for off_t */
44 #include <pthread.h>
45 #include <string.h>
46 #include <wchar.h>
47 #include <locale.h>
48 
49 /*
50  * Information local to this implementation of stdio,
51  * in particular, macros and private variables.
52  */
53 
54 extern int	_sread(FILE *, char *, int);
55 extern int	_swrite(FILE *, char const *, int);
56 extern fpos_t	_sseek(FILE *, fpos_t, int);
57 extern int	_ftello(FILE *, fpos_t *);
58 extern int	_fseeko(FILE *, off_t, int, int);
59 extern int	__fflush(FILE *fp);
60 extern void	__fcloseall(void);
61 extern wint_t	__fgetwc_mbs(FILE *, mbstate_t *, int *, locale_t);
62 extern wint_t	__fputwc(wchar_t, FILE *, locale_t);
63 extern int	__sflush(FILE *);
64 extern FILE	*__sfp(void);
65 extern int	__slbexpand(FILE *, size_t);
66 extern int	__srefill(FILE *);
67 extern int	__sread(void *, char *, int);
68 extern int	__swrite(void *, char const *, int);
69 extern fpos_t	__sseek(void *, fpos_t, int);
70 extern int	__sclose(void *);
71 extern void	__sinit(void);
72 extern void	_cleanup(void);
73 extern void	__smakebuf(FILE *);
74 extern int	__swhatbuf(FILE *, size_t *, int *);
75 extern int	_fwalk(int (*)(FILE *));
76 extern int	__svfscanf(FILE *, locale_t, const char *, __va_list);
77 extern int	__swsetup(FILE *);
78 extern int	__sflags(const char *, int *);
79 extern int	__ungetc(int, FILE *);
80 extern wint_t	__ungetwc(wint_t, FILE *, locale_t);
81 extern int	__vfprintf(FILE *, locale_t, int, const char *, __va_list);
82 extern int	__vfscanf(FILE *, const char *, __va_list);
83 extern int	__vfwprintf(FILE *, locale_t, const wchar_t *, __va_list);
84 extern int	__vfwscanf(FILE * __restrict, locale_t, const wchar_t * __restrict,
85 		    __va_list);
86 extern size_t	__fread(void * __restrict buf, size_t size, size_t count,
87 		FILE * __restrict fp);
88 extern int	__sdidinit;
89 
90 static inline wint_t
__fgetwc(FILE * fp,locale_t locale)91 __fgetwc(FILE *fp, locale_t locale)
92 {
93 	int nread;
94 
95 	return (__fgetwc_mbs(fp, &fp->_mbstate, &nread, locale));
96 }
97 
98 /*
99  * Prepare the given FILE for writing, and return 0 iff it
100  * can be written now.  Otherwise, return EOF and set errno.
101  */
102 #define	prepwrite(fp) \
103  	((((fp)->_flags & __SWR) == 0 || \
104  	    ((fp)->_bf._base == NULL && ((fp)->_flags & __SSTR) == 0)) && \
105 	 __swsetup(fp))
106 
107 /*
108  * Test whether the given stdio file has an active ungetc buffer;
109  * release such a buffer, without restoring ordinary unread data.
110  */
111 #define	HASUB(fp) ((fp)->_ub._base != NULL)
112 #define	FREEUB(fp) { \
113 	if ((fp)->_ub._base != (fp)->_ubuf) \
114 		free((char *)(fp)->_ub._base); \
115 	(fp)->_ub._base = NULL; \
116 }
117 
118 /*
119  * test for an fgetln() buffer.
120  */
121 #define	HASLB(fp) ((fp)->_lb._base != NULL)
122 #define	FREELB(fp) { \
123 	free((char *)(fp)->_lb._base); \
124 	(fp)->_lb._base = NULL; \
125 }
126 
127 /*
128  * Structure initializations for 'fake' FILE objects.
129  */
130 #define	FAKE_FILE {				\
131 	._file = -1,				\
132 	._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \
133 }
134 
135 /*
136  * Set the orientation for a stream. If o > 0, the stream has wide-
137  * orientation. If o < 0, the stream has byte-orientation.
138  */
139 #define	ORIENT(fp, o)	do {				\
140 	if ((fp)->_orientation == 0)			\
141 		(fp)->_orientation = (o);		\
142 } while (0)
143 
144 void __stdio_cancel_cleanup(void *);
145 #define	FLOCKFILE_CANCELSAFE(fp)					\
146 	{								\
147 		struct _pthread_cleanup_info __cleanup_info__;		\
148 		if (__isthreaded) {					\
149 			_FLOCKFILE(fp);					\
150 			___pthread_cleanup_push_imp(			\
151 			    __stdio_cancel_cleanup, (fp), 		\
152 			    &__cleanup_info__);				\
153 		} else {						\
154 			___pthread_cleanup_push_imp(			\
155 			    __stdio_cancel_cleanup, NULL, 		\
156 			    &__cleanup_info__);				\
157 		}							\
158 		{
159 #define	FUNLOCKFILE_CANCELSAFE()					\
160 			(void)0;					\
161 		}							\
162 		___pthread_cleanup_pop_imp(1);				\
163 	}
164 
165 #endif /* _STDIO_LOCAL_H */
166