146111Sbostic /*- 2*61180Sbostic * Copyright (c) 1990, 1993 3*61180Sbostic * The Regents of the University of California. All rights reserved. 446111Sbostic * 546111Sbostic * This code is derived from software contributed to Berkeley by 646111Sbostic * Chris Torek. 746111Sbostic * 846111Sbostic * %sccs.include.redist.c% 946111Sbostic */ 1046111Sbostic 1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*61180Sbostic static char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 06/04/93"; 1346111Sbostic #endif /* LIBC_SCCS and not lint */ 1446111Sbostic 1556413Sbostic #include <errno.h> 1646111Sbostic #include <stdio.h> 1746111Sbostic #include "local.h" 1846111Sbostic 1946111Sbostic /* Flush a single file, or (if fp is NULL) all files. */ fflush(fp)2046111Sbosticfflush(fp) 2146111Sbostic register FILE *fp; 2246111Sbostic { 2356220Storek 2446111Sbostic if (fp == NULL) 2546111Sbostic return (_fwalk(__sflush)); 2656220Storek if ((fp->_flags & (__SWR | __SRW)) == 0) { 2746111Sbostic errno = EBADF; 2846111Sbostic return (EOF); 2946111Sbostic } 3046111Sbostic return (__sflush(fp)); 3146111Sbostic } 3246111Sbostic __sflush(fp)3346111Sbostic__sflush(fp) 3446111Sbostic register FILE *fp; 3546111Sbostic { 3646111Sbostic register unsigned char *p; 3746111Sbostic register int n, t; 3846111Sbostic 3946111Sbostic t = fp->_flags; 4046111Sbostic if ((t & __SWR) == 0) 4146111Sbostic return (0); 4246111Sbostic 4346111Sbostic if ((p = fp->_bf._base) == NULL) 4446111Sbostic return (0); 4546111Sbostic 4646111Sbostic n = fp->_p - p; /* write this much */ 4746111Sbostic 4846111Sbostic /* 4946111Sbostic * Set these immediately to avoid problems with longjmp and to allow 5046111Sbostic * exchange buffering (via setvbuf) in user write function. 5146111Sbostic */ 5246111Sbostic fp->_p = p; 5346111Sbostic fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; 5446111Sbostic 5546111Sbostic for (; n > 0; n -= t, p += t) { 5646111Sbostic t = (*fp->_write)(fp->_cookie, (char *)p, n); 5746111Sbostic if (t <= 0) { 5846111Sbostic fp->_flags |= __SERR; 5946111Sbostic return (EOF); 6046111Sbostic } 6146111Sbostic } 6246111Sbostic return (0); 6346111Sbostic } 64