xref: /titanic_44/usr/src/cmd/acct/fwtmp.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include "acctdef.h"
35 #include <utmpx.h>
36 #include <locale.h>
37 
38 struct	utmpx	Ut;
39 static char time_buf[50];
40 
41 static int inp(FILE *, struct utmpx *);
42 
43 int
44 main(int c, char **v)
45 {
46 
47 	int	iflg, cflg;
48 
49 	(void) setlocale(LC_ALL, "");
50 
51 	iflg = cflg = 0;
52 
53 	while (--c > 0) {
54 		if (**++v == '-')
55 			while (*++*v)
56 				switch (**v) {
57 				case 'c':
58 					cflg++;
59 					continue;
60 				case 'i':
61 					iflg++;
62 					continue;
63 				}
64 	}
65 
66 	for (;;) {
67 		if (iflg) {
68 			if (inp(stdin, &Ut) == EOF)
69 				break;
70 		} else {
71 			if (fread(&Ut, sizeof (Ut), 1, stdin) != 1)
72 				break;
73 		}
74 		if (cflg)
75 			fwrite(&Ut, sizeof (Ut), 1, stdout);
76 		else {
77 			cftime(time_buf, DATE_FMT, &Ut.ut_xtime);
78 			printf("%-*.*s %-4.4s %-*.*s %9d %2hd "
79 			    "%4.4ho %4.4ho %ld %ld %d %hd %-.*s %s",
80 			    NSZ,
81 			    NSZ,
82 			    Ut.ut_name,
83 			    Ut.ut_id,
84 			    LINESZ,
85 			    LINESZ,
86 			    Ut.ut_line,
87 			    Ut.ut_pid,
88 			    Ut.ut_type,
89 			    Ut.ut_exit.e_termination,
90 			    Ut.ut_exit.e_exit,
91 			    Ut.ut_xtime,
92 			    Ut.ut_tv.tv_usec,
93 			    Ut.ut_session,
94 			    Ut.ut_syslen,
95 			    Ut.ut_syslen,
96 			    Ut.ut_host,
97 			    time_buf);
98 		}
99 	}
100 	exit(0);
101 }
102 
103 static int
104 inp(FILE *file, struct utmpx *u)
105 {
106 
107 	char	buf[BUFSIZ];
108 	char *p;
109 	int i;
110 
111 	if (fgets((p = buf), BUFSIZ, file) == NULL)
112 		return (EOF);
113 
114 	for (i = 0; i < NSZ; i++)	/* Allow a space in name field */
115 		u->ut_name[i] = *p++;
116 	for (i = NSZ-1; i >= 0; i--) {
117 		if (u->ut_name[i] == ' ')
118 			u->ut_name[i] = '\0';
119 		else
120 			break;
121 	}
122 	p++;
123 
124 	for (i = 0; i < 4; i++)
125 		if ((u->ut_id[i] = *p++) == ' ')
126 			u->ut_id[i] = '\0';
127 	p++;
128 
129 	for (i = 0; i < LINESZ; i++)	/* Allow a space in line field */
130 		u->ut_line[i] = *p++;
131 	for (i = LINESZ-1; i >= 0; i--) {
132 		if (u->ut_line[i] == ' ')
133 			u->ut_line[i] = '\0';
134 		else
135 			break;
136 	}
137 
138 	sscanf(p, "%d %hd %ho %ho %ld %ld %d %hd",
139 		&u->ut_pid,
140 		&u->ut_type,
141 		&u->ut_exit.e_termination,
142 		&u->ut_exit.e_exit,
143 		&u->ut_xtime,
144 		&u->ut_tv.tv_usec,
145 		&u->ut_session,
146 		&u->ut_syslen);
147 
148 	if (u->ut_syslen > 1)
149 		sscanf(p, "%*d %*hd %*ho %*ho %*ld %*ld %*d %*hd %s",
150 		    u->ut_host);
151 	else
152 		u->ut_host[0] = '\0';
153 
154 	return (1);
155 }
156