2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.8 1999/09/16 13:14:43 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. */
28 IOFileObject* fo = (IOFileObject*)ptr;
32 /* Check if someone hasn't zapped us */
33 if ( fo == NULL || fo->fd == -1 )
38 if ( FILEOBJ_IS_EOF(fo) ) {
39 ghc_errtype = ERR_EOF;
44 /* Weird case: buffering has suddenly been turned off.
45 Return non-std value and deal with this case on the Haskell side.
47 if ( FILEOBJ_UNBUFFERED(fo) ) {
51 /* if input stream is connect to an output stream, flush this one first. */
52 if ( fo->connectedTo != NULL &&
53 fo->connectedTo->fd != -1 &&
54 (fo->connectedTo->flags & FILEOBJ_WRITE)
56 rc = flushFile((StgForeignPtr)fo->connectedTo);
58 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
60 /* RW object: flush the (output) buffer first. */
61 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
62 rc = flushBuffer(ptr);
63 if (rc < 0) return rc;
65 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
67 /* return the unread parts of the file buffer..*/
68 if ( fo->flags & FILEOBJ_READ &&
70 fo->bufWPtr > fo->bufRPtr ) {
71 count = fo->bufWPtr - fo->bufRPtr;
77 fprintf(stderr, "rb: %d %d %d\n", fo->bufRPtr, fo->bufWPtr, fo->bufSize);
83 fo->flags & FILEOBJ_WINSOCK ?
84 recv(fd, fo->buf, fo->bufSize, 0) :
85 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
87 read(fd, fo->buf, fo->bufSize))) <= 0 ) {
91 ghc_errtype = ERR_EOF;
94 } else if ( count == -1 && errno == EAGAIN) {
96 return FILEOBJ_BLOCKED_READ;
97 } else if ( count == -1 && errno != EINTR) {
108 /* Filling up a (block-buffered) buffer of length len */
110 readChunk(ptr,buf,len)
115 IOFileObject* fo = (IOFileObject*)ptr;
116 int count=0,rc=0, total_count;
120 /* Check if someone hasn't zapped us */
126 if ( fd == -1 ) /* File has been closed for us */
129 if ( FILEOBJ_IS_EOF(fo) ) {
130 ghc_errtype = ERR_EOF;
135 /* if input stream is connect to an output stream, flush it first */
136 if ( fo->connectedTo != NULL &&
137 fo->connectedTo->fd != -1 &&
138 (fo->connectedTo->flags & FILEOBJ_WRITE)
140 rc = flushFile((StgForeignPtr)fo->connectedTo);
142 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
144 /* RW object: flush the (output) buffer first. */
145 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
146 rc = flushBuffer(ptr);
147 if (rc < 0) return rc;
149 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
151 /* copy the unread parts of the file buffer..*/
152 if ( FILEOBJ_READABLE(fo) &&
154 fo->bufWPtr >= fo->bufRPtr ) {
155 count = ( len < (fo->bufWPtr - fo->bufRPtr)) ? len : (fo->bufWPtr - fo->bufRPtr);
156 memcpy(buf,fo->buf, count);
162 if (len - count <= 0)
173 fo->flags & FILEOBJ_WINSOCK ?
174 recv(fd, p, len, 0) :
175 read(fd, p, len))) <= 0 ) {
177 read(fd, p, len))) <= 0 ) {
179 if ( count == 0 ) { /* EOF */
181 } else if ( count == -1 && errno == EAGAIN) {
183 return FILEOBJ_BLOCKED_READ;
184 } else if ( count == -1 && errno != EINTR) {
189 total_count += count;
194 total_count += count;
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
213 IOFileObject* fo = (IOFileObject*)ptr;
217 /* Check if someone hasn't zapped us */
218 if ( fo == NULL || fo->fd == -1 )
221 if ( FILEOBJ_IS_EOF(fo) ) {
222 ghc_errtype = ERR_EOF;
227 /* Weird case: buffering has been turned off.
228 Return non-std value and deal with this case on the Haskell side.
230 if ( FILEOBJ_UNBUFFERED(fo) ) {
234 /* if input stream is connect to an output stream, flush it first */
235 if ( fo->connectedTo != NULL &&
236 fo->connectedTo->fd != -1 &&
237 (fo->connectedTo->flags & FILEOBJ_WRITE)
239 rc = flushFile((StgForeignPtr)fo->connectedTo);
241 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
243 /* RW object: flush the (output) buffer first. */
244 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
245 rc = flushBuffer(ptr);
246 if (rc < 0) return rc;
248 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
250 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
251 fo->bufRPtr=0; fo->bufWPtr=0;
252 rc = fill_up_line_buffer(fo);
253 if (rc < 0) return rc;
257 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
258 if (s1 != NULL ) { /* Found one */
259 /* Note: we *don't* zero terminate the line */
260 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
261 fo->bufRPtr += count;
264 /* Just return partial line */
265 count = fo->bufWPtr - fo->bufRPtr;
266 fo->bufRPtr += count;
277 IOFileObject* fo= (IOFileObject*)ptr;
281 /* Check if someone hasn't zapped us */
282 if ( fo == NULL || fo->fd == -1)
285 if ( FILEOBJ_IS_EOF(fo) ) {
286 ghc_errtype = ERR_EOF;
291 /* Buffering has been changed, report back */
292 if ( FILEOBJ_LINEBUFFERED(fo) ) {
294 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
298 /* if input stream is connect to an output stream, flush it first */
299 if ( fo->connectedTo != NULL &&
300 fo->connectedTo->fd != -1 &&
301 (fo->connectedTo->flags & FILEOBJ_WRITE)
303 rc = flushFile((StgForeignPtr)fo->connectedTo);
305 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
307 /* RW object: flush the (output) buffer first. */
308 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
309 rc = flushBuffer(ptr);
310 if (rc < 0) return rc;
312 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
317 fo->flags & FILEOBJ_WINSOCK ?
318 recv(fo->fd, &c, 1, 0) :
319 read(fo->fd, &c, 1))) <= 0 ) {
321 read(fo->fd, &c, 1))) <= 0 ) {
324 ghc_errtype = ERR_EOF;
327 } else if ( count == -1 && errno == EAGAIN) {
329 return FILEOBJ_BLOCKED_READ;
330 } else if ( count == -1 && errno != EINTR) {
337 if ( isatty(fo->fd) && c == EOT ) {