[060d28d] | 1 | /*- |
---|
[bb80d9d] | 2 | * SPDX-License-Identifier: BSD-3-Clause |
---|
| 3 | * |
---|
[060d28d] | 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 | * |
---|
[66659ff] | 10 | * Copyright (c) 2011 The FreeBSD Foundation |
---|
| 11 | * All rights reserved. |
---|
| 12 | * Portions of this software were developed by David Chisnall |
---|
| 13 | * under sponsorship from the FreeBSD Foundation. |
---|
| 14 | * |
---|
[060d28d] | 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. |
---|
[de8a76d] | 23 | * 3. Neither the name of the University nor the names of its contributors |
---|
[060d28d] | 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 | * @(#)local.h 8.3 (Berkeley) 7/3/94 |
---|
| 40 | * $FreeBSD$ |
---|
| 41 | */ |
---|
| 42 | |
---|
[c37f9fb] | 43 | #ifndef _STDIO_LOCAL_H |
---|
| 44 | #define _STDIO_LOCAL_H |
---|
| 45 | |
---|
[3d1e767] | 46 | #include <sys/types.h> /* for off_t */ |
---|
[060d28d] | 47 | #include <pthread.h> |
---|
| 48 | #include <string.h> |
---|
| 49 | #include <wchar.h> |
---|
[66659ff] | 50 | #include <locale.h> |
---|
[060d28d] | 51 | |
---|
| 52 | /* |
---|
| 53 | * Information local to this implementation of stdio, |
---|
| 54 | * in particular, macros and private variables. |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | extern int _sread(FILE *, char *, int); |
---|
| 58 | extern int _swrite(FILE *, char const *, int); |
---|
| 59 | extern fpos_t _sseek(FILE *, fpos_t, int); |
---|
| 60 | extern int _ftello(FILE *, fpos_t *); |
---|
| 61 | extern int _fseeko(FILE *, off_t, int, int); |
---|
| 62 | extern int __fflush(FILE *fp); |
---|
| 63 | extern void __fcloseall(void); |
---|
[c40e45b] | 64 | extern wint_t __fgetwc_mbs(FILE *, mbstate_t *, int *, locale_t); |
---|
[66659ff] | 65 | #ifndef __rtems__ |
---|
| 66 | extern wint_t __fputwc(wchar_t, FILE *, locale_t); |
---|
| 67 | #endif /* __rtems__ */ |
---|
[060d28d] | 68 | extern int __sflush(FILE *); |
---|
| 69 | extern FILE *__sfp(void); |
---|
| 70 | extern int __slbexpand(FILE *, size_t); |
---|
| 71 | #ifndef __rtems__ |
---|
| 72 | extern int __srefill(FILE *); |
---|
[c37f9fb] | 73 | #else /* __rtems__ */ |
---|
[060d28d] | 74 | /* |
---|
| 75 | * __srefill is used by fgetln(). The method is in newlib but the |
---|
| 76 | * prototype is in a private .h which is not installed. |
---|
| 77 | * allows it to be pulled from newlib. |
---|
| 78 | */ |
---|
| 79 | extern int __srefill_r(struct _reent *,FILE *); |
---|
| 80 | |
---|
[d6b8db2] | 81 | #define __srefill(_x) __srefill_r(_REENT, _x) |
---|
[c37f9fb] | 82 | #endif /* __rtems__ */ |
---|
[060d28d] | 83 | extern int __sread(void *, char *, int); |
---|
| 84 | extern int __swrite(void *, char const *, int); |
---|
| 85 | extern fpos_t __sseek(void *, fpos_t, int); |
---|
| 86 | extern int __sclose(void *); |
---|
[453ff00] | 87 | #ifndef __rtems__ |
---|
[060d28d] | 88 | extern void __sinit(void); |
---|
[453ff00] | 89 | #endif /* __rtems__ */ |
---|
[060d28d] | 90 | extern void _cleanup(void); |
---|
| 91 | extern void __smakebuf(FILE *); |
---|
| 92 | extern int __swhatbuf(FILE *, size_t *, int *); |
---|
| 93 | extern int _fwalk(int (*)(FILE *)); |
---|
[66659ff] | 94 | #ifndef __rtems__ |
---|
| 95 | extern int __svfscanf(FILE *, locale_t, const char *, __va_list); |
---|
| 96 | #endif /* __rtems__ */ |
---|
[060d28d] | 97 | extern int __swsetup(FILE *); |
---|
| 98 | extern int __sflags(const char *, int *); |
---|
| 99 | extern int __ungetc(int, FILE *); |
---|
[66659ff] | 100 | #ifndef __rtems__ |
---|
| 101 | extern wint_t __ungetwc(wint_t, FILE *, locale_t); |
---|
| 102 | extern int __vfprintf(FILE *, locale_t, const char *, __va_list); |
---|
| 103 | #endif /* __rtems__ */ |
---|
[060d28d] | 104 | extern int __vfscanf(FILE *, const char *, __va_list); |
---|
[66659ff] | 105 | #ifndef __rtems__ |
---|
| 106 | extern int __vfwprintf(FILE *, locale_t, const wchar_t *, __va_list); |
---|
| 107 | extern int __vfwscanf(FILE * __restrict, locale_t, const wchar_t * __restrict, |
---|
[060d28d] | 108 | __va_list); |
---|
[66659ff] | 109 | #endif /* __rtems__ */ |
---|
[060d28d] | 110 | extern size_t __fread(void * __restrict buf, size_t size, size_t count, |
---|
| 111 | FILE * __restrict fp); |
---|
| 112 | extern int __sdidinit; |
---|
| 113 | |
---|
[c40e45b] | 114 | static inline wint_t |
---|
| 115 | __fgetwc(FILE *fp, locale_t locale) |
---|
| 116 | { |
---|
| 117 | int nread; |
---|
| 118 | |
---|
| 119 | return (__fgetwc_mbs(fp, &fp->_mbstate, &nread, locale)); |
---|
| 120 | } |
---|
[060d28d] | 121 | |
---|
| 122 | /* |
---|
| 123 | * Prepare the given FILE for writing, and return 0 iff it |
---|
| 124 | * can be written now. Otherwise, return EOF and set errno. |
---|
| 125 | */ |
---|
| 126 | #define prepwrite(fp) \ |
---|
| 127 | ((((fp)->_flags & __SWR) == 0 || \ |
---|
| 128 | ((fp)->_bf._base == NULL && ((fp)->_flags & __SSTR) == 0)) && \ |
---|
| 129 | __swsetup(fp)) |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | * Test whether the given stdio file has an active ungetc buffer; |
---|
| 133 | * release such a buffer, without restoring ordinary unread data. |
---|
| 134 | */ |
---|
| 135 | #define HASUB(fp) ((fp)->_ub._base != NULL) |
---|
| 136 | #define FREEUB(fp) { \ |
---|
| 137 | if ((fp)->_ub._base != (fp)->_ubuf) \ |
---|
| 138 | free((char *)(fp)->_ub._base); \ |
---|
| 139 | (fp)->_ub._base = NULL; \ |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* |
---|
| 143 | * test for an fgetln() buffer. |
---|
| 144 | */ |
---|
| 145 | #define HASLB(fp) ((fp)->_lb._base != NULL) |
---|
| 146 | #define FREELB(fp) { \ |
---|
| 147 | free((char *)(fp)->_lb._base); \ |
---|
| 148 | (fp)->_lb._base = NULL; \ |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /* |
---|
| 152 | * Structure initializations for 'fake' FILE objects. |
---|
| 153 | */ |
---|
| 154 | #define FAKE_FILE { \ |
---|
| 155 | ._file = -1, \ |
---|
| 156 | ._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \ |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /* |
---|
| 160 | * Set the orientation for a stream. If o > 0, the stream has wide- |
---|
| 161 | * orientation. If o < 0, the stream has byte-orientation. |
---|
| 162 | */ |
---|
| 163 | #ifdef __rtems__ |
---|
| 164 | #define ORIENT(fp, o) |
---|
[c37f9fb] | 165 | #else /* __rtems__ */ |
---|
[060d28d] | 166 | #define ORIENT(fp, o) do { \ |
---|
| 167 | if ((fp)->_orientation == 0) \ |
---|
| 168 | (fp)->_orientation = (o); \ |
---|
| 169 | } while (0) |
---|
[c37f9fb] | 170 | |
---|
| 171 | void __stdio_cancel_cleanup(void *); |
---|
| 172 | #define FLOCKFILE_CANCELSAFE(fp) \ |
---|
| 173 | { \ |
---|
| 174 | struct _pthread_cleanup_info __cleanup_info__; \ |
---|
| 175 | if (__isthreaded) { \ |
---|
| 176 | _FLOCKFILE(fp); \ |
---|
| 177 | ___pthread_cleanup_push_imp( \ |
---|
| 178 | __stdio_cancel_cleanup, (fp), \ |
---|
| 179 | &__cleanup_info__); \ |
---|
| 180 | } else { \ |
---|
| 181 | ___pthread_cleanup_push_imp( \ |
---|
| 182 | __stdio_cancel_cleanup, NULL, \ |
---|
| 183 | &__cleanup_info__); \ |
---|
| 184 | } \ |
---|
| 185 | { |
---|
| 186 | #define FUNLOCKFILE_CANCELSAFE() \ |
---|
| 187 | (void)0; \ |
---|
| 188 | } \ |
---|
| 189 | ___pthread_cleanup_pop_imp(1); \ |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | #endif /* __rtems__ */ |
---|
| 193 | #endif /* _STDIO_LOCAL_H */ |
---|