source: trunk/minix/lib/stdio/fflush.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.8 KB
Line 
1/*
2 * fflush.c - flush stream(s)
3 */
4/* $Header: /cvsup/minix/src/lib/stdio/fflush.c,v 1.3 2006/02/02 16:59:07 beng Exp $ */
5
6#include <sys/types.h>
7#include <stdio.h>
8#include <errno.h>
9#include "loc_incl.h"
10
11ssize_t _write(int d, const char *buf, size_t nbytes);
12off_t _lseek(int fildes, off_t offset, int whence);
13
14int
15fflush(FILE *stream)
16{
17 int count, c1, i, retval = 0;
18
19 if (!stream) {
20 for(i= 0; i < FOPEN_MAX; i++)
21 if (__iotab[i] && fflush(__iotab[i]))
22 retval = EOF;
23 return retval;
24 }
25
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;
36 }
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;
44 }
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;
51
52 if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
53 stream->_flags &= ~_IOWRITING;
54
55 count = stream->_ptr - stream->_buf;
56 stream->_ptr = stream->_buf;
57
58 if ( count <= 0 )
59 return 0;
60
61 if (io_testflag(stream, _IOAPPEND)) {
62 if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
63 stream->_flags |= _IOERR;
64 return EOF;
65 }
66 }
67 c1 = _write(stream->_fd, (char *)stream->_buf, count);
68
69 stream->_count = 0;
70
71 if ( count == c1 )
72 return 0;
73
74 stream->_flags |= _IOERR;
75 return EOF;
76}
77
78void
79__cleanup(void)
80{
81 register int i;
82
83 for(i= 0; i < FOPEN_MAX; i++)
84 if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
85 (void) fflush(__iotab[i]);
86}
Note: See TracBrowser for help on using the repository browser.