2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: writeFile.c,v 1.8 1999/09/16 13:14:43 simonmar 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 *pBuf = (char *) fo->buf;
52 /* Disallow short writes */
53 if (bytes == 0 || fo->buf == NULL)
59 fo->flags & FILEOBJ_WINSOCK ?
60 send(fo->fd, pBuf, bytes, 0) :
61 write(fo->fd, pBuf, bytes))) < bytes) {
63 write(fo->fd, pBuf, bytes))) < bytes) {
73 /* Signal that we've emptied the buffer */
80 writeBuf(ptr, buf, len)
85 IOFileObject* fo = (IOFileObject*)ptr;
88 char *pBuf = (char *) buf;
93 /* First of all, check if we do need to flush the buffer .. */
94 /* Note - in the case of line buffering, we do not currently check
95 whether we need to flush buffer due to line terminators in the
96 buffer we're outputting */
97 if ( fo->buf != NULL && /* buffered and */
98 (fo->bufWPtr + len < (fo->bufSize)) /* there's room */
100 /* Block copying is likely to be cheaper than, flush, followed by write */
101 memcpy(((char*)fo->buf + fo->bufWPtr), buf, len);
105 /* If we do overflow, flush current contents of the buffer and
106 directly output the chunk.
107 (no attempt at splitting up the chunk is currently made)
109 if ( fo->buf != NULL && /* buffered and */
110 (fo->bufWPtr + len >= (fo->bufSize)) /* there's not room */
113 rc = writeFileObject(ptr, fo->bufWPtr);
114 /* ToDo: undo buffer fill if we're blocking.. */
124 fo->flags & FILEOBJ_WINSOCK ?
125 send(fo->fd, pBuf, (int)len, 0) :
126 write(fo->fd, pBuf, (int)len))) < len ) {
128 write(fo->fd, pBuf, (int)len))) < len ) {
134 } else if ( errno == EAGAIN ) {
136 return FILEOBJ_BLOCKED_WRITE;
137 } else if ( errno != EINTR ) {
148 writeBufBA(ptr, buf, len)
152 { return (writeBuf(ptr,(StgAddr)buf, len)); }