2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: writeFile.c,v 1.6 1999/07/12 10:43:13 sof Exp $
6 * hPutStr Runtime Support
12 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
21 writeFileObject(ptr, bytes)
26 IOFileObject* fo = (IOFileObject*)ptr;
28 char *p = (char *) fo->buf;
30 /* If we've got a r/w file object in our hand, flush the
31 (input) buffer contents first.
33 if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
34 fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
35 rc = flushReadBuffer(ptr);
36 if (rc < 0) return rc;
39 return (writeBuffer(ptr, bytes));
43 writeBuffer(ptr, bytes)
48 IOFileObject* fo = (IOFileObject*)ptr;
50 char *p = (char *) fo->buf;
52 /* Disallow short writes */
53 if (bytes == 0 || fo->buf == NULL)
56 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
57 return FILEOBJ_BLOCKED_WRITE;
62 fo->flags & FILEOBJ_WINSOCK ?
63 send(fo->fd, fo->buf, bytes, 0) :
64 write(fo->fd, fo->buf, bytes))) < bytes) {
66 write(fo->fd, fo->buf, bytes))) < bytes) {
76 /* Signal that we've emptied the buffer */
83 writeBuf(ptr, buf, len)
88 IOFileObject* fo = (IOFileObject*)ptr;
91 char *p = (char *) buf;
96 /* First of all, check if we do need to flush the buffer .. */
97 /* Note - in the case of line buffering, we do not currently check
98 whether we need to flush buffer due to line terminators in the
99 buffer we're outputting */
100 if ( fo->buf != NULL && /* buffered and */
101 (fo->bufWPtr + len < (fo->bufSize)) /* there's room */
103 /* Block copying is likely to be cheaper than, flush, followed by write */
104 memcpy(((char*)fo->buf + fo->bufWPtr), buf, len);
108 /* If we do overflow, flush current contents of the buffer and
109 directly output the chunk.
110 (no attempt at splitting up the chunk is currently made)
112 if ( fo->buf != NULL && /* buffered and */
113 (fo->bufWPtr + len >= (fo->bufSize)) /* there's not room */
116 rc = writeFileObject(ptr, fo->bufWPtr);
117 /* ToDo: undo buffer fill if we're blocking.. */
124 if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
125 return FILEOBJ_BLOCKED_WRITE;
127 /* Disallow short writes */
131 fo->flags & FILEOBJ_WINSOCK ?
132 send(fo->fd, (char*)buf, (int)len, 0) :
133 write(fo->fd, (char*)buf, (int)len))) < len ) {
135 write(fo->fd, (char*)buf, (int)len))) < len ) {
137 if (errno != EINTR) {
150 writeBufBA(ptr, buf, len)
154 { return (writeBuf(ptr,(StgAddr)buf, len)); }