xref: /illumos-gate/usr/src/cmd/acct/acctcon1.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.18	*/
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	acctcon1 [-p] [-t] [-l file] [-o file] <wtmpx-file >ctmp-file
33*7c478bd9Sstevel@tonic-gate  *	-p	print input only, no processing
34*7c478bd9Sstevel@tonic-gate  *	-t	test mode: use latest time found in input, rather than
35*7c478bd9Sstevel@tonic-gate  *		current time when computing times of lines still on
36*7c478bd9Sstevel@tonic-gate  *		(only way to get repeatable data from old files)
37*7c478bd9Sstevel@tonic-gate  *	-l file	causes output of line usage summary
38*7c478bd9Sstevel@tonic-gate  *	-o file	causes first/last/reboots report to be written to file
39*7c478bd9Sstevel@tonic-gate  *	reads input (normally /var/adm/wtmpx), produces
40*7c478bd9Sstevel@tonic-gate  *	list of sessions, sorted by ending time in ctmp.h/ascii format
41*7c478bd9Sstevel@tonic-gate  *	A_TSIZE is max # distinct ttys
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
46*7c478bd9Sstevel@tonic-gate #include <stdio.h>
47*7c478bd9Sstevel@tonic-gate #include <ctype.h>
48*7c478bd9Sstevel@tonic-gate #include <time.h>
49*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
50*7c478bd9Sstevel@tonic-gate #include <locale.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int	a_tsize	= A_TSIZE;
53*7c478bd9Sstevel@tonic-gate int	tsize	= -1;	/* used slots in tbuf table */
54*7c478bd9Sstevel@tonic-gate struct  utmpx	wb;	/* record structure read into */
55*7c478bd9Sstevel@tonic-gate struct	ctmp	cb;	/* record structure written out of */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate struct tbuf {
58*7c478bd9Sstevel@tonic-gate 	char	tline[LSZ];	/* /dev/...  */
59*7c478bd9Sstevel@tonic-gate 	char	tname[NSZ];	/* user name */
60*7c478bd9Sstevel@tonic-gate 	time_t	ttime;		/* start time */
61*7c478bd9Sstevel@tonic-gate 	dev_t	tdev;		/* device */
62*7c478bd9Sstevel@tonic-gate 	int	tlsess;		/* # complete sessions */
63*7c478bd9Sstevel@tonic-gate 	int	tlon;		/* # times on (ut_type of 7) */
64*7c478bd9Sstevel@tonic-gate 	int	tloff;		/* # times off (ut_type != 7) */
65*7c478bd9Sstevel@tonic-gate 	long	ttotal;		/* total time used on this line */
66*7c478bd9Sstevel@tonic-gate } * tbuf;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #define DATE_FMT	"%a %b %e %H:%M:%S %Y\n"
69*7c478bd9Sstevel@tonic-gate int	nsys;
70*7c478bd9Sstevel@tonic-gate struct sys {
71*7c478bd9Sstevel@tonic-gate 	char	sname[LSZ];	/* reasons for ACCOUNTING records */
72*7c478bd9Sstevel@tonic-gate 	char	snum;		/* number of times encountered */
73*7c478bd9Sstevel@tonic-gate } sy[NSYS];
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate time_t	datetime;	/* old time if date changed, otherwise 0 */
76*7c478bd9Sstevel@tonic-gate time_t	firstime;
77*7c478bd9Sstevel@tonic-gate time_t	lastime;
78*7c478bd9Sstevel@tonic-gate int	ndates;		/* number of times date changed */
79*7c478bd9Sstevel@tonic-gate int	exitcode;
80*7c478bd9Sstevel@tonic-gate char	*report	= NULL;
81*7c478bd9Sstevel@tonic-gate char	*replin = NULL;
82*7c478bd9Sstevel@tonic-gate int	printonly;
83*7c478bd9Sstevel@tonic-gate int	tflag;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static char time_buf[50];
86*7c478bd9Sstevel@tonic-gate uid_t	namtouid();
87*7c478bd9Sstevel@tonic-gate dev_t	lintodev();
88*7c478bd9Sstevel@tonic-gate static size_t wread(void);
89*7c478bd9Sstevel@tonic-gate static int valid(void);
90*7c478bd9Sstevel@tonic-gate static void fixup(FILE *);
91*7c478bd9Sstevel@tonic-gate static void loop(void);
92*7c478bd9Sstevel@tonic-gate static void bootshut(void);
93*7c478bd9Sstevel@tonic-gate static int iline(void);
94*7c478bd9Sstevel@tonic-gate static void upall(void);
95*7c478bd9Sstevel@tonic-gate static void update(struct tbuf *);
96*7c478bd9Sstevel@tonic-gate static void printrep(void);
97*7c478bd9Sstevel@tonic-gate static void printlin(void);
98*7c478bd9Sstevel@tonic-gate static void prctmp(struct ctmp *);
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate int
101*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	char *prog = argv[0];
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
106*7c478bd9Sstevel@tonic-gate 	while (--argc > 0 && **++argv == '-')
107*7c478bd9Sstevel@tonic-gate 		switch(*++*argv) {
108*7c478bd9Sstevel@tonic-gate 		case 'l':
109*7c478bd9Sstevel@tonic-gate 			if (--argc > 0)
110*7c478bd9Sstevel@tonic-gate 				replin = *++argv;
111*7c478bd9Sstevel@tonic-gate 			continue;
112*7c478bd9Sstevel@tonic-gate 		case 'o':
113*7c478bd9Sstevel@tonic-gate 			if (--argc > 0)
114*7c478bd9Sstevel@tonic-gate 				report = *++argv;
115*7c478bd9Sstevel@tonic-gate 			continue;
116*7c478bd9Sstevel@tonic-gate 		case 'p':
117*7c478bd9Sstevel@tonic-gate 			printonly++;
118*7c478bd9Sstevel@tonic-gate 			continue;
119*7c478bd9Sstevel@tonic-gate 		case 't':
120*7c478bd9Sstevel@tonic-gate 			tflag++;
121*7c478bd9Sstevel@tonic-gate 			continue;
122*7c478bd9Sstevel@tonic-gate 		default:
123*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "usage: %s [-p] [-t] [-l lineuse] [-o reboot]\n", prog);
124*7c478bd9Sstevel@tonic-gate 			exit(1);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if ((tbuf = (struct tbuf *) calloc(a_tsize,
129*7c478bd9Sstevel@tonic-gate 		sizeof (struct tbuf))) == NULL) {
130*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: Cannot allocate memory\n");
131*7c478bd9Sstevel@tonic-gate 		exit(3);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (printonly) {
135*7c478bd9Sstevel@tonic-gate 		while (wread()) {
136*7c478bd9Sstevel@tonic-gate 			if (valid()) {
137*7c478bd9Sstevel@tonic-gate 				printf("%.*s\t%.*s\t%lu",
138*7c478bd9Sstevel@tonic-gate 				    sizeof (wb.ut_line),
139*7c478bd9Sstevel@tonic-gate 				    wb.ut_line,
140*7c478bd9Sstevel@tonic-gate 				    sizeof (wb.ut_name),
141*7c478bd9Sstevel@tonic-gate 				    wb.ut_name,
142*7c478bd9Sstevel@tonic-gate 				    wb.ut_xtime);
143*7c478bd9Sstevel@tonic-gate 				cftime(time_buf, DATE_FMT, &wb.ut_xtime);
144*7c478bd9Sstevel@tonic-gate 				printf("\t%s", time_buf);
145*7c478bd9Sstevel@tonic-gate 			} else
146*7c478bd9Sstevel@tonic-gate 				fixup(stdout);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 		exit(exitcode);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	while (wread()) {
153*7c478bd9Sstevel@tonic-gate 		if (firstime == 0)
154*7c478bd9Sstevel@tonic-gate 			firstime = wb.ut_xtime;
155*7c478bd9Sstevel@tonic-gate 		if (valid())
156*7c478bd9Sstevel@tonic-gate 			loop();
157*7c478bd9Sstevel@tonic-gate 		else
158*7c478bd9Sstevel@tonic-gate 			fixup(stderr);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	wb.ut_name[0] = '\0';
161*7c478bd9Sstevel@tonic-gate 	strcpy(wb.ut_line, "acctcon1");
162*7c478bd9Sstevel@tonic-gate 	wb.ut_type = ACCOUNTING;
163*7c478bd9Sstevel@tonic-gate 	if (tflag)
164*7c478bd9Sstevel@tonic-gate 		wb.ut_xtime = lastime;
165*7c478bd9Sstevel@tonic-gate 	else
166*7c478bd9Sstevel@tonic-gate 		time(&wb.ut_xtime);
167*7c478bd9Sstevel@tonic-gate 	loop();
168*7c478bd9Sstevel@tonic-gate 	if (report != NULL)
169*7c478bd9Sstevel@tonic-gate 		printrep();
170*7c478bd9Sstevel@tonic-gate 	if (replin != NULL)
171*7c478bd9Sstevel@tonic-gate 		printlin();
172*7c478bd9Sstevel@tonic-gate 	exit(exitcode);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate static size_t
176*7c478bd9Sstevel@tonic-gate wread()
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	return (fread(&wb, sizeof(wb), 1, stdin) == 1);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate  * valid: check input wtmp record, return 1 if looks OK
184*7c478bd9Sstevel@tonic-gate  */
185*7c478bd9Sstevel@tonic-gate static int
186*7c478bd9Sstevel@tonic-gate valid()
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	int i, c;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	/* XPG say that user names should not start with a "-". */
191*7c478bd9Sstevel@tonic-gate         if ((c = wb.ut_name[0]) == '-')
192*7c478bd9Sstevel@tonic-gate 		return(0);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
195*7c478bd9Sstevel@tonic-gate 		c = wb.ut_name[i];
196*7c478bd9Sstevel@tonic-gate 		if (isalnum(c) || c == '$' || c == ' ' || c == '_' || c == '-')
197*7c478bd9Sstevel@tonic-gate 			continue;
198*7c478bd9Sstevel@tonic-gate 		else if (c == '\0')
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 		else
201*7c478bd9Sstevel@tonic-gate 			return(0);
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	if((wb.ut_type >= EMPTY) && (wb.ut_type <= UTMAXTYPE))
205*7c478bd9Sstevel@tonic-gate 		return(1);
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	return(0);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate  *	fixup assumes that V6 wtmp (16 bytes long) is mixed in with
212*7c478bd9Sstevel@tonic-gate  *	V7 records (20 bytes each)
213*7c478bd9Sstevel@tonic-gate  *
214*7c478bd9Sstevel@tonic-gate  *	Starting with Release 5.0 of UNIX, this routine will no
215*7c478bd9Sstevel@tonic-gate  *	longer reset the read pointer.  This has a snowball effect
216*7c478bd9Sstevel@tonic-gate  *	On the following records until the offset corrects itself.
217*7c478bd9Sstevel@tonic-gate  *	If a message is printed from here, it should be regarded as
218*7c478bd9Sstevel@tonic-gate  *	a bad record and not as a V6 record.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate static void
221*7c478bd9Sstevel@tonic-gate fixup(FILE *stream)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad wtmpx: offset %lu.\n", ftell(stdin)-sizeof(wb));
224*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad record is:  %.*s\t%.*s\t%lu",
225*7c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_line),
226*7c478bd9Sstevel@tonic-gate 	    wb.ut_line,
227*7c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_name),
228*7c478bd9Sstevel@tonic-gate 	    wb.ut_name,
229*7c478bd9Sstevel@tonic-gate 	    wb.ut_xtime);
230*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &wb.ut_xtime);
231*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "\t%s", time_buf);
232*7c478bd9Sstevel@tonic-gate #ifdef	V6
233*7c478bd9Sstevel@tonic-gate 	fseek(stdin, (long)-4, 1);
234*7c478bd9Sstevel@tonic-gate #endif
235*7c478bd9Sstevel@tonic-gate 	exitcode = 1;
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate static void
239*7c478bd9Sstevel@tonic-gate loop()
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	int timediff;
242*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	if(wb.ut_line[0] == '\0' )	/* It's an init admin process */
245*7c478bd9Sstevel@tonic-gate 		return;			/* no connect accounting data here */
246*7c478bd9Sstevel@tonic-gate 	switch(wb.ut_type) {
247*7c478bd9Sstevel@tonic-gate 	case OLD_TIME:
248*7c478bd9Sstevel@tonic-gate 		datetime = wb.ut_xtime;
249*7c478bd9Sstevel@tonic-gate 		return;
250*7c478bd9Sstevel@tonic-gate 	case NEW_TIME:
251*7c478bd9Sstevel@tonic-gate 		if(datetime == 0)
252*7c478bd9Sstevel@tonic-gate 			return;
253*7c478bd9Sstevel@tonic-gate 		timediff = wb.ut_xtime - datetime;
254*7c478bd9Sstevel@tonic-gate 		for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
255*7c478bd9Sstevel@tonic-gate 			tp->ttime += timediff;
256*7c478bd9Sstevel@tonic-gate 		datetime = 0;
257*7c478bd9Sstevel@tonic-gate 		ndates++;
258*7c478bd9Sstevel@tonic-gate 		return;
259*7c478bd9Sstevel@tonic-gate 	case BOOT_TIME:
260*7c478bd9Sstevel@tonic-gate 		upall();
261*7c478bd9Sstevel@tonic-gate 	case ACCOUNTING:
262*7c478bd9Sstevel@tonic-gate 	case RUN_LVL:
263*7c478bd9Sstevel@tonic-gate 		lastime = wb.ut_xtime;
264*7c478bd9Sstevel@tonic-gate 		bootshut();
265*7c478bd9Sstevel@tonic-gate 		return;
266*7c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
267*7c478bd9Sstevel@tonic-gate 	case LOGIN_PROCESS:
268*7c478bd9Sstevel@tonic-gate 	case INIT_PROCESS:
269*7c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:
270*7c478bd9Sstevel@tonic-gate 		update(&tbuf[iline()]);
271*7c478bd9Sstevel@tonic-gate 		return;
272*7c478bd9Sstevel@tonic-gate 	case EMPTY:
273*7c478bd9Sstevel@tonic-gate 		return;
274*7c478bd9Sstevel@tonic-gate 	default:
275*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &wb.ut_xtime);
276*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: invalid type %d for %s %s %s",
277*7c478bd9Sstevel@tonic-gate 		    wb.ut_type,
278*7c478bd9Sstevel@tonic-gate 		    wb.ut_name,
279*7c478bd9Sstevel@tonic-gate 		    wb.ut_line,
280*7c478bd9Sstevel@tonic-gate 		    time_buf);
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate /*
285*7c478bd9Sstevel@tonic-gate  * bootshut: record reboot (or shutdown)
286*7c478bd9Sstevel@tonic-gate  * bump count, looking up wb.ut_line in sy table
287*7c478bd9Sstevel@tonic-gate  */
288*7c478bd9Sstevel@tonic-gate static void
289*7c478bd9Sstevel@tonic-gate bootshut()
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	int i;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
294*7c478bd9Sstevel@tonic-gate 		;
295*7c478bd9Sstevel@tonic-gate 	if (i >= nsys) {
296*7c478bd9Sstevel@tonic-gate 		if (++nsys > NSYS) {
297*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
298*7c478bd9Sstevel@tonic-gate 				"acctcon1: recompile with larger NSYS\n");
299*7c478bd9Sstevel@tonic-gate 			nsys = NSYS;
300*7c478bd9Sstevel@tonic-gate 			return;
301*7c478bd9Sstevel@tonic-gate 		}
302*7c478bd9Sstevel@tonic-gate 		CPYN(sy[i].sname, wb.ut_line);
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 	sy[i].snum++;
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate /*
308*7c478bd9Sstevel@tonic-gate  * iline: look up/enter current line name in tbuf, return index
309*7c478bd9Sstevel@tonic-gate  * (used to avoid system dependencies on naming)
310*7c478bd9Sstevel@tonic-gate  */
311*7c478bd9Sstevel@tonic-gate static int
312*7c478bd9Sstevel@tonic-gate iline()
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	int i;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= tsize; i++)
317*7c478bd9Sstevel@tonic-gate 		if (EQN(wb.ut_line, tbuf[i].tline))
318*7c478bd9Sstevel@tonic-gate 			return(i);
319*7c478bd9Sstevel@tonic-gate 	if (++tsize >= a_tsize) {
320*7c478bd9Sstevel@tonic-gate 		a_tsize = a_tsize + A_TSIZE;
321*7c478bd9Sstevel@tonic-gate 		if ((tbuf = (struct tbuf *) realloc(tbuf, a_tsize *
322*7c478bd9Sstevel@tonic-gate 			sizeof (struct tbuf))) == NULL) {
323*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctcon1: Cannot reallocate memory\n");
324*7c478bd9Sstevel@tonic-gate 			exit(2);
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 	}
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	CPYN(tbuf[tsize].tline, wb.ut_line);
329*7c478bd9Sstevel@tonic-gate 	tbuf[tsize].tdev = lintodev(wb.ut_line);
330*7c478bd9Sstevel@tonic-gate 	return(tsize);
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate static void
334*7c478bd9Sstevel@tonic-gate upall()
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	wb.ut_type = INIT_PROCESS;	/* fudge a logoff for reboot record */
339*7c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
340*7c478bd9Sstevel@tonic-gate 		update(tp);
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate /*
344*7c478bd9Sstevel@tonic-gate  * update tbuf with new time, write ctmp record for end of session
345*7c478bd9Sstevel@tonic-gate  */
346*7c478bd9Sstevel@tonic-gate static void
347*7c478bd9Sstevel@tonic-gate update(struct tbuf *tp)
348*7c478bd9Sstevel@tonic-gate {
349*7c478bd9Sstevel@tonic-gate 	time_t	told,	/* last time for tbuf record */
350*7c478bd9Sstevel@tonic-gate 		tnew;	/* time of this record */
351*7c478bd9Sstevel@tonic-gate 			/* Difference is connect time */
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	told = tp->ttime;
354*7c478bd9Sstevel@tonic-gate 	tnew = wb.ut_xtime;
355*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &told);
356*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "The old time is: %s", time_buf);
357*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &tnew);
358*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "the new time is: %s", time_buf);
359*7c478bd9Sstevel@tonic-gate 	if (told > tnew) {
360*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &told);
361*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: bad times: old: %s", time_buf);
362*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &tnew);
363*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "new: %s", time_buf);
364*7c478bd9Sstevel@tonic-gate 		exitcode = 1;
365*7c478bd9Sstevel@tonic-gate 		tp->ttime = tnew;
366*7c478bd9Sstevel@tonic-gate 		return;
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	tp->ttime = tnew;
369*7c478bd9Sstevel@tonic-gate 	switch(wb.ut_type) {
370*7c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
371*7c478bd9Sstevel@tonic-gate 		tp->tlsess++;
372*7c478bd9Sstevel@tonic-gate 		if(tp->tname[0] != '\0') { /* Someone logged in without */
373*7c478bd9Sstevel@tonic-gate 					   /* logging off. Put out record. */
374*7c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
375*7c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
376*7c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
377*7c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
378*7c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
379*7c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
380*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon1: could not calculate prime/non-prime hours\n");
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 				exit(1);
383*7c478bd9Sstevel@tonic-gate 			}
384*7c478bd9Sstevel@tonic-gate 			prctmp(&cb);
385*7c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
386*7c478bd9Sstevel@tonic-gate 		}
387*7c478bd9Sstevel@tonic-gate 		else	/* Someone just logged in */
388*7c478bd9Sstevel@tonic-gate 			tp->tlon++;
389*7c478bd9Sstevel@tonic-gate 		CPYN(tp->tname, wb.ut_name);
390*7c478bd9Sstevel@tonic-gate 		break;
391*7c478bd9Sstevel@tonic-gate 	case INIT_PROCESS:
392*7c478bd9Sstevel@tonic-gate 	case LOGIN_PROCESS:
393*7c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:
394*7c478bd9Sstevel@tonic-gate 		tp->tloff++;
395*7c478bd9Sstevel@tonic-gate 		if(tp->tname[0] != '\0') { /* Someone logged off */
396*7c478bd9Sstevel@tonic-gate 			/* Set up and print ctmp record */
397*7c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
398*7c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
399*7c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
400*7c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
401*7c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
402*7c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
403*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon1: could not calculate prime/non-prime hours\n");
404*7c478bd9Sstevel@tonic-gate 				exit(1);
405*7c478bd9Sstevel@tonic-gate 			}
406*7c478bd9Sstevel@tonic-gate 			prctmp(&cb);
407*7c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
408*7c478bd9Sstevel@tonic-gate 			tp->tname[0] = '\0';
409*7c478bd9Sstevel@tonic-gate 		}
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate static void
414*7c478bd9Sstevel@tonic-gate printrep()
415*7c478bd9Sstevel@tonic-gate {
416*7c478bd9Sstevel@tonic-gate 	int i;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	freopen(report, "w", stdout);
419*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &firstime);
420*7c478bd9Sstevel@tonic-gate 	printf("from %s", time_buf);
421*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &lastime);
422*7c478bd9Sstevel@tonic-gate 	printf("to   %s", time_buf);
423*7c478bd9Sstevel@tonic-gate 	if (ndates)
424*7c478bd9Sstevel@tonic-gate 		printf("%d\tdate change%c\n",ndates,(ndates>1 ? 's' : '\0'));
425*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
426*7c478bd9Sstevel@tonic-gate 		printf("%d\t%.*s\n", sy[i].snum,
427*7c478bd9Sstevel@tonic-gate 		    sizeof (sy[i].sname), sy[i].sname);
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate /*
431*7c478bd9Sstevel@tonic-gate  *	print summary of line usage
432*7c478bd9Sstevel@tonic-gate  *	accuracy only guaranteed for wtmpx file started fresh
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate static void
435*7c478bd9Sstevel@tonic-gate printlin()
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
438*7c478bd9Sstevel@tonic-gate 	double timet, timei;
439*7c478bd9Sstevel@tonic-gate 	double ttime;
440*7c478bd9Sstevel@tonic-gate 	int tsess, ton, toff;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	freopen(replin, "w", stdout);
443*7c478bd9Sstevel@tonic-gate 	ttime = 0.0;
444*7c478bd9Sstevel@tonic-gate 	tsess = ton = toff = 0;
445*7c478bd9Sstevel@tonic-gate 	timet = MINS(lastime-firstime);
446*7c478bd9Sstevel@tonic-gate 	printf("TOTAL DURATION IS %.0f MINUTES\n", timet);
447*7c478bd9Sstevel@tonic-gate 	printf("LINE         MINUTES  PERCENT  # SESS  # ON  # OFF\n");
448*7c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++) {
449*7c478bd9Sstevel@tonic-gate 		timei = MINS(tp->ttotal);
450*7c478bd9Sstevel@tonic-gate 		ttime += timei;
451*7c478bd9Sstevel@tonic-gate 		tsess += tp->tlsess;
452*7c478bd9Sstevel@tonic-gate 		ton += tp->tlon;
453*7c478bd9Sstevel@tonic-gate 		toff += tp->tloff;
454*7c478bd9Sstevel@tonic-gate 		printf("%-*.*s %-7.0f  %-7.0f  %-6d  %-4d  %-5d\n",
455*7c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
456*7c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
457*7c478bd9Sstevel@tonic-gate 		    tp->tline,
458*7c478bd9Sstevel@tonic-gate 		    timei,
459*7c478bd9Sstevel@tonic-gate 		    (timet > 0.)? 100*timei/timet : 0.,
460*7c478bd9Sstevel@tonic-gate 		    tp->tlsess,
461*7c478bd9Sstevel@tonic-gate 		    tp->tlon,
462*7c478bd9Sstevel@tonic-gate 		    tp->tloff);
463*7c478bd9Sstevel@tonic-gate 	}
464*7c478bd9Sstevel@tonic-gate 	printf("TOTALS       %-7.0f  --       %-6d  %-4d  %-5d\n",
465*7c478bd9Sstevel@tonic-gate 	    ttime, tsess, ton, toff);
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate static void
469*7c478bd9Sstevel@tonic-gate prctmp(struct ctmp *t)
470*7c478bd9Sstevel@tonic-gate {
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	printf("%u\t%ld\t%.*s\t%lu\t%lu\t%lu",
473*7c478bd9Sstevel@tonic-gate 	    t->ct_tty,
474*7c478bd9Sstevel@tonic-gate 	    t->ct_uid,
475*7c478bd9Sstevel@tonic-gate 	    OUTPUT_NSZ,
476*7c478bd9Sstevel@tonic-gate 	    t->ct_name,
477*7c478bd9Sstevel@tonic-gate 	    t->ct_con[0],
478*7c478bd9Sstevel@tonic-gate 	    t->ct_con[1],
479*7c478bd9Sstevel@tonic-gate 	    t->ct_start);
480*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &t->ct_start);
481*7c478bd9Sstevel@tonic-gate 	printf("\t%s", time_buf);
482*7c478bd9Sstevel@tonic-gate }
483