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