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