2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: writeFile.c,v 1.9 1999/11/05 15:25:49 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) {
65 if ( count == -1 && errno == EAGAIN) {
67 return FILEOBJ_BLOCKED_WRITE;
69 else if ( count == -1 && errno != EINTR ) {
79 /* Signal that we've emptied the buffer */
86 writeBuf(ptr, buf, len)
91 IOFileObject* fo = (IOFileObject*)ptr;
94 char *pBuf = (char *) buf;
99 /* First of all, check if we do need to flush the buffer .. */
100 /* Note - in the case of line buffering, we do not currently check
101 whether we need to flush buffer due to line terminators in the
102 buffer we're outputting */
103 if ( fo->buf != NULL && /* buffered and */
104 (fo->bufWPtr + len < (fo->bufSize)) /* there's room */
106 /* Block copying is likely to be cheaper than, flush, followed by write */
107 memcpy(((char*)fo->buf + fo->bufWPtr), buf, len);
111 /* If we do overflow, flush current contents of the buffer and
112 directly output the chunk.
113 (no attempt at splitting up the chunk is currently made)
115 if ( fo->buf != NULL && /* buffered and */
116 (fo->bufWPtr + len >= (fo->bufSize)) /* there's not room */
119 rc = writeFileObject(ptr, fo->bufWPtr);
120 /* ToDo: undo buffer fill if we're blocking.. */
130 fo->flags & FILEOBJ_WINSOCK ?
131 send(fo->fd, pBuf, (int)len, 0) :
132 write(fo->fd, pBuf, (int)len))) < len ) {
134 write(fo->fd, pBuf, (int)len))) < len ) {
140 } else if ( errno == EAGAIN ) {
142 return FILEOBJ_BLOCKED_WRITE;
143 } else if ( errno != EINTR ) {
154 writeBufBA(ptr, buf, len)
158 { return (writeBuf(ptr,(StgAddr)buf, len)); }