2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: flushFile.c,v 1.6 1999/11/25 16:54:14 simonmar Exp $
6 * hFlush Runtime Support
13 flushFile(StgForeignPtr ptr)
15 IOFileObject* fo = (IOFileObject*)ptr;
18 if ( (fo->flags & FILEOBJ_WRITE) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
19 rc = writeBuffer(ptr,fo->bufWPtr - fo->bufRPtr);
26 flushBuffer(StgForeignPtr ptr)
28 IOFileObject* fo = (IOFileObject*)ptr;
31 /* If the file object is writeable, or if it's
32 RW *and* the last operation on it was a write,
35 if ( (!FILEOBJ_READABLE(fo) && FILEOBJ_WRITEABLE(fo)) ||
36 (FILEOBJ_RW(fo) && FILEOBJ_JUST_WRITTEN(fo)) ) {
41 /* Reset read & write pointer for input buffers */
42 if ( (fo->flags & FILEOBJ_READ) ) {
50 For RW file objects, flushing input buffers doesn't just involve
51 resetting the read & write pointers, we also have to change the
52 underlying file position to point to the effective read position.
54 (Sigh, I now understand the real reason for why stdio opted for
55 the solution of leaving this to the programmer!)
58 flushReadBuffer(StgForeignPtr ptr)
60 IOFileObject* fo = (IOFileObject*)ptr;
63 delta = fo->bufWPtr - fo->bufRPtr;
66 while ( lseek(fo->fd, -delta, SEEK_CUR) == -1) {
81 flushConnectedBuf(StgForeignPtr ptr)
84 IOFileObject* fo = (IOFileObject*)ptr;
86 /* if the stream is connected to an output stream, flush it. */
87 if ( fo->connectedTo != NULL && fo->connectedTo->fd != -1 &&
88 (fo->connectedTo->flags & FILEOBJ_WRITE) ) {
89 rc = flushBuffer((StgForeignPtr)fo->connectedTo);
91 /* Willfully ignore the return code for now. */