17c478bd9Sstevel@tonic-gate #ifndef LINT
2*9525b14bSRao Shoaib static const char rcsid[] = "$Id: writev.c,v 1.3 2005/04/27 04:56:13 sra Exp $";
37c478bd9Sstevel@tonic-gate #endif
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate #include "port_before.h"
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #include <sys/types.h>
87c478bd9Sstevel@tonic-gate #include <sys/uio.h>
97c478bd9Sstevel@tonic-gate #include <sys/stat.h>
107c478bd9Sstevel@tonic-gate #include <sys/socket.h>
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "port_after.h"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #ifndef NEED_WRITEV
157c478bd9Sstevel@tonic-gate int __bindcompat_writev;
167c478bd9Sstevel@tonic-gate #else
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #ifdef _CRAY
197c478bd9Sstevel@tonic-gate #define OWN_WRITEV
207c478bd9Sstevel@tonic-gate int
217c478bd9Sstevel@tonic-gate __writev(int fd, struct iovec *iov, int iovlen)
227c478bd9Sstevel@tonic-gate {
237c478bd9Sstevel@tonic-gate 	struct stat statbuf;
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statbuf) < 0)
267c478bd9Sstevel@tonic-gate 		return (-1);
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 	/*
297c478bd9Sstevel@tonic-gate 	 * Allow for atomic writes to network.
307c478bd9Sstevel@tonic-gate 	 */
31*9525b14bSRao Shoaib 	if (statbuf.st_mode & S_IFSOCK) {
327c478bd9Sstevel@tonic-gate 		struct msghdr   mesg;
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate 		memset(&mesg, 0, sizeof(mesg));
357c478bd9Sstevel@tonic-gate 		mesg.msg_name = 0;
367c478bd9Sstevel@tonic-gate 		mesg.msg_namelen = 0;
377c478bd9Sstevel@tonic-gate 		mesg.msg_iov = iov;
387c478bd9Sstevel@tonic-gate 		mesg.msg_iovlen = iovlen;
397c478bd9Sstevel@tonic-gate 		mesg.msg_accrights = 0;
407c478bd9Sstevel@tonic-gate 		mesg.msg_accrightslen = 0;
417c478bd9Sstevel@tonic-gate 		return (sendmsg(fd, &mesg, 0));
427c478bd9Sstevel@tonic-gate 	} else {
437c478bd9Sstevel@tonic-gate 		struct iovec *tv;
447c478bd9Sstevel@tonic-gate 		int i, rcode = 0, count = 0;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 		for (i = 0, tv = iov; i <= iovlen; tv++) {
477c478bd9Sstevel@tonic-gate 			rcode = write(fd, tv->iov_base, tv->iov_len);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 			if (rcode < 0)
507c478bd9Sstevel@tonic-gate 				break;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 			count += rcode;
537c478bd9Sstevel@tonic-gate 		}
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 		if (count == 0)
567c478bd9Sstevel@tonic-gate 			return (rcode);
577c478bd9Sstevel@tonic-gate 		else
587c478bd9Sstevel@tonic-gate 			return (count);
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #else /*_CRAY*/
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate int
657c478bd9Sstevel@tonic-gate __writev(fd, vp, vpcount)
667c478bd9Sstevel@tonic-gate 	int fd;
677c478bd9Sstevel@tonic-gate 	const struct iovec *vp;
687c478bd9Sstevel@tonic-gate 	int vpcount;
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int count = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	while (vpcount-- > 0) {
737c478bd9Sstevel@tonic-gate 		int written = write(fd, vp->iov_base, vp->iov_len);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		if (written < 0)
767c478bd9Sstevel@tonic-gate 			return (-1);
777c478bd9Sstevel@tonic-gate 		count += written;
787c478bd9Sstevel@tonic-gate 		if (written != vp->iov_len)
797c478bd9Sstevel@tonic-gate 			break;
807c478bd9Sstevel@tonic-gate 		vp++;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	return (count);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #endif /*_CRAY*/
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #endif /*NEED_WRITEV*/
88*9525b14bSRao Shoaib 
89*9525b14bSRao Shoaib /*! \file */
90