1 /* source: xiowrite.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
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
) {
52 writt
= writefull(pipe
->fd
, buff
, bytes
);
58 if (pipe
->cool_write
) {
59 Notice4("write(%d, %p, "F_Zu
"): %s",
60 pipe
->fd
, buff
, bytes
, strerror(_errno
));
65 Error4("write(%d, %p, "F_Zu
"): %s",
66 pipe
->fd
, buff
, bytes
, strerror(_errno
));
76 char space[sizeof(struct sockaddr_un)];
79 /*socklen_t fromlen;*/
82 writt
= Sendto(pipe
->fd
, buff
, bytes
, 0,
83 &pipe
->peersa
.soa
, pipe
->salen
);
84 } while (writt
< 0 && errno
== EINTR
);
88 Error6("sendto(%d, %p, "F_Zu
", 0, %s, "F_socklen
"): %s",
89 pipe
->fd
, buff
, bytes
,
90 sockaddr_info(&pipe
->peersa
.soa
, pipe
->salen
,
91 infobuff
, sizeof(infobuff
)),
92 pipe
->salen
, strerror(_errno
));
96 if ((size_t)writt
< bytes
) {
98 Warn7("sendto(%d, %p, "F_Zu
", 0, %s, "F_socklen
") only wrote "F_Zu
" of "F_Zu
" bytes",
99 pipe
->fd
, buff
, bytes
,
100 sockaddr_info(&pipe
->peersa
.soa
, pipe
->salen
,
101 infobuff
, sizeof(infobuff
)),
102 pipe
->salen
, writt
, bytes
);
107 union sockaddr_union us
;
108 socklen_t uslen
= sizeof(us
);
109 Getsockname(pipe
->fd
, &us
.soa
, &uslen
);
110 Notice1("local address: %s",
111 sockaddr_info(&us
.soa
, uslen
, infobuff
, sizeof(infobuff
)));
114 #endif /* _WITH_SOCKET */
117 writt
= Write(pipe
->para
.bipipe
.fdout
, buff
, bytes
);
120 Error4("write(%d, %p, "F_Zu
"): %s",
121 pipe
->para
.bipipe
.fdout
, buff
, bytes
, strerror(_errno
));
128 writt
= Write(pipe
->para
.exec
.fdout
, buff
, bytes
);
131 Error4("write(%d, %p, "F_Zu
"): %s",
132 pipe
->para
.exec
.fdout
, buff
, bytes
, strerror(_errno
));
139 case XIOWRITE_OPENSSL
:
140 /* this function prints its own error messages */
141 return xiowrite_openssl(pipe
, buff
, bytes
);
142 #endif /* WITH_OPENSSL */
145 Error1("xiowrite(): bad data type specification %d", pipe
->dtype
);