xref: /onnv-gate/usr/src/cmd/acct/acctcon.c (revision 428:a261479c3dbd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  *	acctcon [-l file] [-o file] <wtmpx-file
350Sstevel@tonic-gate  *	-l file	causes output of line usage summary
360Sstevel@tonic-gate  *	-o file	causes first/last/reboots report to be written to file
370Sstevel@tonic-gate  *	reads input (normally /var/adm/wtmpx), produces
380Sstevel@tonic-gate  *	list of sessions, sorted by ending time in tacct.h format
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include "acctdef.h"
450Sstevel@tonic-gate #include <ctype.h>
460Sstevel@tonic-gate #include <time.h>
470Sstevel@tonic-gate #include <utmpx.h>
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include <string.h>
500Sstevel@tonic-gate #include <search.h>
51*428Ssl108498 #include <stdlib.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int   a_tsize = A_TSIZE;
540Sstevel@tonic-gate int	tsize	= -1;	/* highest index of used slot in tbuf table */
55*428Ssl108498 static	int csize;
560Sstevel@tonic-gate struct  utmpx	wb;	/* record structure read into */
570Sstevel@tonic-gate struct	ctmp	cb;	/* record structure written out of */
580Sstevel@tonic-gate struct	tacct	tb;
590Sstevel@tonic-gate double	timet, timei;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate struct tbuf {
620Sstevel@tonic-gate 	char	tline[LSZ];	/* /dev/...  */
630Sstevel@tonic-gate 	char	tname[NSZ];	/* user name */
640Sstevel@tonic-gate 	time_t	ttime;		/* start time */
650Sstevel@tonic-gate 	dev_t	tdev;		/* device */
660Sstevel@tonic-gate 	int	tlsess;		/* # complete sessions */
670Sstevel@tonic-gate 	int	tlon;		/* # times on (ut_type of 7) */
680Sstevel@tonic-gate 	int	tloff;		/* # times off (ut_type != 7) */
690Sstevel@tonic-gate 	long	ttotal;		/* total time used on this line */
700Sstevel@tonic-gate } *tbuf;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate struct ctab {
730Sstevel@tonic-gate 	uid_t		ct_uid;
740Sstevel@tonic-gate 	char		ct_name[NSZ];
750Sstevel@tonic-gate 	long 		ct_con[2];
760Sstevel@tonic-gate 	ushort_t	ct_sess;
770Sstevel@tonic-gate } *pctab;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate int	nsys;
800Sstevel@tonic-gate struct sys {
810Sstevel@tonic-gate 	char	sname[LSZ];	/* reasons for ACCOUNTING records */
820Sstevel@tonic-gate 	char	snum;		/* number of times encountered */
830Sstevel@tonic-gate } sy[NSYS];
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static char time_buf[50];
860Sstevel@tonic-gate time_t	datetime;	/* old time if date changed, otherwise 0 */
870Sstevel@tonic-gate time_t	firstime;
880Sstevel@tonic-gate time_t	lastime;
890Sstevel@tonic-gate int	ndates;		/* number of times date changed */
900Sstevel@tonic-gate int	exitcode;
910Sstevel@tonic-gate char	*report	= NULL;
920Sstevel@tonic-gate char	*replin = NULL;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate uid_t	namtouid();
950Sstevel@tonic-gate dev_t	lintodev();
960Sstevel@tonic-gate static int valid(void);
970Sstevel@tonic-gate static void fixup(FILE *);
980Sstevel@tonic-gate static void loop(void);
990Sstevel@tonic-gate static void bootshut(void);
1000Sstevel@tonic-gate static int iline(void);
1010Sstevel@tonic-gate static void upall(void);
1020Sstevel@tonic-gate static void update(struct tbuf *);
1030Sstevel@tonic-gate static void printrep(void);
1040Sstevel@tonic-gate static void printlin(void);
1050Sstevel@tonic-gate static int tcmp(struct tbuf *, struct tbuf *);
1060Sstevel@tonic-gate static int node_compare(const void *, const void *);
1070Sstevel@tonic-gate static void enter(struct ctmp *);
1080Sstevel@tonic-gate static void print_node(const void *, VISIT, int);
1090Sstevel@tonic-gate static void output(void);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate extern char 	*optarg;
1120Sstevel@tonic-gate extern int	optind;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate void **root = NULL;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate int
1170Sstevel@tonic-gate main(int argc, char **argv)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	int c;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1220Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "l:o:")) != EOF)
1230Sstevel@tonic-gate 		switch (c) {
1240Sstevel@tonic-gate 		case 'l':
1250Sstevel@tonic-gate 			replin = optarg;
1260Sstevel@tonic-gate 			break;
1270Sstevel@tonic-gate 		case 'o':
1280Sstevel@tonic-gate 			report = optarg;
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 		case '?':
1310Sstevel@tonic-gate 			fprintf(stderr, "usage: %s [-l lineuse] "
1320Sstevel@tonic-gate 			    "[-o reboot]\n", argv[0]);
1330Sstevel@tonic-gate 			exit(1);
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if ((tbuf = (struct tbuf *)calloc(a_tsize,
1370Sstevel@tonic-gate 		sizeof (struct tbuf))) == NULL) {
1380Sstevel@tonic-gate 		fprintf(stderr, "acctcon: Cannot allocate memory\n");
1390Sstevel@tonic-gate 		exit(3);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * XXX - fixme - need a good way of getting the fd that getutxent would
1440Sstevel@tonic-gate 	 * use to access wtmpx, so we can convert this read of stdin to use
1450Sstevel@tonic-gate 	 * the APIs and remove the dependence on the existence of the file.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	while (fread(&wb, sizeof (wb), 1, stdin) == 1) {
1480Sstevel@tonic-gate 		if (firstime == 0)
1490Sstevel@tonic-gate 			firstime = wb.ut_xtime;
1500Sstevel@tonic-gate 		if (valid())
1510Sstevel@tonic-gate 			loop();
1520Sstevel@tonic-gate 		else
1530Sstevel@tonic-gate 			fixup(stderr);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	wb.ut_name[0] = '\0';
1560Sstevel@tonic-gate 	strcpy(wb.ut_line, "acctcon");
1570Sstevel@tonic-gate 	wb.ut_type = ACCOUNTING;
1580Sstevel@tonic-gate 	wb.ut_xtime = lastime;
1590Sstevel@tonic-gate 	loop();
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	output();
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (report != NULL)
1640Sstevel@tonic-gate 		printrep();
1650Sstevel@tonic-gate 	if (replin != NULL)
1660Sstevel@tonic-gate 		printlin();
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	exit(exitcode);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * valid: check input wtmpx record, return 1 if looks OK
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate static int
1760Sstevel@tonic-gate valid()
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	int i, c;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* XPG say that user names should not start with a "-" */
1810Sstevel@tonic-gate 	if ((c = wb.ut_name[0]) == '-')
1820Sstevel@tonic-gate 		return (0);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
1850Sstevel@tonic-gate 		c = wb.ut_name[i];
1860Sstevel@tonic-gate 		if (isalnum(c) || c == '$' || c == ' ' || c == '.' ||
1870Sstevel@tonic-gate 			c == '_' || c == '-')
1880Sstevel@tonic-gate 			continue;
1890Sstevel@tonic-gate 		else if (c == '\0')
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 		else
1920Sstevel@tonic-gate 			return (0);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if ((wb.ut_type >= EMPTY) && (wb.ut_type <= UTMAXTYPE))
1960Sstevel@tonic-gate 		return (1);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return (0);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static void
2020Sstevel@tonic-gate fixup(FILE *stream)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	fprintf(stream, "bad wtmpx: offset %lu.\n", ftell(stdin)-sizeof (wb));
2050Sstevel@tonic-gate 	fprintf(stream, "bad record is:  %.*s\t%.*s\t%lu",
2060Sstevel@tonic-gate 	    sizeof (wb.ut_line),
2070Sstevel@tonic-gate 	    wb.ut_line,
2080Sstevel@tonic-gate 	    sizeof (wb.ut_name),
2090Sstevel@tonic-gate 	    wb.ut_name,
2100Sstevel@tonic-gate 	    wb.ut_xtime);
2110Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2120Sstevel@tonic-gate 	fprintf(stream, "\t%s", time_buf);
2130Sstevel@tonic-gate 	exitcode = 1;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static void
2170Sstevel@tonic-gate loop()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	int timediff;
2200Sstevel@tonic-gate 	struct tbuf *tp;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (wb.ut_line[0] == '\0')	/* It's an init admin process */
2230Sstevel@tonic-gate 		return;			/* no connect accounting data here */
2240Sstevel@tonic-gate 	switch (wb.ut_type) {
2250Sstevel@tonic-gate 	case OLD_TIME:
2260Sstevel@tonic-gate 		datetime = wb.ut_xtime;
2270Sstevel@tonic-gate 		return;
2280Sstevel@tonic-gate 	case NEW_TIME:
2290Sstevel@tonic-gate 		if (datetime == 0)
2300Sstevel@tonic-gate 			return;
2310Sstevel@tonic-gate 		timediff = wb.ut_xtime - datetime;
2320Sstevel@tonic-gate 		for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
2330Sstevel@tonic-gate 			tp->ttime += timediff;
2340Sstevel@tonic-gate 		datetime = 0;
2350Sstevel@tonic-gate 		ndates++;
2360Sstevel@tonic-gate 		return;
2370Sstevel@tonic-gate 	case BOOT_TIME:
2380Sstevel@tonic-gate 		upall();
2390Sstevel@tonic-gate 	case ACCOUNTING:
2400Sstevel@tonic-gate 	case RUN_LVL:
2410Sstevel@tonic-gate 		lastime = wb.ut_xtime;
2420Sstevel@tonic-gate 		bootshut();
2430Sstevel@tonic-gate 		return;
2440Sstevel@tonic-gate 	case USER_PROCESS:
2450Sstevel@tonic-gate 	case LOGIN_PROCESS:
2460Sstevel@tonic-gate 	case INIT_PROCESS:
2470Sstevel@tonic-gate 	case DEAD_PROCESS:	/* WHCC mod 3/86  */
2480Sstevel@tonic-gate 		update(&tbuf[iline()]);
2490Sstevel@tonic-gate 		return;
2500Sstevel@tonic-gate 	case EMPTY:
2510Sstevel@tonic-gate 		return;
2520Sstevel@tonic-gate 	default:
2530Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2540Sstevel@tonic-gate 		fprintf(stderr, "acctcon: invalid type %d for %s %s %s",
2550Sstevel@tonic-gate 			wb.ut_type,
2560Sstevel@tonic-gate 			wb.ut_name,
2570Sstevel@tonic-gate 			wb.ut_line,
2580Sstevel@tonic-gate 			time_buf);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * bootshut: record reboot (or shutdown)
2640Sstevel@tonic-gate  * bump count, looking up wb.ut_line in sy table
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate static void
2670Sstevel@tonic-gate bootshut()
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	int i;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
2720Sstevel@tonic-gate 		;
2730Sstevel@tonic-gate 	if (i >= nsys) {
2740Sstevel@tonic-gate 		if (++nsys > NSYS) {
2750Sstevel@tonic-gate 			fprintf(stderr,
2760Sstevel@tonic-gate 				"acctcon: recompile with larger NSYS\n");
2770Sstevel@tonic-gate 			nsys = NSYS;
2780Sstevel@tonic-gate 			return;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		CPYN(sy[i].sname, wb.ut_line);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	sy[i].snum++;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * iline: look up/enter current line name in tbuf, return index
2870Sstevel@tonic-gate  * (used to avoid system dependencies on naming)
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate static int
2900Sstevel@tonic-gate iline()
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	int i;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	for (i = 0; i <= tsize; i++)
2950Sstevel@tonic-gate 		if (EQN(wb.ut_line, tbuf[i].tline))
2960Sstevel@tonic-gate 			return (i);
2970Sstevel@tonic-gate 	if (++tsize >= a_tsize) {
2980Sstevel@tonic-gate 		a_tsize = a_tsize + A_TSIZE;
2990Sstevel@tonic-gate 		if ((tbuf = (struct tbuf *)realloc(tbuf, a_tsize *
3000Sstevel@tonic-gate 			sizeof (struct tbuf))) == NULL) {
3010Sstevel@tonic-gate 			fprintf(stderr, "acctcon: Cannot reallocate memory\n");
3020Sstevel@tonic-gate 			exit(2);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	CPYN(tbuf[tsize].tline, wb.ut_line);
3070Sstevel@tonic-gate 	tbuf[tsize].tdev = lintodev(wb.ut_line);
3080Sstevel@tonic-gate 	return (tsize);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate static void
3120Sstevel@tonic-gate upall()
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	struct tbuf *tp;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	wb.ut_type = DEAD_PROCESS;	/* fudge a logoff for reboot record. */
3170Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
3180Sstevel@tonic-gate 		update(tp);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * update tbuf with new time, write ctmp record for end of session
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate static void
3250Sstevel@tonic-gate update(struct tbuf *tp)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	time_t	told,	/* last time for tbuf record */
3280Sstevel@tonic-gate 		tnew;	/* time of this record */
3290Sstevel@tonic-gate 			/* Difference is connect time */
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	told = tp->ttime;
3320Sstevel@tonic-gate 	tnew = wb.ut_xtime;
3330Sstevel@tonic-gate 	if (told > tnew) {
3340Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &told);
3350Sstevel@tonic-gate 		fprintf(stderr, "acctcon: bad times: old: %s", time_buf);
3360Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &tnew);
3370Sstevel@tonic-gate 		fprintf(stderr, "new: %s", time_buf);
3380Sstevel@tonic-gate 		exitcode = 1;
3390Sstevel@tonic-gate 		tp->ttime = tnew;
3400Sstevel@tonic-gate 		return;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	tp->ttime = tnew;
3430Sstevel@tonic-gate 	switch (wb.ut_type) {
3440Sstevel@tonic-gate 	case USER_PROCESS:
3450Sstevel@tonic-gate 		tp->tlsess++;
3460Sstevel@tonic-gate 		/*
3470Sstevel@tonic-gate 		 * Someone logged in without logging off. Put out record.
3480Sstevel@tonic-gate 		 */
3490Sstevel@tonic-gate 		if (tp->tname[0] != '\0') {
3500Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
3510Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
3520Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
3530Sstevel@tonic-gate 			cb.ct_start = told;
3540Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
3550Sstevel@tonic-gate 			    cb.ct_con) == 0) {
3560Sstevel@tonic-gate 				fprintf(stderr, "acctcon: could not calculate "
3570Sstevel@tonic-gate 				    "prime/non-prime hours\n");
3580Sstevel@tonic-gate 				exit(1);
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 			enter(&cb);
3610Sstevel@tonic-gate 			tp->ttotal += tnew-told;
3620Sstevel@tonic-gate 		} else	/* Someone just logged in */
3630Sstevel@tonic-gate 			tp->tlon++;
3640Sstevel@tonic-gate 		CPYN(tp->tname, wb.ut_name);
3650Sstevel@tonic-gate 		break;
3660Sstevel@tonic-gate 	case DEAD_PROCESS:
3670Sstevel@tonic-gate 		tp->tloff++;
3680Sstevel@tonic-gate 		if (tp->tname[0] != '\0') { /* Someone logged off */
3690Sstevel@tonic-gate 			/* Set up and print ctmp record */
3700Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
3710Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
3720Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
3730Sstevel@tonic-gate 			cb.ct_start = told;
3740Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
3750Sstevel@tonic-gate 			    cb.ct_con) == 0) {
3760Sstevel@tonic-gate 				fprintf(stderr, "acctcon: could not calculate "
3770Sstevel@tonic-gate 				    "prime/non-prime hours\n");
3780Sstevel@tonic-gate 				exit(1);
3790Sstevel@tonic-gate 			}
3800Sstevel@tonic-gate 			enter(&cb);
3810Sstevel@tonic-gate 			tp->ttotal += tnew-told;
3820Sstevel@tonic-gate 			tp->tname[0] = '\0';
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate static void
3880Sstevel@tonic-gate printrep()
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	int i;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	freopen(report, "w", stdout);
3930Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &firstime);
3940Sstevel@tonic-gate 	printf("from %s", time_buf);
3950Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &lastime);
3960Sstevel@tonic-gate 	printf("to   %s", time_buf);
3970Sstevel@tonic-gate 	if (ndates)
3980Sstevel@tonic-gate 		printf("%d\tdate change%c\n", ndates, (ndates > 1 ? 's' :
3990Sstevel@tonic-gate 		    '\0'));
4000Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
4010Sstevel@tonic-gate 		printf("%d\t%.*s\n", sy[i].snum,
4020Sstevel@tonic-gate 		    sizeof (sy[i].sname), sy[i].sname);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  *	print summary of line usage
4080Sstevel@tonic-gate  *	accuracy only guaranteed for wtmpx file started fresh
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate static void
4110Sstevel@tonic-gate printlin()
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	struct tbuf *tp;
4140Sstevel@tonic-gate 	double ttime;
4150Sstevel@tonic-gate 	int tsess, ton, toff;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	freopen(replin, "w", stdout);
4180Sstevel@tonic-gate 	ttime = 0.0;
4190Sstevel@tonic-gate 	tsess = ton = toff = 0;
4200Sstevel@tonic-gate 	timet = MINS(lastime-firstime);
4210Sstevel@tonic-gate 	printf("TOTAL DURATION IS %.0f MINUTES\n", timet);
4220Sstevel@tonic-gate 	printf("LINE         MINUTES  PERCENT  # SESS  # ON  # OFF\n");
423*428Ssl108498 	qsort((char *)tbuf, tsize + 1, sizeof (tbuf[0]),
424*428Ssl108498 	    (int (*)(const void *, const void *))tcmp);
4250Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++) {
4260Sstevel@tonic-gate 		timei = MINS(tp->ttotal);
4270Sstevel@tonic-gate 		ttime += timei;
4280Sstevel@tonic-gate 		tsess += tp->tlsess;
4290Sstevel@tonic-gate 		ton += tp->tlon;
4300Sstevel@tonic-gate 		toff += tp->tloff;
4310Sstevel@tonic-gate 		printf("%-*.*s %-7.0f  %-7.0f  %-6d  %-4d  %-5d\n",
4320Sstevel@tonic-gate 		    OUTPUT_LSZ,
4330Sstevel@tonic-gate 		    OUTPUT_LSZ,
4340Sstevel@tonic-gate 		    tp->tline,
4350Sstevel@tonic-gate 		    timei,
4360Sstevel@tonic-gate 		    (timet > 0.)? 100*timei/timet : 0.,
4370Sstevel@tonic-gate 		    tp->tlsess,
4380Sstevel@tonic-gate 		    tp->tlon,
4390Sstevel@tonic-gate 		    tp->tloff);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	printf("TOTALS       %-7.0f  --       %-6d  %-4d  %-5d\n",
4420Sstevel@tonic-gate 	    ttime, tsess, ton, toff);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static int
4460Sstevel@tonic-gate tcmp(struct tbuf *t1, struct tbuf *t2)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	return (strncmp(t1->tline, t2->tline, LSZ));
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate static int
4520Sstevel@tonic-gate node_compare(const void *node1, const void *node2)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	if (((const struct ctab *)node1)->ct_uid >
4550Sstevel@tonic-gate 	    ((const struct ctab *)node2)->ct_uid)
4560Sstevel@tonic-gate 		return (1);
4570Sstevel@tonic-gate 	else if (((const struct ctab *)node1)->ct_uid <
4580Sstevel@tonic-gate 	    ((const struct ctab *)node2)->ct_uid)
4590Sstevel@tonic-gate 		return (-1);
4600Sstevel@tonic-gate 	else
4610Sstevel@tonic-gate 		return (0);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate static void
4650Sstevel@tonic-gate enter(struct ctmp *c)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	unsigned i;
4680Sstevel@tonic-gate 	int j;
4690Sstevel@tonic-gate 	struct ctab **pt;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if ((pctab = (struct ctab *)malloc(sizeof (struct ctab))) == NULL) {
4720Sstevel@tonic-gate 		fprintf(stderr, "acctcon: malloc fail!\n");
4730Sstevel@tonic-gate 		exit(2);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	pctab->ct_uid = c->ct_uid;
4770Sstevel@tonic-gate 	CPYN(pctab->ct_name, c->ct_name);
4780Sstevel@tonic-gate 	pctab->ct_con[0] = c->ct_con[0];
4790Sstevel@tonic-gate 	pctab->ct_con[1] = c->ct_con[1];
4800Sstevel@tonic-gate 	pctab->ct_sess = 1;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (*(pt = (struct ctab **)tsearch((void *)pctab, (void **)&root,  \
4830Sstevel@tonic-gate 		node_compare)) == NULL) {
4840Sstevel@tonic-gate 		fprintf(stderr, "Not enough space available to build tree\n");
4850Sstevel@tonic-gate 		exit(1);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (*pt != pctab) {
4890Sstevel@tonic-gate 		(*pt)->ct_con[0] += c->ct_con[0];
4900Sstevel@tonic-gate 		(*pt)->ct_con[1] += c->ct_con[1];
4910Sstevel@tonic-gate 		(*pt)->ct_sess++;
4920Sstevel@tonic-gate 		free(pctab);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate static void
4980Sstevel@tonic-gate print_node(const void *node, VISIT order, int level)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	if (order == postorder || order == leaf) {
5010Sstevel@tonic-gate 		tb.ta_uid = (*(struct ctab **)node)->ct_uid;
5020Sstevel@tonic-gate 		CPYN(tb.ta_name, (*(struct ctab **)node)->ct_name);
5030Sstevel@tonic-gate 		tb.ta_con[0] = ((*(struct ctab **)node)->ct_con[0]) / 60.0;
5040Sstevel@tonic-gate 		tb.ta_con[1] = ((*(struct ctab **)node)->ct_con[1]) / 60.0;
5050Sstevel@tonic-gate 		tb.ta_sc = (*(struct ctab **)node)->ct_sess;
5060Sstevel@tonic-gate 		fwrite(&tb, sizeof (tb), 1, stdout);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate static void
5110Sstevel@tonic-gate output()
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	twalk((struct ctab *)root, print_node);
5140Sstevel@tonic-gate }
515