1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
*
* $Id: writeFile.c,v 1.9 1999/11/05 15:25:49 simonmar Exp $
*
* hPutStr Runtime Support
*/
#include "Rts.h"
#include "stgio.h"
#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
#define USE_WINSOCK
#endif
#ifdef USE_WINSOCK
#include <winsock.h>
#endif
StgInt
writeFileObject(ptr, bytes)
StgForeignPtr ptr;
StgInt bytes;
{
int rc=0;
IOFileObject* fo = (IOFileObject*)ptr;
char *p = (char *) fo->buf;
/* If we've got a r/w file object in our hand, flush the
(input) buffer contents first.
*/
if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
rc = flushReadBuffer(ptr);
if (rc < 0) return rc;
}
return (writeBuffer(ptr, bytes));
}
StgInt
writeBuffer(ptr, bytes)
StgForeignPtr ptr;
StgInt bytes;
{
int count, rc=0;
IOFileObject* fo = (IOFileObject*)ptr;
char *pBuf = (char *) fo->buf;
/* Disallow short writes */
if (bytes == 0 || fo->buf == NULL)
return 0;
while ((count =
(
#ifdef USE_WINSOCK
fo->flags & FILEOBJ_WINSOCK ?
send(fo->fd, pBuf, bytes, 0) :
write(fo->fd, pBuf, bytes))) < bytes) {
#else
write(fo->fd, pBuf, bytes))) < bytes) {
#endif
if ( count == -1 && errno == EAGAIN) {
errno = 0;
return FILEOBJ_BLOCKED_WRITE;
}
else if ( count == -1 && errno != EINTR ) {
cvtErrno();
stdErrno();
return -1;
}
else {
bytes -= count;
pBuf += count;
}
}
/* Signal that we've emptied the buffer */
fo->bufWPtr=0;
return 0;
}
StgInt
writeBuf(ptr, buf, len)
StgForeignPtr ptr;
StgAddr buf;
StgInt len;
{
IOFileObject* fo = (IOFileObject*)ptr;
int count;
int rc = 0;
char *pBuf = (char *) buf;
if (len == 0 )
return 0;
/* First of all, check if we do need to flush the buffer .. */
/* Note - in the case of line buffering, we do not currently check
whether we need to flush buffer due to line terminators in the
buffer we're outputting */
if ( fo->buf != NULL && /* buffered and */
(fo->bufWPtr + len < (fo->bufSize)) /* there's room */
) {
/* Block copying is likely to be cheaper than, flush, followed by write */
memcpy(((char*)fo->buf + fo->bufWPtr), buf, len);
fo->bufWPtr += len;
return 0;
}
/* If we do overflow, flush current contents of the buffer and
directly output the chunk.
(no attempt at splitting up the chunk is currently made)
*/
if ( fo->buf != NULL && /* buffered and */
(fo->bufWPtr + len >= (fo->bufSize)) /* there's not room */
) {
/* Flush buffer */
rc = writeFileObject(ptr, fo->bufWPtr);
/* ToDo: undo buffer fill if we're blocking.. */
}
if (rc != 0) {
return rc;
}
while ((count =
(
#ifdef USE_WINSOCK
fo->flags & FILEOBJ_WINSOCK ?
send(fo->fd, pBuf, (int)len, 0) :
write(fo->fd, pBuf, (int)len))) < len ) {
#else
write(fo->fd, pBuf, (int)len))) < len ) {
#endif
if ( count >= 0 ) {
len -= count;
pBuf += count;
continue;
} else if ( errno == EAGAIN ) {
errno = 0;
return FILEOBJ_BLOCKED_WRITE;
} else if ( errno != EINTR ) {
cvtErrno();
stdErrno();
return -1;
}
}
return 0;
}
StgInt
writeBufBA(ptr, buf, len)
StgForeignPtr ptr;
StgByteArray buf;
StgInt len;
{ return (writeBuf(ptr,(StgAddr)buf, len)); }
|