xref: /minix/lib/libc/stdio/local.h (revision 0a6a1f1d)
1 /*	$NetBSD: local.h,v 1.38 2014/06/18 17:47:58 christos Exp $	*/
2 
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)local.h	8.3 (Berkeley) 7/3/94
35  */
36 
37 #include "wcio.h"
38 #include "fileext.h"
39 
40 #include <limits.h>
41 #include <stdarg.h>
42 #include <stdbool.h>
43 
44 /*
45  * Information local to this implementation of stdio,
46  * in particular, macros and private variables.
47  */
48 
49 extern int	__sflush(FILE *);
50 extern FILE	*__sfp(void);
51 extern void	__sfpinit(FILE *);
52 extern int	__srefill(FILE *);
53 extern ssize_t	__sread(void *, void *, size_t);
54 extern ssize_t	__swrite(void *, const void *, size_t);
55 extern off_t	__sseek(void *, off_t, int);
56 extern int	__sclose(void *);
57 extern void	__sinit(void);
58 extern void	_cleanup(void);
59 extern void	(*__cleanup)(void);
60 extern void	__smakebuf(FILE *);
61 extern int	__swhatbuf(FILE *, size_t *, int *);
62 extern int	_fwalk(int (*)(FILE *));
63 extern char    *_mktemp(char *);
64 extern int	__swsetup(FILE *);
65 extern int	__sflags(const char *, int *);
66 extern int	__svfscanf(FILE * __restrict, const char * __restrict,
67     va_list) __scanflike(2, 0);
68 extern int	__svfscanf_l(FILE * __restrict, locale_t,
69     const char * __restrict, va_list) __scanflike(3, 0);
70 extern int	__svfscanf_unlocked_l(FILE * __restrict, locale_t,
71     const char * __restrict, va_list) __scanflike(3, 0);
72 extern int	__vfprintf_unlocked_l(FILE * __restrict, locale_t,
73     const char * __restrict, va_list) __printflike(3, 0);
74 
75 
76 extern int	__sdidinit;
77 
78 extern wint_t	__fgetwc_unlock(FILE *);
79 extern wint_t	__fputwc_unlock(wchar_t, FILE *);
80 
81 extern ssize_t	__getdelim(char **__restrict, size_t *__restrict, int,
82     FILE *__restrict);
83 extern char	*__fgetstr(FILE * __restrict, size_t * __restrict, int);
84 extern int 	 __vfwprintf_unlocked_l(FILE *, locale_t, const wchar_t *, va_list);
85 extern int	 __vfwscanf_unlocked_l(FILE * __restrict, locale_t,
86     const wchar_t * __restrict, va_list);
87 
88 /*
89  * Return true iff the given FILE cannot be written now.
90  */
91 #define	cantwrite(fp) \
92 	((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \
93 	 __swsetup(fp))
94 
95 /*
96  * Test whether the given stdio file has an active ungetc buffer;
97  * release such a buffer, without restoring ordinary unread data.
98  */
99 #define	HASUB(fp) (_UB(fp)._base != NULL)
100 #define	FREEUB(fp) { \
101 	if (_UB(fp)._base != (fp)->_ubuf) \
102 		free((char *)_UB(fp)._base); \
103 	_UB(fp)._base = NULL; \
104 }
105 
106 /*
107  * test for an fgetln() buffer.
108  */
109 #define	FREELB(fp) { \
110 	free(_EXT(fp)->_fgetstr_buf); \
111 	_EXT(fp)->_fgetstr_buf = NULL; \
112 	_EXT(fp)->_fgetstr_len = 0; \
113 }
114 
115 extern void __flockfile_internal(FILE *, int);
116 extern void __funlockfile_internal(FILE *, int);
117 
118 extern char *__gets(char *);
119 
120 /*
121  * Detect if the current file position fits in a long int.
122  */
123 
124 static __inline bool
__long_overflow(off_t pos)125 __long_overflow(off_t pos)
126 {
127 	return (pos < LONG_MIN) || (pos > LONG_MAX);
128 }
129