2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \subsection[fileObject.lc]{Managing file objects}
10 #include "fileObject.h"
17 ((IOFileObject*)fo)->flags = flg;
26 ((IOFileObject*)fo)->bufWPtr = len;
34 return (((IOFileObject*)fo)->bufWPtr);
41 return (((IOFileObject*)fo)->bufSize);
50 ((IOFileObject*)fo)->buf = buf;
51 ((IOFileObject*)fo)->bufSize = sz;
58 { return (((IOFileObject*)fo)->buf); }
64 /* getWriteableBuf() is called prior to starting to pack
65 a Haskell string into the IOFileObject buffer. It takes
66 care of flushing the (input) buffer in the case we're
67 dealing with a RW handle.
69 IOFileObject* fo = (IOFileObject*)ptr;
71 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
72 flushReadBuffer(ptr); /* ignoring return code */
73 /* Ahead of time really, but indicate that we're (just about to) write */
75 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
83 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
88 { return (((IOFileObject*)fo)->fd); }
93 { return (((IOFileObject*)fo)->connectedTo->fd); }
100 { ((IOFileObject*)fo)->fd = fp;
105 setConnectedTo(fo, fw, flg)
110 if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
113 ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
117 static int __pushback_buf_size__ = 2;
120 setPushbackBufSize(i)
122 { __pushback_buf_size__ = (i > 0 ? i : 0); }
126 { return (__pushback_buf_size__); }
129 clearNonBlockingIOFlag__ (ptr)
131 { ((IOFileObject*)ptr)->flags &= ~FILEOBJ_NONBLOCKING_IO; }
134 setNonBlockingIOFlag__ (ptr)
136 { ((IOFileObject*)ptr)->flags |= FILEOBJ_NONBLOCKING_IO; }
139 clearConnNonBlockingIOFlag__ (ptr)
141 { ((IOFileObject*)ptr)->connectedTo->flags &= ~FILEOBJ_NONBLOCKING_IO; }
144 setConnNonBlockingIOFlag__ (ptr)
147 if ( ((IOFileObject*)ptr)->connectedTo != NULL ) {
148 ((IOFileObject*)ptr)->connectedTo->flags |= FILEOBJ_NONBLOCKING_IO;
154 /* Only ever called on line-buffered file objects */
156 fill_up_line_buffer(fo)
162 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
164 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
169 len = fo->bufSize - fo->bufWPtr + 1;
170 p = (unsigned char*)fo->buf + fo->bufWPtr;
172 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady ((StgForeignObj)fo,0) != 1 )
173 return FILEOBJ_BLOCKED_READ;
175 if ((count = read(fo->fd, p, len)) <= 0) {
177 ghc_errtype = ERR_EOF;
181 } else if ( count == -1 && errno == EAGAIN) {
183 return FILEOBJ_BLOCKED_READ;
184 } else if ( count == -1 && errno != EINTR ) {
190 fo->bufWPtr += count;
191 return (fo->bufWPtr - ipos);