ack/lang/cem/libcc.ansi/stdio/fflush.c

67 lines
1.2 KiB
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fflush.c - flush stream(s)
*/
/* $Header$ */
#include <stdio.h>
#include "loc_incl.h"
int _write(int d, const char *buf, int nbytes);
1989-05-30 13:34:25 +00:00
int
fflush(FILE *stream)
{
int count, c1, i, retval = 0;
if (!stream) {
for(i= 0; i < FOPEN_MAX; i++)
1989-12-18 15:04:14 +00:00
if (__iotab[i] && fflush(__iotab[i]))
1989-05-30 13:34:25 +00:00
retval = EOF;
return retval;
}
1989-06-26 10:37:05 +00:00
if (!stream->_buf
|| (!io_testflag(_IOREADING)
&& !io_testflag(_IOWRITING)))
1989-06-26 10:37:05 +00:00
return 0;
if (io_testflag(stream, _IOREADING)) {
(void) fseek(stream, 0L, SEEK_CUR);
return 0;
} else if (io_testflag(stream, _IONBF)) return 0;
1989-05-30 13:34:25 +00:00
1989-06-26 10:37:05 +00:00
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
stream->_flags &= ~_IOWRITING;
count = stream->_ptr - stream->_buf;
stream->_ptr = stream->_buf;
1989-05-30 13:34:25 +00:00
if ( count <= 0 )
1989-06-26 10:37:05 +00:00
return 0;
1989-05-30 13:34:25 +00:00
if (io_testflag(stream, _IOAPPEND)) {
if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
c1 = _write(stream->_fd, (char *)stream->_buf, count);
1989-05-30 13:34:25 +00:00
1989-06-26 10:37:05 +00:00
stream->_count = 0;
if ( count == c1 )
return 0;
1989-05-30 13:34:25 +00:00
stream->_flags |= _IOERR;
1989-06-26 10:37:05 +00:00
return EOF;
1989-05-30 13:34:25 +00:00
}
void
__cleanup(void)
{
register int i;
for(i= 0; i < FOPEN_MAX; i++)
if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
(void) fflush(__iotab[i]);
}