8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbc / libc / sys / 4.2 / write.c
blob4180aacbe7b05f08738c4d9fc1afec27971e7315
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "../common/compat.h"
30 #include <stdio.h>
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <unistd.h>
36 * If writing to a utmp-like file, map the utmp structure to
37 * new format on the fly.
39 extern int errno;
41 extern int conv2utmp(char *, char *, int);
42 extern int conv2utmpx(char *, char *, int);
44 int
45 write(int fd, char *buf, int size)
47 return (bc_write(fd, buf, size));
50 int
51 bc_write(int fd, char *buf, int size)
53 int ret, off;
54 int nsize;
55 char *nbuf;
57 if (fd_get(fd) != -1) { /* writing utmp (utmpx actually) */
58 nsize = getmodsize(size, sizeof (struct compat_utmp),
59 sizeof (struct utmpx));
61 if ((nbuf = (void *)malloc(nsize)) == NULL) {
62 (void) fprintf(stderr, "write: malloc failed\n");
63 exit(-1);
66 (void) memset(nbuf, 0, nsize);
68 ret = conv2utmpx(nbuf, buf, size);
70 if ((ret = _write(fd, nbuf, ret)) == -1) {
71 if (errno == EAGAIN)
72 errno = EWOULDBLOCK;
73 free(nbuf);
74 return (-1);
77 free(nbuf);
79 ret = getmodsize(ret, sizeof (struct utmpx),
80 sizeof (struct compat_utmp));
82 return (ret);
85 if ((ret = _write(fd, buf, size)) == -1) {
86 if (errno == EAGAIN)
87 errno = EWOULDBLOCK;
89 return (ret);
93 /* From SunOS/SVR4 utmp.h */
94 #define USER_PROCESS 7
95 #define DEAD_PROCESS 8
97 extern int
98 conv2utmpx(char *nbuf, char *buf, int len)
100 struct compat_utmp *ut;
101 struct utmpx *utx;
103 utx = (struct utmpx *) nbuf;
104 ut = (struct compat_utmp *) buf;
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++;
126 return ((char *) utx - nbuf);