2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileObject.c,v 1.5 1999/07/12 10:43:12 sof Exp $
6 * hPutStr Runtime Support
11 #include "fileObject.h"
15 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
28 ((IOFileObject*)fo)->flags = flg;
37 ((IOFileObject*)fo)->bufWPtr = len;
45 return (((IOFileObject*)fo)->bufWPtr);
52 return (((IOFileObject*)fo)->bufSize);
61 ((IOFileObject*)fo)->buf = buf;
62 ((IOFileObject*)fo)->bufSize = sz;
69 { return (((IOFileObject*)fo)->buf); }
75 /* getWriteableBuf() is called prior to starting to pack
76 a Haskell string into the IOFileObject buffer. It takes
77 care of flushing the (input) buffer in the case we're
78 dealing with a RW handle.
80 IOFileObject* fo = (IOFileObject*)ptr;
82 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
83 flushReadBuffer(ptr); /* ignoring return code */
84 /* Ahead of time really, but indicate that we're (just about to) write */
86 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
94 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
99 { return (((IOFileObject*)fo)->fd); }
104 { return (((IOFileObject*)fo)->connectedTo->fd); }
111 { ((IOFileObject*)fo)->fd = fp;
116 setConnectedTo(fo, fw, flg)
121 if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
124 ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
128 static int __pushback_buf_size__ = 2;
131 setPushbackBufSize(i)
133 { __pushback_buf_size__ = (i > 0 ? i : 0); }
137 { return (__pushback_buf_size__); }
140 clearNonBlockingIOFlag__ (ptr)
142 { ((IOFileObject*)ptr)->flags &= ~FILEOBJ_NONBLOCKING_IO; }
145 setNonBlockingIOFlag__ (ptr)
147 { ((IOFileObject*)ptr)->flags |= FILEOBJ_NONBLOCKING_IO; }
150 clearConnNonBlockingIOFlag__ (ptr)
152 { ((IOFileObject*)ptr)->connectedTo->flags &= ~FILEOBJ_NONBLOCKING_IO; }
155 setConnNonBlockingIOFlag__ (ptr)
158 if ( ((IOFileObject*)ptr)->connectedTo != NULL ) {
159 ((IOFileObject*)ptr)->connectedTo->flags |= FILEOBJ_NONBLOCKING_IO;
165 /* Only ever called on line-buffered file objects */
167 fill_up_line_buffer(fo)
173 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
175 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
180 len = fo->bufSize - fo->bufWPtr + 1;
181 p = (unsigned char*)fo->buf + fo->bufWPtr;
183 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady ((StgForeignPtr)fo,0) != 1 )
184 return FILEOBJ_BLOCKED_READ;
189 fo->flags & FILEOBJ_WINSOCK ?
190 recv(fo->fd, p, len, 0) :
191 read(fo->fd, p, len))) <= 0 ) {
193 read(fo->fd, p, len))) <= 0 ) {
196 ghc_errtype = ERR_EOF;
200 } else if ( count == -1 && errno == EAGAIN) {
202 return FILEOBJ_BLOCKED_READ;
203 } else if ( count == -1 && errno != EINTR ) {
209 fo->bufWPtr += count;
210 return (fo->bufWPtr - ipos);