00001
00002
00003
00004
00005
00006 #include <sys/types.h>
00007 #include <stdio.h>
00008 #include <errno.h>
00009 #include "loc_incl.h"
00010
00011 ssize_t _write(int d, const char *buf, size_t nbytes);
00012 off_t _lseek(int fildes, off_t offset, int whence);
00013
00014 int
00015 fflush(FILE *stream)
00016 {
00017 int count, c1, i, retval = 0;
00018
00019 if (!stream) {
00020 for(i= 0; i < FOPEN_MAX; i++)
00021 if (__iotab[i] && fflush(__iotab[i]))
00022 retval = EOF;
00023 return retval;
00024 }
00025
00026 if (!stream->_buf
00027 || (!io_testflag(stream, _IOREADING)
00028 && !io_testflag(stream, _IOWRITING)))
00029 return 0;
00030 if (io_testflag(stream, _IOREADING)) {
00031
00032 int adjust = 0;
00033 if (io_testflag(stream, _IOFIFO)) {
00034
00035 return 0;
00036 }
00037 if (stream->_buf && !io_testflag(stream,_IONBF))
00038 adjust = -stream->_count;
00039 stream->_count = 0;
00040 if (_lseek(fileno(stream), (off_t) adjust, SEEK_CUR) == -1 &&
00041 errno != ESPIPE) {
00042 stream->_flags |= _IOERR;
00043 return EOF;
00044 }
00045 errno = 0;
00046 if (io_testflag(stream, _IOWRITE))
00047 stream->_flags &= ~(_IOREADING | _IOWRITING);
00048 stream->_ptr = stream->_buf;
00049 return 0;
00050 } else if (io_testflag(stream, _IONBF)) return 0;
00051
00052 if (io_testflag(stream, _IOREAD))
00053 stream->_flags &= ~_IOWRITING;
00054
00055 count = stream->_ptr - stream->_buf;
00056 stream->_ptr = stream->_buf;
00057
00058 if ( count <= 0 )
00059 return 0;
00060
00061 if (io_testflag(stream, _IOAPPEND)) {
00062 if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
00063 stream->_flags |= _IOERR;
00064 return EOF;
00065 }
00066 }
00067 c1 = _write(stream->_fd, (char *)stream->_buf, count);
00068
00069 stream->_count = 0;
00070
00071 if ( count == c1 )
00072 return 0;
00073
00074 stream->_flags |= _IOERR;
00075 return EOF;
00076 }
00077
00078 void
00079 __cleanup(void)
00080 {
00081 register int i;
00082
00083 for(i= 0; i < FOPEN_MAX; i++)
00084 if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
00085 (void) fflush(__iotab[i]);
00086 }