readv/writev

Rich Salz rsalz at bbn.com
Wed Jun 26 07:30:10 AEST 1991


In <1991Jun21.215941.5693 at ncsu.edu> jwb at cepmax.ncsu.edu writes:
>Stevens (in Unix Network Programming) notes that read/write on a
>socket may input/output fewer bytes than requested.  Can I expect the
>same behavior for readv/writev?  If so, how might one implement
>"readvn"/"writevn"? (a la Steven's readn/writen)

I assume you want something like this:

/*
**  Handling the return value of writev is a pain.  It should return
**  the number of iov's it fully wrote out, and update the fields
**  in all of them to contain the new startpoints.
*/
int
bigwritev(fd, vp, vpcount)
    int			fd;
    struct iovec	*vp;
    register int	vpcount;
{
    register int	i;
    register long	left;

    /* Get the total bytecount. */
    for (left = 0, i = vpcount; --i >= 0; )
	left += vp[i].iov_len;

    while (vpcount) {
	if ((i = writev(fd, vp, vpcount)) < 0)
	    return -1;
	if ((left -= i) <= 0)
	    break;
	for (; i >= vp->iov_len; vp++, vpcount--)
	    i -= vp->iov_len;
	vp->iov_base += i;
	vp->iov_len -= i;
    }
    return 0;
}

-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.



More information about the Comp.unix.programmer mailing list