10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3224Sraf * Common Development and Distribution License (the "License").
6*3224Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*3224Sraf
220Sstevel@tonic-gate /*
23*3224Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24*3224Sraf * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "../common/compat.h"
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <sys/uio.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * If writing to a utmp-like file, map the utmp structure to
380Sstevel@tonic-gate * new format on the fly.
390Sstevel@tonic-gate */
400Sstevel@tonic-gate extern int errno;
410Sstevel@tonic-gate
420Sstevel@tonic-gate extern int conv2utmpx(char *, char *, int);
430Sstevel@tonic-gate
440Sstevel@tonic-gate int
writev(int fd,struct iovec * iov,int iovcnt)450Sstevel@tonic-gate writev(int fd, struct iovec *iov, int iovcnt)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate return (bc_writev(fd, iov, iovcnt));
480Sstevel@tonic-gate }
490Sstevel@tonic-gate
500Sstevel@tonic-gate int
bc_writev(int fd,struct iovec * iov,int iovcnt)510Sstevel@tonic-gate bc_writev(int fd, struct iovec *iov, int iovcnt)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate int ret, off;
540Sstevel@tonic-gate int nsize, total = 0;
550Sstevel@tonic-gate char *nbuf;
560Sstevel@tonic-gate int i;
57*3224Sraf
580Sstevel@tonic-gate if (fd_get(fd) != -1) { /* writing utmp (utmpx, actually) */
590Sstevel@tonic-gate for (i = 0; i < iovcnt; i++) {
600Sstevel@tonic-gate nsize = getmodsize(iov[i].iov_len,
610Sstevel@tonic-gate sizeof (struct compat_utmp),
620Sstevel@tonic-gate sizeof (struct utmpx));
630Sstevel@tonic-gate
640Sstevel@tonic-gate if ((nbuf = (void *)malloc(nsize)) == NULL) {
650Sstevel@tonic-gate fprintf(stderr, "writev: malloc failed\n");
660Sstevel@tonic-gate exit(-1);
670Sstevel@tonic-gate }
68*3224Sraf
690Sstevel@tonic-gate (void) memset(nbuf, 0, nsize);
700Sstevel@tonic-gate
710Sstevel@tonic-gate ret = conv2utmpx(nbuf, iov[i].iov_base, iov[i].iov_len);
720Sstevel@tonic-gate
730Sstevel@tonic-gate if ((ret = _write(fd, nbuf, ret)) == -1) {
740Sstevel@tonic-gate if (errno == EAGAIN)
750Sstevel@tonic-gate errno = EWOULDBLOCK;
760Sstevel@tonic-gate free(nbuf);
770Sstevel@tonic-gate return (-1);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
80*3224Sraf free(nbuf);
810Sstevel@tonic-gate
82*3224Sraf ret = getmodsize(ret, sizeof (struct utmpx),
830Sstevel@tonic-gate sizeof (struct compat_utmp));
84*3224Sraf total += ret;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate return (total);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate if ((ret = _writev(fd, iov, iovcnt)) == -1) {
900Sstevel@tonic-gate if (errno == EAGAIN)
910Sstevel@tonic-gate errno = EWOULDBLOCK;
920Sstevel@tonic-gate }
93*3224Sraf
940Sstevel@tonic-gate return (ret);
950Sstevel@tonic-gate }
96