at_wini also needs a pci_reserve() for the pci compatability device, if
[minix3.git] / lib / stdio / fflush.c
blobb154609d00428a17edb5229faa70a9f6ff94cab5
1 /*
2 * fflush.c - flush stream(s)
3 */
4 /* $Header$ */
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include "loc_incl.h"
11 ssize_t _write(int d, const char *buf, size_t nbytes);
12 off_t _lseek(int fildes, off_t offset, int whence);
14 int
15 fflush(FILE *stream)
17 int count, c1, i, retval = 0;
19 if (!stream) {
20 for(i= 0; i < FOPEN_MAX; i++)
21 if (__iotab[i] && fflush(__iotab[i]))
22 retval = EOF;
23 return retval;
26 if (!stream->_buf
27 || (!io_testflag(stream, _IOREADING)
28 && !io_testflag(stream, _IOWRITING)))
29 return 0;
30 if (io_testflag(stream, _IOREADING)) {
31 /* (void) fseek(stream, 0L, SEEK_CUR); */
32 int adjust = 0;
33 if (io_testflag(stream, _IOFIFO)) {
34 /* Can't seek in a pipe. */
35 return 0;
37 if (stream->_buf && !io_testflag(stream,_IONBF))
38 adjust = -stream->_count;
39 stream->_count = 0;
40 if (_lseek(fileno(stream), (off_t) adjust, SEEK_CUR) == -1 &&
41 errno != ESPIPE) {
42 stream->_flags |= _IOERR;
43 return EOF;
45 errno = 0;
46 if (io_testflag(stream, _IOWRITE))
47 stream->_flags &= ~(_IOREADING | _IOWRITING);
48 stream->_ptr = stream->_buf;
49 return 0;
50 } else if (io_testflag(stream, _IONBF)) return 0;
52 if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
53 stream->_flags &= ~_IOWRITING;
55 count = stream->_ptr - stream->_buf;
56 stream->_ptr = stream->_buf;
58 if ( count <= 0 )
59 return 0;
61 if (io_testflag(stream, _IOAPPEND)) {
62 if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
63 stream->_flags |= _IOERR;
64 return EOF;
67 c1 = _write(stream->_fd, (char *)stream->_buf, count);
69 stream->_count = 0;
71 if ( count == c1 )
72 return 0;
74 stream->_flags |= _IOERR;
75 return EOF;
78 void
79 __cleanup(void)
81 register int i;
83 for(i= 0; i < FOPEN_MAX; i++)
84 if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
85 (void) fflush(__iotab[i]);