xref: /titanic_51/usr/src/lib/libbc/libc/sys/sys5/write.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 
34 /*
35  * If writing to a utmp-like file, map the utmp structure to
36  * new format on the fly.
37  */
38 extern int conv2utmpx(char *, char *, int);
39 
40 int
41 write(int fd, char *buf, int size)
42 {
43 	return (bc_write(fd, buf, size));
44 }
45 
46 int
47 bc_write(int fd, char *buf, int size)
48 {
49 	int ret, off;
50 	int nsize;
51 	char *nbuf;
52 
53 	if (fd_get(fd) != -1) {
54 		nsize = getmodsize(size, sizeof (struct compat_utmp),
55 		    sizeof (struct utmpx));
56 
57 		if ((nbuf = (void *)malloc(nsize)) == NULL) {
58 			(void) fprintf(stderr, "write: malloc failed\n");
59 			exit(-1);
60 		}
61 
62 		(void) memset(nbuf, 0, nsize);
63 
64 		ret = conv2utmpx(nbuf, buf, size);
65 
66 		if ((ret = _write(fd, nbuf, ret)) == -1) {
67 			free(nbuf);
68 			return (-1);
69 		}
70 
71 		free(nbuf);
72 
73 		ret = getmodsize(ret, sizeof (struct utmpx),
74 		    sizeof (struct compat_utmp));
75 		return (ret);
76 	}
77 
78 	return (_write(fd, buf, size));
79 }
80 
81 /* From SunOS/SVR4 utmp.h */
82 #define	USER_PROCESS 7
83 #define	DEAD_PROCESS 8
84 
85 extern int
86 conv2utmpx(char *nbuf, char *buf, int len)
87 {
88 	struct compat_utmp *ut;
89 	struct utmpx *utx;
90 
91 	utx = (struct utmpx *) nbuf;
92 	ut  = (struct compat_utmp  *) buf;
93 
94 	while ((char *)ut < (buf + len)) {
95                 (void) strcpy(utx->ut_user, ut->ut_name);
96 		(void) memset(utx->ut_id, 0, sizeof (utx->ut_id));
97 		(void) strcpy(utx->ut_line, ut->ut_line);
98 		utx->ut_pid = 0;
99 		if ((strcmp(utx->ut_user, "") == 0) &&
100 		    (strcmp(utx->ut_host, "") == 0))
101 			utx->ut_type = DEAD_PROCESS;
102 		else
103 			utx->ut_type = USER_PROCESS;
104 		utx->ut_exit.e_termination = 0;
105 		utx->ut_exit.e_exit = 0;
106 		utx->ut_tv.tv_sec = ut->ut_time;
107 		utx->ut_tv.tv_usec = 0;
108 		utx->ut_session = 0;
109 		utx->ut_syslen = sizeof (ut->ut_name) + 1;
110 		(void) strcpy(utx->ut_host, ut->ut_host);
111 		ut++;
112 		utx++;
113 	}
114 	return ((char *) utx - nbuf);
115 }
116