/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <errno.h>36#include <stdio.h>37#include "un-namespace.h"38#include "libc_private.h"39#include "local.h"4041static int sflush_locked(FILE *);4243/*44* Flush a single file, or (if fp is NULL) all files.45* MT-safe version46*/47int48fflush(FILE *fp)49{50int retval;5152if (fp == NULL)53return (_fwalk(sflush_locked));54FLOCKFILE_CANCELSAFE(fp);5556/*57* There is disagreement about the correct behaviour of fflush()58* when passed a file which is not open for writing. According to59* the ISO C standard, the behaviour is undefined.60* Under linux, such an fflush returns success and has no effect;61* under Windows, such an fflush is documented as behaving instead62* as fpurge().63* Given that applications may be written with the expectation of64* either of these two behaviours, the only safe (non-astonishing)65* option is to return EBADF and ask that applications be fixed.66* SUSv3 now requires that fflush() returns success on a read-only67* stream.68*69*/70if ((fp->_flags & (__SWR | __SRW)) == 0)71retval = 0;72else73retval = __sflush(fp);74FUNLOCKFILE_CANCELSAFE();75return (retval);76}7778/*79* Flush a single file, or (if fp is NULL) all files.80* Non-MT-safe version81*/82int83__fflush(FILE *fp)84{85int retval;8687if (fp == NULL)88return (_fwalk(sflush_locked));89if ((fp->_flags & (__SWR | __SRW)) == 0)90retval = 0;91else92retval = __sflush(fp);93return (retval);94}9596__weak_reference(__fflush, fflush_unlocked);9798int99__sflush(FILE *fp)100{101unsigned char *p;102int n, f, t;103104f = fp->_flags;105if ((f & __SWR) == 0)106return (0);107108if ((p = fp->_bf._base) == NULL)109return (0);110111n = fp->_p - p; /* write this much */112113/*114* Set these immediately to avoid problems with longjmp and to allow115* exchange buffering (via setvbuf) in user write function.116*/117fp->_p = p;118fp->_w = f & (__SLBF|__SNBF) ? 0 : fp->_bf._size;119120for (; n > 0; n -= t, p += t) {121t = _swrite(fp, (char *)p, n);122if (t <= 0) {123if (p > fp->_p)124/* Some was written. */125memmove(fp->_p, p, n);126/* Reset _p and _w. */127fp->_p += n;128if ((fp->_flags & __SNBF) == 0)129fp->_w -= n;130fp->_flags |= __SERR;131return (EOF);132}133}134return (0);135}136137static int138sflush_locked(FILE *fp)139{140int ret;141142FLOCKFILE_CANCELSAFE(fp);143ret = __sflush(fp);144FUNLOCKFILE_CANCELSAFE();145return (ret);146}147148149