2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: readFile.c,v 1.15 2000/04/12 17:33:16 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(FileObjet *, void *, int)
113 * FILEOBJ_BLOCKED_CONN_WRITE blocking while flushing
114 * buffer of connected handle.
115 * FILEOBJ_BLOCKED_READ didn't read anything; would block
116 * n, where n > 0 read n bytes into buffer.
117 * 0 EOF has been reached
121 readChunk(StgForeignPtr ptr, StgAddr buf, StgInt off, StgInt len)
123 IOFileObject* fo = (IOFileObject*)ptr;
124 int count, rc=0, total_count=0;
128 /* Check if someone hasn't zapped us */
134 if ( fd == -1 ) /* File has been closed for us */
137 if ( FILEOBJ_IS_EOF(fo) ) {
141 /* if input stream is connect to an output stream, flush it first */
142 if ( fo->connectedTo != NULL &&
143 fo->connectedTo->fd != -1 &&
144 (fo->connectedTo->flags & FILEOBJ_WRITE)
146 rc = flushFile((StgForeignPtr)fo->connectedTo);
148 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
150 /* RW object: flush the (output) buffer first. */
151 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
152 rc = flushBuffer(ptr);
153 if (rc < 0) return rc;
155 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
159 /* copy the unread parts of the file buffer..*/
160 if ( FILEOBJ_READABLE(fo) &&
162 fo->bufWPtr >= fo->bufRPtr ) {
164 if (fo->bufWPtr - fo->bufRPtr >= len) {
165 /* buffer has enough data to fulfill the request */
166 memcpy(buf, fo->buf + fo->bufRPtr, len);
170 /* can only partially fulfill the request from the buffer */
171 count = fo->bufWPtr - fo->bufRPtr;
172 memcpy(buf, fo->buf + fo->bufRPtr, count);
184 fo->flags & FILEOBJ_WINSOCK ?
185 recv(fd, p, len, 0) :
186 read(fd, p, len))) <= 0 ) {
188 read(fd, p, len))) <= 0 ) {
197 else if ( count == -1 && errno == EAGAIN) {
200 return total_count; /* partial read */
202 return FILEOBJ_BLOCKED_READ;
206 else if ( count == -1 && errno != EINTR) {
213 total_count += count;
218 readLine() tries to fill the buffer up with a line of chars, returning
219 the length of the resulting line.
221 Users of readLine() should immediately afterwards copy out the line
227 readLine(StgForeignPtr ptr)
229 IOFileObject* fo = (IOFileObject*)ptr;
232 /* Check if someone hasn't zapped us */
233 if ( fo == NULL || fo->fd == -1 )
236 if ( FILEOBJ_IS_EOF(fo) ) {
237 ghc_errtype = ERR_EOF;
242 /* Weird case: buffering has been turned off.
243 Return non-std value and deal with this case on the Haskell side.
245 if ( FILEOBJ_UNBUFFERED(fo) ) {
249 /* if input stream is connect to an output stream, flush it first */
250 if ( fo->connectedTo != NULL &&
251 fo->connectedTo->fd != -1 &&
252 (fo->connectedTo->flags & FILEOBJ_WRITE)
254 rc = flushFile((StgForeignPtr)fo->connectedTo);
256 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
258 /* RW object: flush the (output) buffer first. */
259 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) ) {
260 rc = flushBuffer(ptr);
261 if (rc < 0) return rc;
263 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
265 if ( fo->bufRPtr < 0 || fo->bufRPtr >= fo->bufWPtr ) { /* Buffer is empty */
268 rc = fill_up_line_buffer(fo);
269 if (rc < 0) return rc;
273 unsigned char* s1 = memchr((unsigned char *)fo->buf+fo->bufRPtr, '\n', fo->bufWPtr - fo->bufRPtr);
274 if (s1 != NULL ) { /* Found one */
275 /* Note: we *don't* zero terminate the line */
276 count = s1 - ((unsigned char*)fo->buf + fo->bufRPtr) + 1;
277 fo->bufRPtr += count;
280 /* Just return partial line */
281 count = fo->bufWPtr - fo->bufRPtr;
282 fo->bufRPtr += count;
290 readChar(StgForeignPtr ptr)
292 IOFileObject* fo= (IOFileObject*)ptr;
296 /* Check if someone hasn't zapped us */
297 if ( fo == NULL || fo->fd == -1)
300 if ( FILEOBJ_IS_EOF(fo) ) {
301 ghc_errtype = ERR_EOF;
306 /* Buffering has been changed, report back */
307 if ( FILEOBJ_LINEBUFFERED(fo) ) {
309 } else if ( FILEOBJ_BLOCKBUFFERED(fo) ) {
313 /* if input stream is connect to an output stream, flush it first */
314 if ( fo->connectedTo != NULL &&
315 fo->connectedTo->fd != -1 &&
316 (fo->connectedTo->flags & FILEOBJ_WRITE)
318 rc = flushFile((StgForeignPtr)fo->connectedTo);
320 if (rc < 0) return (rc == FILEOBJ_BLOCKED_WRITE ? FILEOBJ_BLOCKED_CONN_WRITE : rc);
322 /* RW object: flush the (output) buffer first. */
323 if ( FILEOBJ_WRITEABLE(fo) && FILEOBJ_JUST_WRITTEN(fo) && FILEOBJ_NEEDS_FLUSHING(fo) ) {
324 rc = flushBuffer(ptr);
325 if (rc < 0) return rc;
327 fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ;
332 fo->flags & FILEOBJ_WINSOCK ?
333 recv(fo->fd, &c, 1, 0) :
334 read(fo->fd, &c, 1))) <= 0 ) {
336 read(fo->fd, &c, 1))) <= 0 ) {
339 ghc_errtype = ERR_EOF;
342 } else if ( count == -1 && errno == EAGAIN) {
344 return FILEOBJ_BLOCKED_READ;
345 } else if ( count == -1 && errno != EINTR) {
352 if ( isatty(fo->fd) && c == EOT ) {