2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.9 1999/12/08 15:47:08 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;
216 /* Check if someone hasn't zapped us */
217 if ( fo == NULL || fo->fd == -1 )
220 if ( FILEOBJ_IS_EOF(fo) ) {
221 ghc_errtype = ERR_EOF;
226 /* Weird case: buffering has been turned off.
227 Return non-std value and deal with this case on the Haskell side.
229 if ( FILEOBJ_UNBUFFERED(fo) ) {
233 /* if input stream is connect to an output stream, flush it first */
234 if ( fo->connectedTo != NULL &&
235 fo->connectedTo->fd != -1 &&
236 (fo->connectedTo->flags & FILEOBJ_WRITE)
238 rc = flushFile((StgForeignPtr)fo->connectedTo);
240 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
242 /* RW object: flush the (output) buffer first. */
243 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
244 rc = flushBuffer(ptr);
245 if (rc < 0) return rc;
247 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
249 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
250 fo->bufRPtr=0; fo->bufWPtr=0;
251 rc = fill_up_line_buffer(fo);
252 if (rc < 0) return rc;
256 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
257 if (s1 != NULL ) { /* Found one */
258 /* Note: we *don't* zero terminate the line */
259 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
260 fo->bufRPtr += count;
263 /* Just return partial line */
264 count = fo->bufWPtr - fo->bufRPtr;
265 fo->bufRPtr += count;
276 IOFileObject* fo= (IOFileObject*)ptr;
280 /* Check if someone hasn't zapped us */
281 if ( fo == NULL || fo->fd == -1)
284 if ( FILEOBJ_IS_EOF(fo) ) {
285 ghc_errtype = ERR_EOF;
290 /* Buffering has been changed, report back */
291 if ( FILEOBJ_LINEBUFFERED(fo) ) {
293 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
297 /* if input stream is connect to an output stream, flush it first */
298 if ( fo->connectedTo != NULL &&
299 fo->connectedTo->fd != -1 &&
300 (fo->connectedTo->flags & FILEOBJ_WRITE)
302 rc = flushFile((StgForeignPtr)fo->connectedTo);
304 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
306 /* RW object: flush the (output) buffer first. */
307 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
308 rc = flushBuffer(ptr);
309 if (rc < 0) return rc;
311 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
316 fo->flags & FILEOBJ_WINSOCK ?
317 recv(fo->fd, &c, 1, 0) :
318 read(fo->fd, &c, 1))) <= 0 ) {
320 read(fo->fd, &c, 1))) <= 0 ) {
323 ghc_errtype = ERR_EOF;
326 } else if ( count == -1 && errno == EAGAIN) {
328 return FILEOBJ_BLOCKED_READ;
329 } else if ( count == -1 && errno != EINTR) {
336 if ( isatty(fo->fd) && c == EOT ) {