xref: /titanic_52/usr/src/lib/libbc/libc/sys/sys5/read.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 <syscall.h>
34 
35 /*
36  * If reading from the utmp file, map the data to the SunOS 4.1
37  * format on the fly.
38  */
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 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 			free(nbuf);
65 			return (-1);
66 		}
67 
68 		to_utmp(buf, nbuf, ret);
69 
70 		ret = getmodsize(ret, sizeof (struct utmpx),
71 		    sizeof (struct compat_utmp));
72 		free(nbuf);
73 		return (ret);
74 	}
75 
76 	return (_read(fd, buf, size));
77 }
78 
79 void
80 to_utmp(char *buf, char *nbuf, int len)
81 {
82 	struct compat_utmp *ut;
83 	struct utmpx *utx;
84 
85 	utx = (struct utmpx *) nbuf;
86 	ut  = (struct compat_utmp  *) buf;
87 
88 	while ((char *)utx < (nbuf + len)) {
89 		(void) strncpy(ut->ut_line, utx->ut_line, sizeof (ut->ut_line));
90                 (void) strncpy(ut->ut_name, utx->ut_user, sizeof (ut->ut_name));
91                 (void) strncpy(ut->ut_host, utx->ut_host, sizeof (ut->ut_host));
92                 ut->ut_time = utx->ut_tv.tv_sec;
93                 utx++;
94                 ut++;
95         }
96 }
97