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