1 /* source: xiowrite.c */
2 /* Copyright Gerhard Rieger 2001-2008 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this is the source of the extended write function */
8 #include "xiosysincludes.h"
11 #include "xio-readline.h"
12 #include "xio-openssl.h"
16 note that the write() call can block even if the select()/poll() call
17 reported the FD writeable: in case the FD is not nonblocking and a lock
19 on return value < 0: errno reflects the value from write() */
20 ssize_t
xiowrite(xiofile_t
*file
, const void *buff
, size_t bytes
) {
25 if (file
->tag
== XIO_TAG_INVALID
) {
26 Error1("xiowrite(): invalid xiofile descriptor %p", file
);
31 if (file
->tag
== XIO_TAG_DUAL
) {
32 pipe
= file
->dual
.stream
[1];
33 if (pipe
->tag
== XIO_TAG_INVALID
) {
34 Error1("xiowrite(): invalid xiofile sub descriptor %p[1]", file
);
43 /* try to extract a prompt from the write data */
44 if ((pipe
->dtype
& XIODATA_READMASK
) == XIOREAD_READLINE
) {
45 xioscan_readline(pipe
, buff
, bytes
);
47 #endif /* WITH_READLINE */
49 switch (pipe
->dtype
& XIODATA_WRITEMASK
) {
53 writt
= Write(pipe
->fd
, buff
, bytes
);
54 } while (writt
< 0 && errno
== EINTR
);
60 if (pipe
->cool_write
) {
61 Notice4("write(%d, %p, "F_Zu
"): %s",
62 pipe
->fd
, buff
, bytes
, strerror(_errno
));
67 Error4("write(%d, %p, "F_Zu
"): %s",
68 pipe
->fd
, buff
, bytes
, strerror(_errno
));
73 if ((size_t)writt
< bytes
) {
74 Warn2("write() only wrote "F_Zu
" of "F_Zu
" bytes",
82 char space[sizeof(struct sockaddr_un)];
85 /*socklen_t fromlen;*/
88 writt
= Sendto(pipe
->fd
, buff
, bytes
, 0,
89 &pipe
->peersa
.soa
, pipe
->salen
);
90 } while (writt
< 0 && errno
== EINTR
);
94 Error6("sendto(%d, %p, "F_Zu
", 0, %s, "F_socklen
"): %s",
95 pipe
->fd
, buff
, bytes
,
96 sockaddr_info(&pipe
->peersa
.soa
, pipe
->salen
,
97 infobuff
, sizeof(infobuff
)),
98 pipe
->salen
, strerror(_errno
));
102 if ((size_t)writt
< bytes
) {
104 Warn7("sendto(%d, %p, "F_Zu
", 0, %s, "F_socklen
") only wrote "F_Zu
" of "F_Zu
" bytes",
105 pipe
->fd
, buff
, bytes
,
106 sockaddr_info(&pipe
->peersa
.soa
, pipe
->salen
,
107 infobuff
, sizeof(infobuff
)),
108 pipe
->salen
, writt
, bytes
);
113 union sockaddr_union us
;
114 socklen_t uslen
= sizeof(us
);
115 Getsockname(pipe
->fd
, &us
.soa
, &uslen
);
116 Notice1("local address: %s",
117 sockaddr_info(&us
.soa
, uslen
, infobuff
, sizeof(infobuff
)));
120 #endif /* _WITH_SOCKET */
124 writt
= Write(pipe
->para
.bipipe
.fdout
, buff
, bytes
);
125 } while (writt
< 0 && errno
== EINTR
);
128 Error4("write(%d, %p, "F_Zu
"): %s",
129 pipe
->para
.bipipe
.fdout
, buff
, bytes
, strerror(_errno
));
133 if ((size_t)writt
< bytes
) {
134 Warn2("write() only wrote "F_Zu
" of "F_Zu
" bytes",
141 writt
= Write(pipe
->para
.exec
.fdout
, buff
, bytes
);
142 } while (writt
< 0 && errno
== EINTR
);
145 Error4("write(%d, %p, "F_Zu
"): %s",
146 pipe
->para
.exec
.fdout
, buff
, bytes
, strerror(_errno
));
150 if ((size_t)writt
< bytes
) {
151 Warn2("write() only processed "F_Zu
" of "F_Zu
" bytes",
157 case XIOWRITE_OPENSSL
:
158 /* this function prints its own error messages */
159 return xiowrite_openssl(pipe
, buff
, bytes
);
160 #endif /* WITH_OPENSSL */
163 Error1("xiowrite(): bad data type specification %d", pipe
->dtype
);