xref: /onnv-gate/usr/src/lib/libbc/libc/sys/4.2/read.c (revision 3224:c0c9287a0eec)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3224Sraf  * Common Development and Distribution License (the "License").
6*3224Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3224Sraf 
220Sstevel@tonic-gate /*
23*3224Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*3224Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "../common/compat.h"
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <unistd.h>
34*3224Sraf #include <sys/syscall.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * If reading from the utmp file, map the data to the SunOS 4.1
38*3224Sraf  * format on the fly.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate extern void to_utmp(char *, char *, int);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int
read(int fd,char * buf,int size)430Sstevel@tonic-gate read(int fd, char *buf, int size)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	return (bc_read(fd, buf, size));
460Sstevel@tonic-gate }
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int
bc_read(int fd,char * buf,int size)490Sstevel@tonic-gate bc_read(int fd, char *buf, int size)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	int fds, ret, off;
520Sstevel@tonic-gate 	char *nbuf;
53*3224Sraf 
540Sstevel@tonic-gate 	if (fd_get(fd) != -1) {	/* we're reading utmp (utmpx, really) */
55*3224Sraf 		size = getmodsize(size, sizeof (struct compat_utmp),
560Sstevel@tonic-gate 							sizeof (struct utmpx));
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 		if ((nbuf = (void *)malloc(size)) == NULL) {
590Sstevel@tonic-gate 			(void) fprintf(stderr, "read: malloc failed\n");
600Sstevel@tonic-gate 			exit(-1);
610Sstevel@tonic-gate 		}
62*3224Sraf 
630Sstevel@tonic-gate 		if ((ret = _read(fd, nbuf, size)) == -1) {
640Sstevel@tonic-gate 			if (errno == EAGAIN)
650Sstevel@tonic-gate 				errno = EWOULDBLOCK;
660Sstevel@tonic-gate 			free(nbuf);
670Sstevel@tonic-gate 			return (-1);
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 		to_utmp(buf, nbuf, ret);
700Sstevel@tonic-gate 
71*3224Sraf 		ret = getmodsize(ret, sizeof (struct utmpx),
720Sstevel@tonic-gate 						sizeof (struct compat_utmp));
730Sstevel@tonic-gate 		free(nbuf);
740Sstevel@tonic-gate 		return (ret);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if ((ret = _read(fd, buf, size)) == -1) {
780Sstevel@tonic-gate 		if (errno == EAGAIN)
790Sstevel@tonic-gate 			errno = EWOULDBLOCK;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	return (ret);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
to_utmp(char * buf,char * nbuf,int len)850Sstevel@tonic-gate to_utmp(char *buf, char *nbuf, int len)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	struct compat_utmp *ut;
880Sstevel@tonic-gate 	struct utmpx *utx;
890Sstevel@tonic-gate 
90*3224Sraf 	utx = (struct utmpx *)nbuf;
91*3224Sraf 	ut  = (struct compat_utmp *)buf;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	while ((char *)utx < (nbuf + len)) {
940Sstevel@tonic-gate 		(void) strncpy(ut->ut_line, utx->ut_line, sizeof (ut->ut_line));
95*3224Sraf 		(void) strncpy(ut->ut_name, utx->ut_user, sizeof (ut->ut_name));
96*3224Sraf 		(void) strncpy(ut->ut_host, utx->ut_host, sizeof (ut->ut_host));
97*3224Sraf 		ut->ut_time = utx->ut_tv.tv_sec;
98*3224Sraf 		utx++;
99*3224Sraf 		ut++;
100*3224Sraf 	}
1010Sstevel@tonic-gate }
102