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