2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.10 2000/01/18 12:42:12 simonmar Exp $
6 * hGetContents Runtime Support
12 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
22 /* Filling up a (block-buffered) buffer, that
23 is completely empty. */
25 readBlock(StgForeignPtr ptr)
27 IOFileObject* fo = (IOFileObject*)ptr;
31 /* Check if someone hasn't zapped us */
32 if ( fo == NULL || fo->fd == -1 )
37 if ( FILEOBJ_IS_EOF(fo) ) {
38 ghc_errtype = ERR_EOF;
43 /* Weird case: buffering has suddenly been turned off.
44 Return non-std value and deal with this case on the Haskell side.
46 if ( FILEOBJ_UNBUFFERED(fo) ) {
50 /* if input stream is connect to an output stream, flush this one first. */
51 if ( fo->connectedTo != NULL &&
52 fo->connectedTo->fd != -1 &&
53 (fo->connectedTo->flags & FILEOBJ_WRITE)
55 rc = flushFile((StgForeignPtr)fo->connectedTo);
57 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
59 /* RW object: flush the (output) buffer first. */
60 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
61 rc = flushBuffer(ptr);
62 if (rc < 0) return rc;
64 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
66 /* return the unread parts of the file buffer..*/
67 if ( fo->flags & FILEOBJ_READ &&
69 fo->bufWPtr > fo->bufRPtr ) {
70 count = fo->bufWPtr - fo->bufRPtr;
76 fprintf(stderr, "rb: %d %d %d\n", fo->bufRPtr, fo->bufWPtr, fo->bufSize);
82 fo->flags & FILEOBJ_WINSOCK ?
83 recv(fd, fo->buf, fo->bufSize, 0) :
84 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
86 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
90 ghc_errtype = ERR_EOF;
93 } else if ( count == -1 && errno == EAGAIN) {
95 return FILEOBJ_BLOCKED_READ;
96 } else if ( count == -1 && errno != EINTR) {
107 /* Filling up a (block-buffered) buffer of length len */
109 readChunk(StgForeignPtr ptr, StgAddr buf, StgInt len)
111 IOFileObject* fo = (IOFileObject*)ptr;
112 int count=0,rc=0, total_count;
116 /* Check if someone hasn't zapped us */
122 if ( fd == -1 ) /* File has been closed for us */
125 if ( FILEOBJ_IS_EOF(fo) ) {
126 ghc_errtype = ERR_EOF;
131 /* if input stream is connect to an output stream, flush it first */
132 if ( fo->connectedTo != NULL &&
133 fo->connectedTo->fd != -1 &&
134 (fo->connectedTo->flags & FILEOBJ_WRITE)
136 rc = flushFile((StgForeignPtr)fo->connectedTo);
138 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
140 /* RW object: flush the (output) buffer first. */
141 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
142 rc = flushBuffer(ptr);
143 if (rc < 0) return rc;
145 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
147 /* copy the unread parts of the file buffer..*/
148 if ( FILEOBJ_READABLE(fo) &&
150 fo->bufWPtr >= fo->bufRPtr ) {
151 count = ( len < (fo->bufWPtr - fo->bufRPtr)) ? len : (fo->bufWPtr - fo->bufRPtr);
152 memcpy(buf,fo->buf, count);
158 if (len - count <= 0)
169 fo->flags & FILEOBJ_WINSOCK ?
170 recv(fd, p, len, 0) :
171 read(fd, p, len))) <= 0 ) {
173 read(fd, p, len))) <= 0 ) {
175 if ( count == 0 ) { /* EOF */
177 } else if ( count == -1 && errno == EAGAIN) {
178 /* ToDo: partial/blocked reads?????? Looks like we don't recover
179 * from this properly.
182 return FILEOBJ_BLOCKED_READ;
183 } else if ( count == -1 && errno != EINTR) {
188 total_count += count;
193 total_count += count;
194 /* ToDo: might point beyond the end of the buffer??? */
195 fo->bufWPtr = total_count;
201 readLine() tries to fill the buffer up with a line of chars, returning
202 the length of the resulting line.
204 Users of readLine() should immediately afterwards copy out the line
210 readLine(StgForeignPtr ptr)
212 IOFileObject* fo = (IOFileObject*)ptr;
215 /* Check if someone hasn't zapped us */
216 if ( fo == NULL || fo->fd == -1 )
219 if ( FILEOBJ_IS_EOF(fo) ) {
220 ghc_errtype = ERR_EOF;
225 /* Weird case: buffering has been turned off.
226 Return non-std value and deal with this case on the Haskell side.
228 if ( FILEOBJ_UNBUFFERED(fo) ) {
232 /* if input stream is connect to an output stream, flush it first */
233 if ( fo->connectedTo != NULL &&
234 fo->connectedTo->fd != -1 &&
235 (fo->connectedTo->flags & FILEOBJ_WRITE)
237 rc = flushFile((StgForeignPtr)fo->connectedTo);
239 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
241 /* RW object: flush the (output) buffer first. */
242 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
243 rc = flushBuffer(ptr);
244 if (rc < 0) return rc;
246 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
248 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
249 fo->bufRPtr=0; fo->bufWPtr=0;
250 rc = fill_up_line_buffer(fo);
251 if (rc < 0) return rc;
255 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
256 if (s1 != NULL ) { /* Found one */
257 /* Note: we *don't* zero terminate the line */
258 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
259 fo->bufRPtr += count;
262 /* Just return partial line */
263 count = fo->bufWPtr - fo->bufRPtr;
264 fo->bufRPtr += count;
272 readChar(StgForeignPtr ptr)
274 IOFileObject* fo= (IOFileObject*)ptr;
278 /* Check if someone hasn't zapped us */
279 if ( fo == NULL || fo->fd == -1)
282 if ( FILEOBJ_IS_EOF(fo) ) {
283 ghc_errtype = ERR_EOF;
288 /* Buffering has been changed, report back */
289 if ( FILEOBJ_LINEBUFFERED(fo) ) {
291 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
295 /* if input stream is connect to an output stream, flush it first */
296 if ( fo->connectedTo != NULL &&
297 fo->connectedTo->fd != -1 &&
298 (fo->connectedTo->flags & FILEOBJ_WRITE)
300 rc = flushFile((StgForeignPtr)fo->connectedTo);
302 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
304 /* RW object: flush the (output) buffer first. */
305 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
306 rc = flushBuffer(ptr);
307 if (rc < 0) return rc;
309 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
314 fo->flags & FILEOBJ_WINSOCK ?
315 recv(fo->fd, &c, 1, 0) :
316 read(fo->fd, &c, 1))) <= 0 ) {
318 read(fo->fd, &c, 1))) <= 0 ) {
321 ghc_errtype = ERR_EOF;
324 } else if ( count == -1 && errno == EAGAIN) {
326 return FILEOBJ_BLOCKED_READ;
327 } else if ( count == -1 && errno != EINTR) {
334 if ( isatty(fo->fd) && c == EOT ) {