xref: /titanic_44/usr/src/lib/libbc/libc/sys/4.2/readv.c (revision 39c23413b8df94a95f67b34cfd4a4dfc3fd0b48d)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "../common/compat.h"
30 #include <stdio.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <sys/syscall.h>
35 #include <sys/uio.h>
36 
37 /*
38  * If reading from the utmp file, map the data to the SunOS 4.1
39  * format on the fly.
40  */
41 extern void to_utmp(char *, char *, int);
42 
43 int
44 readv(int fd, struct iovec *iov, int iovcnt)
45 {
46 	return (bc_readv(fd, iov, iovcnt));
47 }
48 
49 int
50 bc_readv(int fd, struct iovec *iov, int iovcnt)
51 {
52 	int fds, ret, off;
53 	int i, size, total = 0;
54 	char *nbuf;
55 
56 	if (fd_get(fd) != -1) {	/* we're reading utmp (utmpx really) */
57 		for (i = 0; i < iovcnt; i++) {
58 			size = getmodsize(iov[i].iov_len,
59 			    sizeof (struct compat_utmp),
60 			    sizeof (struct utmpx));
61 
62 			if ((nbuf = (void *)malloc(size)) == NULL) {
63 				fprintf(stderr, "readv: malloc failed\n");
64 				exit(-1);
65 			}
66 
67 			if ((ret = _read(fd, nbuf, size)) == -1) {
68 				if (errno == EAGAIN)
69 					errno = EWOULDBLOCK;
70 				free(nbuf);
71 				return (-1);
72 			}
73 
74 			to_utmp(iov[i].iov_base, nbuf, ret);
75 
76 			ret = getmodsize(ret, sizeof (struct utmpx),
77 			    sizeof (struct compat_utmp));
78 			total += ret;
79 
80 			free(nbuf);
81 		}
82 
83 		return (total);
84 	}
85 
86 	if ((ret = _readv(fd, iov, iovcnt)) == -1) {
87 		if (errno == EAGAIN)
88 			errno = EWOULDBLOCK;
89 	}
90 	return (ret);
91 }
92