2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[writeFile.lc]{hPutStr Runtime Support}
12 writeFileObject(ptr, bytes)
17 IOFileObject* fo = (IOFileObject*)ptr;
19 char *p = (char *) fo->buf;
21 /* If we've got a r/w file object in our hand, flush the
22 (input) buffer contents first.
24 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
25 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
26 rc = flushReadBuffer(ptr);
27 if (rc < 0) return rc;
30 return (writeBuffer(ptr, bytes));
34 writeBuffer(ptr, bytes)
39 IOFileObject* fo = (IOFileObject*)ptr;
41 char *p = (char *) fo->buf;
43 /* Disallow short writes */
44 if (bytes == 0 || fo->buf == NULL)
47 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
48 return FILEOBJ_BLOCKED_WRITE;
50 while ((count = write(fo->fd, fo->buf, bytes)) < bytes) {
59 /* Signal that we've emptied the buffer */
66 writeBuf(ptr, buf, len)
71 IOFileObject* fo = (IOFileObject*)ptr;
74 char *p = (char *) buf;
80 /* First of all, check if we do need to flush the buffer .. */
81 /* Note - in the case of line buffering, we do not currently check
82 whether we need to flush buffer due to line terminators in the
83 buffer we're outputting */
84 if ( fo->buf != NULL && /* buffered and */
85 (fo->bufWPtr + len < (fo->bufSize)) /* there's room */
87 /* Block copying is likely to be cheaper than, flush, followed by write */
88 memcpy(((char*)fo->buf + fo->bufWPtr), buf, len);
92 /* If we do overflow, flush current contents of the buffer and
93 directly output the chunk.
94 (no attempt at splitting up the chunk is currently made)
96 if ( fo->buf != NULL && /* buffered and */
97 (fo->bufWPtr + len >= (fo->bufSize)) /* there's not room */
100 rc = writeFileObject(ptr, fo->bufWPtr);
101 /* ToDo: undo buffer fill if we're blocking.. */
108 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
109 return FILEOBJ_BLOCKED_WRITE;
111 /* Disallow short writes */
112 while ((count = write(fo->fd, (char *)buf, (int)len)) < len) {
113 if (errno != EINTR) {
126 writeBufBA(ptr, buf, len)
130 { return (writeBuf(ptr,(StgAddr)buf, len)); }