xref: /titanic_44/usr/src/lib/libbc/libc/sys/4.2/read.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
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 <errno.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <sys/syscall.h>
35 
36 /*
37  * If reading from the utmp file, map the data to the SunOS 4.1
38  * format on the fly.
39  */
40 extern void to_utmp(char *, char *, int);
41 
42 int
43 read(int fd, char *buf, int size)
44 {
45 	return (bc_read(fd, buf, size));
46 }
47 
48 int
49 bc_read(int fd, char *buf, int size)
50 {
51 	int fds, ret, off;
52 	char *nbuf;
53 
54 	if (fd_get(fd) != -1) {	/* we're reading utmp (utmpx, really) */
55 		size = getmodsize(size, sizeof (struct compat_utmp),
56 							sizeof (struct utmpx));
57 
58 		if ((nbuf = (void *)malloc(size)) == NULL) {
59 			(void) fprintf(stderr, "read: malloc failed\n");
60 			exit(-1);
61 		}
62 
63 		if ((ret = _read(fd, nbuf, size)) == -1) {
64 			if (errno == EAGAIN)
65 				errno = EWOULDBLOCK;
66 			free(nbuf);
67 			return (-1);
68 		}
69 		to_utmp(buf, nbuf, ret);
70 
71 		ret = getmodsize(ret, sizeof (struct utmpx),
72 						sizeof (struct compat_utmp));
73 		free(nbuf);
74 		return (ret);
75 	}
76 
77 	if ((ret = _read(fd, buf, size)) == -1) {
78 		if (errno == EAGAIN)
79 			errno = EWOULDBLOCK;
80 	}
81 	return (ret);
82 }
83 
84 void
85 to_utmp(char *buf, char *nbuf, int len)
86 {
87 	struct compat_utmp *ut;
88 	struct utmpx *utx;
89 
90 	utx = (struct utmpx *)nbuf;
91 	ut  = (struct compat_utmp *)buf;
92 
93 	while ((char *)utx < (nbuf + len)) {
94 		(void) strncpy(ut->ut_line, utx->ut_line, sizeof (ut->ut_line));
95 		(void) strncpy(ut->ut_name, utx->ut_user, sizeof (ut->ut_name));
96 		(void) strncpy(ut->ut_host, utx->ut_host, sizeof (ut->ut_host));
97 		ut->ut_time = utx->ut_tv.tv_sec;
98 		utx++;
99 		ut++;
100 	}
101 }
102