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