1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fflush.c - flush stream(s)
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "loc_incl.h"
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
int fflush(FILE* stream)
|
1989-05-30 13:34:25 +00:00
|
|
|
{
|
|
|
|
int count, c1, i, retval = 0;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
for (i = 0; i < FOPEN_MAX; i++)
|
|
|
|
if (__iotab[i] && fflush(__iotab[i]))
|
|
|
|
retval = EOF;
|
|
|
|
return retval;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
|
1989-06-26 10:37:05 +00:00
|
|
|
if (!stream->_buf
|
1990-02-13 17:08:05 +00:00
|
|
|
|| (!io_testflag(stream, _IOREADING)
|
2018-06-21 20:33:47 +00:00
|
|
|
&& !io_testflag(stream, _IOWRITING)))
|
1989-06-26 10:37:05 +00:00
|
|
|
return 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
if (io_testflag(stream, _IOREADING))
|
|
|
|
{
|
1990-04-04 15:52:01 +00:00
|
|
|
/* (void) fseek(stream, 0L, SEEK_CUR); */
|
|
|
|
int adjust = 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
if (stream->_buf && !io_testflag(stream, _IONBF))
|
1990-04-04 15:52:01 +00:00
|
|
|
adjust = stream->_count;
|
|
|
|
stream->_count = 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
lseek(fileno(stream), (off_t)adjust, SEEK_CUR);
|
1990-04-04 15:52:01 +00:00
|
|
|
if (io_testflag(stream, _IOWRITE))
|
|
|
|
stream->_flags &= ~(_IOREADING | _IOWRITING);
|
|
|
|
stream->_ptr = stream->_buf;
|
1990-01-22 11:13:26 +00:00
|
|
|
return 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
}
|
|
|
|
else if (io_testflag(stream, _IONBF))
|
|
|
|
return 0;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
|
1989-06-26 10:37:05 +00:00
|
|
|
stream->_flags &= ~_IOWRITING;
|
|
|
|
|
|
|
|
count = stream->_ptr - stream->_buf;
|
|
|
|
stream->_ptr = stream->_buf;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (count <= 0)
|
1989-06-26 10:37:05 +00:00
|
|
|
return 0;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (io_testflag(stream, _IOAPPEND))
|
|
|
|
{
|
|
|
|
if (lseek(fileno(stream), 0L, SEEK_END) == -1)
|
|
|
|
{
|
1990-01-22 11:13:26 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
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;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (count == c1)
|
1989-06-26 10:37:05 +00:00
|
|
|
return 0;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
stream->_flags |= _IOERR;
|
2018-06-21 20:33:47 +00:00
|
|
|
return EOF;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
1990-01-22 11:13:26 +00:00
|
|
|
|
2018-06-23 16:35:45 +00:00
|
|
|
static void cleanup(void)
|
1990-01-22 11:13:26 +00:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
for (i = 0; i < FOPEN_MAX; i++)
|
1990-01-22 11:13:26 +00:00
|
|
|
if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
|
2018-06-21 20:33:47 +00:00
|
|
|
(void)fflush(__iotab[i]);
|
1990-01-22 11:13:26 +00:00
|
|
|
}
|
2018-06-23 16:35:45 +00:00
|
|
|
|
|
|
|
void __register_stdio_cleanup(void)
|
|
|
|
{
|
|
|
|
static char registered = 0;
|
|
|
|
if (!registered)
|
|
|
|
{
|
|
|
|
registered = 1;
|
|
|
|
atexit(cleanup);
|
|
|
|
}
|
|
|
|
}
|