1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 by Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #ifndef LINT
7*0Sstevel@tonic-gate static const char rcsid[] = "$Id: writev.c,v 8.6 2003/04/30 05:21:18 marka Exp $";
8*0Sstevel@tonic-gate #endif
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate #include "port_before.h"
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #include <sys/types.h>
16*0Sstevel@tonic-gate #include <sys/uio.h>
17*0Sstevel@tonic-gate #include <sys/stat.h>
18*0Sstevel@tonic-gate #include <sys/socket.h>
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include "port_after.h"
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate #ifndef NEED_WRITEV
23*0Sstevel@tonic-gate int __bindcompat_writev;
24*0Sstevel@tonic-gate #else
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ifdef _CRAY
27*0Sstevel@tonic-gate #define OWN_WRITEV
28*0Sstevel@tonic-gate int
29*0Sstevel@tonic-gate __writev(int fd, struct iovec *iov, int iovlen)
30*0Sstevel@tonic-gate {
31*0Sstevel@tonic-gate 	struct stat statbuf;
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 	if (fstat(fd, &statbuf) < 0)
34*0Sstevel@tonic-gate 		return (-1);
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 	/*
37*0Sstevel@tonic-gate 	 * Allow for atomic writes to network.
38*0Sstevel@tonic-gate 	 */
39*0Sstevel@tonic-gate 	if (statbuf.st_mode & S_IFSOCK) {
40*0Sstevel@tonic-gate 		struct msghdr   mesg;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 		memset(&mesg, 0, sizeof(mesg));
43*0Sstevel@tonic-gate 		mesg.msg_name = 0;
44*0Sstevel@tonic-gate 		mesg.msg_namelen = 0;
45*0Sstevel@tonic-gate 		mesg.msg_iov = iov;
46*0Sstevel@tonic-gate 		mesg.msg_iovlen = iovlen;
47*0Sstevel@tonic-gate 		mesg.msg_accrights = 0;
48*0Sstevel@tonic-gate 		mesg.msg_accrightslen = 0;
49*0Sstevel@tonic-gate 		return (sendmsg(fd, &mesg, 0));
50*0Sstevel@tonic-gate 	} else {
51*0Sstevel@tonic-gate 		struct iovec *tv;
52*0Sstevel@tonic-gate 		int i, rcode = 0, count = 0;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 		for (i = 0, tv = iov; i <= iovlen; tv++) {
55*0Sstevel@tonic-gate 			rcode = write(fd, tv->iov_base, tv->iov_len);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 			if (rcode < 0)
58*0Sstevel@tonic-gate 				break;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 			count += rcode;
61*0Sstevel@tonic-gate 		}
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 		if (count == 0)
64*0Sstevel@tonic-gate 			return (rcode);
65*0Sstevel@tonic-gate 		else
66*0Sstevel@tonic-gate 			return (count);
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #else /*_CRAY*/
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate int
73*0Sstevel@tonic-gate __writev(fd, vp, vpcount)
74*0Sstevel@tonic-gate 	int fd;
75*0Sstevel@tonic-gate 	const struct iovec *vp;
76*0Sstevel@tonic-gate 	int vpcount;
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	int count = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	while (vpcount-- > 0) {
81*0Sstevel@tonic-gate 		int written = write(fd, vp->iov_base, vp->iov_len);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 		if (written < 0)
84*0Sstevel@tonic-gate 			return (-1);
85*0Sstevel@tonic-gate 		count += written;
86*0Sstevel@tonic-gate 		if (written != vp->iov_len)
87*0Sstevel@tonic-gate 			break;
88*0Sstevel@tonic-gate 		vp++;
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	return (count);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #endif /*_CRAY*/
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #endif /*NEED_WRITEV*/
96