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.
161573Srgrimes * 4. 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$");
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()
63131592Scperciva	 * when passed a file which is not open for reading.  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.
71131592Scperciva	 */
721573Srgrimes	if ((fp->_flags & (__SWR | __SRW)) == 0) {
731573Srgrimes		errno = EBADF;
7413545Sjulian		retval = EOF;
7571579Sdeischen	} else
7613545Sjulian		retval = __sflush(fp);
7735129Sjb	FUNLOCKFILE(fp);
7813545Sjulian	return (retval);
791573Srgrimes}
801573Srgrimes
8171579Sdeischen/*
8271579Sdeischen * Flush a single file, or (if fp is NULL) all files.
8371579Sdeischen * Non-MT-safe version
8471579Sdeischen */
8513545Sjulianint
8671579Sdeischen__fflush(FILE *fp)
871573Srgrimes{
8871579Sdeischen	int retval;
891573Srgrimes
9071579Sdeischen	if (fp == NULL)
9172373Sdeischen		return (_fwalk(sflush_locked));
9271579Sdeischen	if ((fp->_flags & (__SWR | __SRW)) == 0) {
9371579Sdeischen		errno = EBADF;
9471579Sdeischen		retval = EOF;
9571579Sdeischen	} else
9671579Sdeischen		retval = __sflush(fp);
9771579Sdeischen	return (retval);
9871579Sdeischen}
9971579Sdeischen
10071579Sdeischenint
10171579Sdeischen__sflush(FILE *fp)
10271579Sdeischen{
10371579Sdeischen	unsigned char *p;
10471579Sdeischen	int n, t;
10571579Sdeischen
1061573Srgrimes	t = fp->_flags;
1071573Srgrimes	if ((t & __SWR) == 0)
1081573Srgrimes		return (0);
1091573Srgrimes
1101573Srgrimes	if ((p = fp->_bf._base) == NULL)
1111573Srgrimes		return (0);
1121573Srgrimes
1131573Srgrimes	n = fp->_p - p;		/* write this much */
1141573Srgrimes
1151573Srgrimes	/*
1161573Srgrimes	 * Set these immediately to avoid problems with longjmp and to allow
1171573Srgrimes	 * exchange buffering (via setvbuf) in user write function.
1181573Srgrimes	 */
1191573Srgrimes	fp->_p = p;
1201573Srgrimes	fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
1211573Srgrimes
1221573Srgrimes	for (; n > 0; n -= t, p += t) {
12382838Sache		t = _swrite(fp, (char *)p, n);
1241573Srgrimes		if (t <= 0) {
1251573Srgrimes			fp->_flags |= __SERR;
1261573Srgrimes			return (EOF);
1271573Srgrimes		}
1281573Srgrimes	}
1291573Srgrimes	return (0);
1301573Srgrimes}
13172373Sdeischen
13272373Sdeischenstatic int
13372373Sdeischensflush_locked(FILE *fp)
13472373Sdeischen{
13572373Sdeischen	int	ret;
13672373Sdeischen
13772373Sdeischen	FLOCKFILE(fp);
13872373Sdeischen	ret = __sflush(fp);
13972373Sdeischen	FUNLOCKFILE(fp);
14072373Sdeischen	return (ret);
14172373Sdeischen}
142