1 #include "port_before.h"
2 
3 #include <sys/types.h>
4 #include <sys/uio.h>
5 #include <sys/stat.h>
6 #include <sys/socket.h>
7 
8 #include "port_after.h"
9 
10 #ifndef NEED_WRITEV
11 int __bindcompat_writev;
12 #else
13 
14 #ifdef _CRAY
15 #define OWN_WRITEV
16 int
__writev(int fd,struct iovec * iov,int iovlen)17 __writev(int fd, struct iovec *iov, int iovlen)
18 {
19 	struct stat statbuf;
20 
21 	if (fstat(fd, &statbuf) < 0)
22 		return (-1);
23 
24 	/*
25 	 * Allow for atomic writes to network.
26 	 */
27 	if (statbuf.st_mode & S_IFSOCK) {
28 		struct msghdr   mesg;
29 
30 		memset(&mesg, 0, sizeof(mesg));
31 		mesg.msg_name = 0;
32 		mesg.msg_namelen = 0;
33 		mesg.msg_iov = iov;
34 		mesg.msg_iovlen = iovlen;
35 		mesg.msg_accrights = 0;
36 		mesg.msg_accrightslen = 0;
37 		return (sendmsg(fd, &mesg, 0));
38 	} else {
39 		struct iovec *tv;
40 		int i, rcode = 0, count = 0;
41 
42 		for (i = 0, tv = iov; i <= iovlen; tv++) {
43 			rcode = write(fd, tv->iov_base, tv->iov_len);
44 
45 			if (rcode < 0)
46 				break;
47 
48 			count += rcode;
49 		}
50 
51 		if (count == 0)
52 			return (rcode);
53 		else
54 			return (count);
55 	}
56 }
57 
58 #else /*_CRAY*/
59 
60 int
__writev(fd,vp,vpcount)61 __writev(fd, vp, vpcount)
62 	int fd;
63 	const struct iovec *vp;
64 	int vpcount;
65 {
66 	int count = 0;
67 
68 	while (vpcount-- > 0) {
69 		int written = write(fd, vp->iov_base, vp->iov_len);
70 
71 		if (written < 0)
72 			return (-1);
73 		count += written;
74 		if (written != vp->iov_len)
75 			break;
76 		vp++;
77 	}
78 	return (count);
79 }
80 
81 #endif /*_CRAY*/
82 
83 #endif /*NEED_WRITEV*/
84 
85 /*! \file */
86