xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/write_buf.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1*41fbaed0Stron /*	$NetBSD: write_buf.c,v 1.1.1.1 2009/06/23 10:09:01 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	write_buf 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	write buffer or bust
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include <iostuff.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /*	ssize_t	write_buf(fd, buf, len, timeout)
12*41fbaed0Stron /*	int	fd;
13*41fbaed0Stron /*	const char *buf;
14*41fbaed0Stron /*	ssize_t	len;
15*41fbaed0Stron /*	int	timeout;
16*41fbaed0Stron /* DESCRIPTION
17*41fbaed0Stron /*	write_buf() writes a buffer to the named stream in as many
18*41fbaed0Stron /*	fragments as needed, and returns the number of bytes written,
19*41fbaed0Stron /*	which is always the number requested or an error indication.
20*41fbaed0Stron /*
21*41fbaed0Stron /*	Arguments:
22*41fbaed0Stron /* .IP fd
23*41fbaed0Stron /*	File descriptor in the range 0..FD_SETSIZE.
24*41fbaed0Stron /* .IP buf
25*41fbaed0Stron /*	Address of data to be written.
26*41fbaed0Stron /* .IP len
27*41fbaed0Stron /*	Amount of data to be written.
28*41fbaed0Stron /* .IP timeout
29*41fbaed0Stron /*	Bounds the time in seconds to wait until \fIfd\fD becomes writable.
30*41fbaed0Stron /*	A value <= 0 means do not wait; this is useful only when \fIfd\fR
31*41fbaed0Stron /*	uses blocking I/O.
32*41fbaed0Stron /* DIAGNOSTICS
33*41fbaed0Stron /*	write_buf() returns -1 in case of trouble. The global \fIerrno\fR
34*41fbaed0Stron /*	variable reflects the nature of the problem.
35*41fbaed0Stron /* LICENSE
36*41fbaed0Stron /* .ad
37*41fbaed0Stron /* .fi
38*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
39*41fbaed0Stron /* AUTHOR(S)
40*41fbaed0Stron /*	Wietse Venema
41*41fbaed0Stron /*	IBM T.J. Watson Research
42*41fbaed0Stron /*	P.O. Box 704
43*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
44*41fbaed0Stron /*--*/
45*41fbaed0Stron 
46*41fbaed0Stron /* System library. */
47*41fbaed0Stron 
48*41fbaed0Stron #include <sys_defs.h>
49*41fbaed0Stron #include <unistd.h>
50*41fbaed0Stron #include <errno.h>
51*41fbaed0Stron #include <time.h>
52*41fbaed0Stron 
53*41fbaed0Stron /* Utility library. */
54*41fbaed0Stron 
55*41fbaed0Stron #include <msg.h>
56*41fbaed0Stron #include <iostuff.h>
57*41fbaed0Stron 
58*41fbaed0Stron /* write_buf - write buffer or bust */
59*41fbaed0Stron 
write_buf(int fd,const char * buf,ssize_t len,int timeout)60*41fbaed0Stron ssize_t write_buf(int fd, const char *buf, ssize_t len, int timeout)
61*41fbaed0Stron {
62*41fbaed0Stron     const char *start = buf;
63*41fbaed0Stron     ssize_t count;
64*41fbaed0Stron     time_t  expire;
65*41fbaed0Stron     int     time_left = timeout;
66*41fbaed0Stron 
67*41fbaed0Stron     if (time_left > 0)
68*41fbaed0Stron 	expire = time((time_t *) 0) + time_left;
69*41fbaed0Stron 
70*41fbaed0Stron     while (len > 0) {
71*41fbaed0Stron 	if (time_left > 0 && write_wait(fd, time_left) < 0)
72*41fbaed0Stron 	    return (-1);
73*41fbaed0Stron 	if ((count = write(fd, buf, len)) < 0) {
74*41fbaed0Stron 	    if ((errno == EAGAIN && time_left > 0) || errno == EINTR)
75*41fbaed0Stron 		 /* void */ ;
76*41fbaed0Stron 	    else
77*41fbaed0Stron 		return (-1);
78*41fbaed0Stron 	} else {
79*41fbaed0Stron 	    buf += count;
80*41fbaed0Stron 	    len -= count;
81*41fbaed0Stron 	}
82*41fbaed0Stron 	if (len > 0 && time_left > 0) {
83*41fbaed0Stron 	    time_left = expire - time((time_t *) 0);
84*41fbaed0Stron 	    if (time_left <= 0) {
85*41fbaed0Stron 		errno = ETIMEDOUT;
86*41fbaed0Stron 		return (-1);
87*41fbaed0Stron 	    }
88*41fbaed0Stron 	}
89*41fbaed0Stron     }
90*41fbaed0Stron     return (buf - start);
91*41fbaed0Stron }
92