2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileObject.c,v 1.10 2000/04/14 16:26:53 rrt Exp $
6 * hPutStr Runtime Support
14 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
23 setBufFlags(StgForeignPtr fo, StgInt flg)
25 ((IOFileObject*)fo)->flags = flg;
30 setBufWPtr(StgForeignPtr fo, StgInt len)
32 ((IOFileObject*)fo)->bufWPtr = len;
37 getBufWPtr(StgForeignPtr fo)
39 return (((IOFileObject*)fo)->bufWPtr);
43 getBufSize(StgForeignPtr fo)
45 return (((IOFileObject*)fo)->bufSize);
49 setBuf(StgForeignPtr fo, StgAddr buf,StgInt sz)
51 ((IOFileObject*)fo)->buf = buf;
52 ((IOFileObject*)fo)->bufSize = sz;
57 getBuf(StgForeignPtr fo)
58 { return (((IOFileObject*)fo)->buf); }
61 getWriteableBuf(StgForeignPtr ptr)
63 /* getWriteableBuf() is called prior to starting to pack
64 a Haskell string into the IOFileObject buffer. It takes
65 care of flushing the (input) buffer in the case we're
66 dealing with a RW handle.
68 IOFileObject* fo = (IOFileObject*)ptr;
70 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
71 flushReadBuffer(ptr); /* ignoring return code */
72 /* Ahead of time really, but indicate that we're (just about to) write */
74 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
79 getBufStart(StgForeignPtr fo, StgInt count)
80 { return ((char*)((IOFileObject*)fo)->buf + (((IOFileObject*)fo)->bufRPtr) - count); }
83 getFileFd(StgForeignPtr fo)
84 { return (((IOFileObject*)fo)->fd); }
87 getConnFileFd(StgForeignPtr fo)
88 { return (((IOFileObject*)fo)->connectedTo->fd); }
92 setFd(StgForeignPtr fo,StgInt fp)
93 { ((IOFileObject*)fo)->fd = fp;
98 setConnectedTo(StgForeignPtr fo, StgForeignPtr fw, StgInt flg)
100 if( flg && (! isatty(((IOFileObject*)fo)->fd) || !isatty(((IOFileObject*)fw)->fd)) ) {
103 ((IOFileObject*)fo)->connectedTo = (IOFileObject*)fw;
107 static int __pushback_buf_size__ = 2;
110 setPushbackBufSize(StgInt i)
111 { __pushback_buf_size__ = (i > 0 ? i : 0); }
114 getPushbackBufSize(void)
115 { return (__pushback_buf_size__); }
117 /* Only ever called on line-buffered file objects */
119 fill_up_line_buffer(IOFileObject* fo)
124 /* ToDo: deal with buffer overflow (i.e., realloc buffer if this happens) */
126 if ( fo->bufRPtr == fo->bufWPtr ) { /* There's nothing in the buffer, reset */
131 len = fo->bufSize - fo->bufWPtr;
132 p = (unsigned char*)fo->buf + fo->bufWPtr;
137 fo->flags & FILEOBJ_WINSOCK ?
138 recv(fo->fd, p, len, 0) :
139 read(fo->fd, p, len))) <= 0 ) {
141 read(fo->fd, p, len))) <= 0 ) {
144 ghc_errtype = ERR_EOF;
148 } else if ( count == -1 && errno == EAGAIN) {
150 return FILEOBJ_BLOCKED_READ;
151 } else if ( count == -1 && errno != EINTR ) {
157 fo->bufWPtr += count;
158 /* TODO: ipos doesn't change???? what's it for??? --SDM */
159 return (fo->bufWPtr - ipos);