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 * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 16249808Semaste * 3. Neither the name of the University nor the names of its contributors 171573Srgrimes * may be used to endorse or promote products derived from this software 181573Srgrimes * without specific prior written permission. 191573Srgrimes * 201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 341573Srgrimesstatic char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 6/4/93"; 351573Srgrimes#endif /* LIBC_SCCS and not lint */ 3692986Sobrien#include <sys/cdefs.h> 3792986Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/libc/stdio/fflush.c 275665 2014-12-10 08:18:22Z delphij $"); 381573Srgrimes 3971579Sdeischen#include "namespace.h" 401573Srgrimes#include <errno.h> 411573Srgrimes#include <stdio.h> 4271579Sdeischen#include "un-namespace.h" 4371579Sdeischen#include "libc_private.h" 441573Srgrimes#include "local.h" 451573Srgrimes 4672373Sdeischenstatic int sflush_locked(FILE *); 4772373Sdeischen 4871579Sdeischen/* 4971579Sdeischen * Flush a single file, or (if fp is NULL) all files. 5071579Sdeischen * MT-safe version 5171579Sdeischen */ 5213545Sjulianint 5371579Sdeischenfflush(FILE *fp) 541573Srgrimes{ 5513545Sjulian int retval; 561573Srgrimes 571573Srgrimes if (fp == NULL) 5872373Sdeischen return (_fwalk(sflush_locked)); 5935129Sjb FLOCKFILE(fp); 60131592Scperciva 61131592Scperciva /* 62131592Scperciva * There is disagreement about the correct behaviour of fflush() 63268924Spfg * when passed a file which is not open for writing. According to 64131592Scperciva * the ISO C standard, the behaviour is undefined. 65131592Scperciva * Under linux, such an fflush returns success and has no effect; 66131592Scperciva * under Windows, such an fflush is documented as behaving instead 67131592Scperciva * as fpurge(). 68131592Scperciva * Given that applications may be written with the expectation of 69131592Scperciva * either of these two behaviours, the only safe (non-astonishing) 70131592Scperciva * option is to return EBADF and ask that applications be fixed. 71268924Spfg * SUSv3 now requires that fflush() returns success on a read-only 72268924Spfg * stream. 73268924Spfg * 74131592Scperciva */ 75268924Spfg if ((fp->_flags & (__SWR | __SRW)) == 0) 76268924Spfg retval = 0; 77268924Spfg else 7813545Sjulian retval = __sflush(fp); 7935129Sjb FUNLOCKFILE(fp); 8013545Sjulian return (retval); 811573Srgrimes} 821573Srgrimes 8371579Sdeischen/* 8471579Sdeischen * Flush a single file, or (if fp is NULL) all files. 8571579Sdeischen * Non-MT-safe version 8671579Sdeischen */ 8713545Sjulianint 8871579Sdeischen__fflush(FILE *fp) 891573Srgrimes{ 9071579Sdeischen int retval; 911573Srgrimes 9271579Sdeischen if (fp == NULL) 9372373Sdeischen return (_fwalk(sflush_locked)); 94268924Spfg if ((fp->_flags & (__SWR | __SRW)) == 0) 95268924Spfg retval = 0; 96268924Spfg else 9771579Sdeischen retval = __sflush(fp); 9871579Sdeischen return (retval); 9971579Sdeischen} 10071579Sdeischen 10171579Sdeischenint 10271579Sdeischen__sflush(FILE *fp) 10371579Sdeischen{ 10471579Sdeischen unsigned char *p; 10571579Sdeischen int n, t; 10671579Sdeischen 1071573Srgrimes t = fp->_flags; 1081573Srgrimes if ((t & __SWR) == 0) 1091573Srgrimes return (0); 1101573Srgrimes 1111573Srgrimes if ((p = fp->_bf._base) == NULL) 1121573Srgrimes return (0); 1131573Srgrimes 1141573Srgrimes n = fp->_p - p; /* write this much */ 1151573Srgrimes 1161573Srgrimes /* 1171573Srgrimes * Set these immediately to avoid problems with longjmp and to allow 1181573Srgrimes * exchange buffering (via setvbuf) in user write function. 1191573Srgrimes */ 1201573Srgrimes fp->_p = p; 1211573Srgrimes fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; 1221573Srgrimes 1231573Srgrimes for (; n > 0; n -= t, p += t) { 12482838Sache t = _swrite(fp, (char *)p, n); 1251573Srgrimes if (t <= 0) { 126268924Spfg /* Reset _p and _w. */ 127275665Sdelphij if (p > fp->_p) { 128275665Sdelphij /* Some was written. */ 129268924Spfg memmove(fp->_p, p, n); 130275665Sdelphij fp->_p += n; 131275665Sdelphij if ((fp->_flags & (__SLBF | __SNBF)) == 0) 132275665Sdelphij fp->_w -= n; 133275665Sdelphij } 1341573Srgrimes fp->_flags |= __SERR; 1351573Srgrimes return (EOF); 1361573Srgrimes } 1371573Srgrimes } 1381573Srgrimes return (0); 1391573Srgrimes} 14072373Sdeischen 14172373Sdeischenstatic int 14272373Sdeischensflush_locked(FILE *fp) 14372373Sdeischen{ 14472373Sdeischen int ret; 14572373Sdeischen 14672373Sdeischen FLOCKFILE(fp); 14772373Sdeischen ret = __sflush(fp); 14872373Sdeischen FUNLOCKFILE(fp); 14972373Sdeischen return (ret); 15072373Sdeischen} 151