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