writev.c revision 2
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifndef LINT
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic const char rcsid[] = "$Id: writev.c,v 1.3 2005/04/27 04:56:13 sra Exp $";
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "port_before.h"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/types.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/uio.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/stat.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/socket.h>
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "port_after.h"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifndef NEED_WRITEV
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint __bindcompat_writev;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#else
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef _CRAY
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define OWN_WRITEV
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe__writev(int fd, struct iovec *iov, int iovlen)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct stat statbuf;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (fstat(fd, &statbuf) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Allow for atomic writes to network.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (statbuf.st_mode & S_IFSOCK) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct msghdr mesg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe memset(&mesg, 0, sizeof(mesg));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_name = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_namelen = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_iov = iov;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_iovlen = iovlen;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_accrights = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mesg.msg_accrightslen = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (sendmsg(fd, &mesg, 0));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct iovec *tv;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int i, rcode = 0, count = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0, tv = iov; i <= iovlen; tv++) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rcode = write(fd, tv->iov_base, tv->iov_len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (rcode < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe count += rcode;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (count == 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (rcode);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (count);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#else /*_CRAY*/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe__writev(fd, vp, vpcount)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int fd;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe const struct iovec *vp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int vpcount;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int count = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (vpcount-- > 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int written = write(fd, vp->iov_base, vp->iov_len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (written < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe count += written;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (written != vp->iov_len)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe vp++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (count);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif /*_CRAY*/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif /*NEED_WRITEV*/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*! \file */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe