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: stable/10/lib/libc/stdio/local.h 321074 2017-07-17 14:09:34Z kib $ 391573Srgrimes */ 401573Srgrimes 41321074Skib#ifndef _STDIO_LOCAL_H 42321074Skib#define _STDIO_LOCAL_H 43321074Skib 4471579Sdeischen#include <sys/types.h> /* for off_t */ 4573254Sdeischen#include <pthread.h> 46101776Stjr#include <string.h> 47101776Stjr#include <wchar.h> 48227753Stheraven#include <locale.h> 4971579Sdeischen 501573Srgrimes/* 51178778Sjhb * Information local to this implementation of stdio, 52178778Sjhb * in particular, macros and private variables. 531573Srgrimes */ 541573Srgrimes 5592905Sobrienextern int _sread(FILE *, char *, int); 5692905Sobrienextern int _swrite(FILE *, char const *, int); 5792905Sobrienextern fpos_t _sseek(FILE *, fpos_t, int); 5892905Sobrienextern int _ftello(FILE *, fpos_t *); 5992905Sobrienextern int _fseeko(FILE *, off_t, int, int); 6092905Sobrienextern int __fflush(FILE *fp); 61157959Sdeischenextern void __fcloseall(void); 62234799Sdasextern wint_t __fgetwc_mbs(FILE *, mbstate_t *, int *, locale_t); 63227753Stheravenextern wint_t __fputwc(wchar_t, FILE *, locale_t); 6492905Sobrienextern int __sflush(FILE *); 6592905Sobrienextern FILE *__sfp(void); 66132241Stjrextern int __slbexpand(FILE *, size_t); 6792905Sobrienextern int __srefill(FILE *); 6892905Sobrienextern int __sread(void *, char *, int); 6992905Sobrienextern int __swrite(void *, char const *, int); 7092905Sobrienextern fpos_t __sseek(void *, fpos_t, int); 7192905Sobrienextern int __sclose(void *); 7292905Sobrienextern void __sinit(void); 7392905Sobrienextern void _cleanup(void); 7492905Sobrienextern void __smakebuf(FILE *); 7592905Sobrienextern int __swhatbuf(FILE *, size_t *, int *); 7692905Sobrienextern int _fwalk(int (*)(FILE *)); 77227753Stheravenextern int __svfscanf(FILE *, locale_t, const char *, __va_list); 7892905Sobrienextern int __swsetup(FILE *); 7992905Sobrienextern int __sflags(const char *, int *); 8092905Sobrienextern int __ungetc(int, FILE *); 81227753Stheravenextern wint_t __ungetwc(wint_t, FILE *, locale_t); 82227753Stheravenextern int __vfprintf(FILE *, locale_t, const char *, __va_list); 83105098Stjrextern int __vfscanf(FILE *, const char *, __va_list); 84227753Stheravenextern int __vfwprintf(FILE *, locale_t, const wchar_t *, __va_list); 85227753Stheravenextern int __vfwscanf(FILE * __restrict, locale_t, const wchar_t * __restrict, 86103856Stjr __va_list); 87153467Sdavidxuextern size_t __fread(void * __restrict buf, size_t size, size_t count, 88153467Sdavidxu FILE * __restrict fp); 8923658Speterextern int __sdidinit; 901573Srgrimes 91234799Sdasstatic inline wint_t 92234799Sdas__fgetwc(FILE *fp, locale_t locale) 93234799Sdas{ 94234799Sdas int nread; 9573254Sdeischen 96234799Sdas return (__fgetwc_mbs(fp, &fp->_mbstate, &nread, locale)); 97234799Sdas} 98234799Sdas 991573Srgrimes/* 100130231Sdas * Prepare the given FILE for writing, and return 0 iff it 101130231Sdas * can be written now. Otherwise, return EOF and set errno. 1021573Srgrimes */ 103130231Sdas#define prepwrite(fp) \ 10478400Sassar ((((fp)->_flags & __SWR) == 0 || \ 10578400Sassar ((fp)->_bf._base == NULL && ((fp)->_flags & __SSTR) == 0)) && \ 1061573Srgrimes __swsetup(fp)) 1071573Srgrimes 1081573Srgrimes/* 1091573Srgrimes * Test whether the given stdio file has an active ungetc buffer; 1101573Srgrimes * release such a buffer, without restoring ordinary unread data. 1111573Srgrimes */ 1121573Srgrimes#define HASUB(fp) ((fp)->_ub._base != NULL) 1131573Srgrimes#define FREEUB(fp) { \ 1141573Srgrimes if ((fp)->_ub._base != (fp)->_ubuf) \ 1151573Srgrimes free((char *)(fp)->_ub._base); \ 1161573Srgrimes (fp)->_ub._base = NULL; \ 1171573Srgrimes} 1181573Srgrimes 1191573Srgrimes/* 1201573Srgrimes * test for an fgetln() buffer. 1211573Srgrimes */ 1221573Srgrimes#define HASLB(fp) ((fp)->_lb._base != NULL) 1231573Srgrimes#define FREELB(fp) { \ 1241573Srgrimes free((char *)(fp)->_lb._base); \ 1251573Srgrimes (fp)->_lb._base = NULL; \ 1261573Srgrimes} 12777686Sdeischen 128101776Stjr/* 129205021Sjhb * Structure initializations for 'fake' FILE objects. 130205021Sjhb */ 131205021Sjhb#define FAKE_FILE { \ 132205021Sjhb ._file = -1, \ 133205021Sjhb ._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \ 134205021Sjhb} 135205021Sjhb 136205021Sjhb/* 137101776Stjr * Set the orientation for a stream. If o > 0, the stream has wide- 138101776Stjr * orientation. If o < 0, the stream has byte-orientation. 139101776Stjr */ 140101776Stjr#define ORIENT(fp, o) do { \ 141178287Sjhb if ((fp)->_orientation == 0) \ 142178287Sjhb (fp)->_orientation = (o); \ 143101776Stjr} while (0) 144321074Skib 145321074Skibvoid __stdio_cancel_cleanup(void *); 146321074Skib#define FLOCKFILE_CANCELSAFE(fp) \ 147321074Skib { \ 148321074Skib struct _pthread_cleanup_info __cleanup_info__; \ 149321074Skib if (__isthreaded) { \ 150321074Skib _FLOCKFILE(fp); \ 151321074Skib ___pthread_cleanup_push_imp( \ 152321074Skib __stdio_cancel_cleanup, (fp), \ 153321074Skib &__cleanup_info__); \ 154321074Skib } else { \ 155321074Skib ___pthread_cleanup_push_imp( \ 156321074Skib __stdio_cancel_cleanup, NULL, \ 157321074Skib &__cleanup_info__); \ 158321074Skib } \ 159321074Skib { 160321074Skib#define FUNLOCKFILE_CANCELSAFE() \ 161321074Skib (void)0; \ 162321074Skib } \ 163321074Skib ___pthread_cleanup_pop_imp(1); \ 164321074Skib } 165321074Skib 166321074Skib#endif /* _STDIO_LOCAL_H */ 167