xref: /titanic_50/usr/src/lib/libbc/libc/sys/sys5/writev.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "../common/compat.h"
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <sys/uio.h>
34 
35 /*
36  * If writing to a utmp-like file, map the utmp structure to
37  * new format on the fly.
38  */
39 extern int conv2utmpx(char *, char *, int);
40 
41 int
42 writev(int fd, struct iovec *iov, int iovcnt)
43 {
44 	return (bc_writev(fd, iov, iovcnt));
45 }
46 
47 int
48 bc_writev(int fd, struct iovec *iov, int iovcnt)
49 {
50 	int ret, off;
51 	int nsize, total = 0;
52 	char *nbuf;
53 	int i;
54 
55 	if (fd_get(fd) != -1) {
56 		for (i = 0; i < iovcnt; i++) {
57 			nsize = getmodsize(iov[i].iov_len,
58 			    sizeof (struct compat_utmp),
59 			    sizeof (struct utmpx));
60 
61 			if ((nbuf = (void *)malloc(nsize)) == NULL) {
62 				fprintf(stderr, "writev: malloc failed\n");
63 				exit(-1);
64 			}
65 
66 			(void) memset(nbuf, 0, nsize);
67 
68 			ret = conv2utmpx(nbuf, iov[i].iov_base, iov[i].iov_len);
69 
70 			if ((ret = _write(fd, nbuf, ret)) == -1) {
71 				free(nbuf);
72 				return (-1);
73 			}
74 
75 			total += ret;
76 
77 			free(nbuf);
78 
79 			ret = getmodsize(ret, sizeof (struct utmpx),
80 			    sizeof (struct compat_utmp));
81 		}
82 		total = getmodsize(total, sizeof (struct utmpx),
83 		    sizeof (struct compat_utmp));
84 		return (total);
85 
86 	}
87 
88 	return (_writev(fd, iov, iovcnt));
89 }
90