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