sl@0: /* FFLUSH.C sl@0: * sl@0: * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: /* sl@0: FUNCTION sl@0: <>---flush buffered file output sl@0: sl@0: INDEX sl@0: fflush sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: int fflush(FILE *<[fp]>); sl@0: sl@0: TRAD_SYNOPSIS sl@0: #include sl@0: int fflush(<[fp]>) sl@0: FILE *<[fp]>; sl@0: sl@0: DESCRIPTION sl@0: The <> output functions can buffer output before delivering it sl@0: to the host system, in order to minimize the overhead of system calls. sl@0: sl@0: Use <> to deliver any such pending output (for the file sl@0: or stream identified by <[fp]>) to the host system. sl@0: sl@0: If <[fp]> is <>, <> delivers pending output from all sl@0: open files. sl@0: sl@0: RETURNS sl@0: <> returns <<0>> unless it encounters a write error; in that sl@0: situation, it returns <>. sl@0: sl@0: PORTABILITY sl@0: ANSI C requires <>. sl@0: sl@0: No supporting OS subroutines are required. sl@0: */ sl@0: sl@0: #include sl@0: #include "LOCAL.H" sl@0: sl@0: /** sl@0: Flush a stream. If the given stream has been opened for writing operations sl@0: the output buffer is phisically written to the file. If the stream was open sl@0: for reading operations the content of the input buffer is cleared. The stream sl@0: remains open after this call. sl@0: sl@0: @param fp pointer to an open file. sl@0: sl@0: @return On Success, a 0 value indicates success. sl@0: On Failure, EOF is returned and errno may be set. sl@0: */ sl@0: EXPORT_C int sl@0: fflush (FILE * fp) sl@0: { sl@0: register unsigned char *p; sl@0: register int n, t; sl@0: sl@0: if (fp == NULL) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return EOF; // Memory for library globals is not allocated (errno not set). sl@0: return _fwalk (r, fflush); sl@0: } sl@0: CHECK_INIT (fp); sl@0: sl@0: t = fp->_flags; sl@0: if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL) sl@0: return 0; sl@0: n = fp->_p - p; /* write this much */ sl@0: sl@0: /* sl@0: * Set these immediately to avoid problems with longjmp sl@0: * and to allow exchange buffering (via setvbuf) in user sl@0: * write function. sl@0: */ sl@0: fp->_p = p; sl@0: fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size; sl@0: sl@0: while (n > 0) sl@0: { sl@0: t = (*fp->_write) (fp->_cookie, (char *) p, n); sl@0: if (t <= 0) sl@0: { sl@0: fp->_flags |= __SERR; sl@0: return EOF; sl@0: } sl@0: p += t; sl@0: n -= t; sl@0: } sl@0: return 0; sl@0: }