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