2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileObject.c,v 1.6 1999/09/16 13:14:42 simonmar 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__); }
139 /* Only ever called on line-buffered file objects */
141 fill_up_line_buffer(fo)
147 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
149 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
154 len = fo->bufSize - fo->bufWPtr + 1;
155 p = (unsigned char*)fo->buf + fo->bufWPtr;
160 fo->flags & FILEOBJ_WINSOCK ?
161 recv(fo->fd, p, len, 0) :
162 read(fo->fd, p, len))) <= 0 ) {
164 read(fo->fd, p, len))) <= 0 ) {
167 ghc_errtype = ERR_EOF;
171 } else if ( count == -1 && errno == EAGAIN) {
173 return FILEOBJ_BLOCKED_READ;
174 } else if ( count == -1 && errno != EINTR ) {
180 fo->bufWPtr += count;
181 return (fo->bufWPtr - ipos);