17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*21d7f835Sgm149974 * Common Development and Distribution License (the "License").
6*21d7f835Sgm149974 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
26*21d7f835Sgm149974 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
277c478bd9Sstevel@tonic-gate * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * acctcon [-l file] [-o file] <wtmpx-file
347c478bd9Sstevel@tonic-gate * -l file causes output of line usage summary
357c478bd9Sstevel@tonic-gate * -o file causes first/last/reboots report to be written to file
367c478bd9Sstevel@tonic-gate * reads input (normally /var/adm/wtmpx), produces
377c478bd9Sstevel@tonic-gate * list of sessions, sorted by ending time in tacct.h format
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include "acctdef.h"
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <time.h>
467c478bd9Sstevel@tonic-gate #include <utmpx.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <search.h>
50414388d7Ssl108498 #include <stdlib.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate int a_tsize = A_TSIZE;
537c478bd9Sstevel@tonic-gate int tsize = -1; /* highest index of used slot in tbuf table */
54414388d7Ssl108498 static int csize;
557c478bd9Sstevel@tonic-gate struct utmpx wb; /* record structure read into */
567c478bd9Sstevel@tonic-gate struct ctmp cb; /* record structure written out of */
577c478bd9Sstevel@tonic-gate struct tacct tb;
587c478bd9Sstevel@tonic-gate double timet, timei;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate struct tbuf {
617c478bd9Sstevel@tonic-gate char tline[LSZ]; /* /dev/... */
627c478bd9Sstevel@tonic-gate char tname[NSZ]; /* user name */
637c478bd9Sstevel@tonic-gate time_t ttime; /* start time */
647c478bd9Sstevel@tonic-gate dev_t tdev; /* device */
657c478bd9Sstevel@tonic-gate int tlsess; /* # complete sessions */
667c478bd9Sstevel@tonic-gate int tlon; /* # times on (ut_type of 7) */
677c478bd9Sstevel@tonic-gate int tloff; /* # times off (ut_type != 7) */
687c478bd9Sstevel@tonic-gate long ttotal; /* total time used on this line */
697c478bd9Sstevel@tonic-gate } *tbuf;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate struct ctab {
727c478bd9Sstevel@tonic-gate uid_t ct_uid;
737c478bd9Sstevel@tonic-gate char ct_name[NSZ];
747c478bd9Sstevel@tonic-gate long ct_con[2];
757c478bd9Sstevel@tonic-gate ushort_t ct_sess;
767c478bd9Sstevel@tonic-gate } *pctab;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate int nsys;
797c478bd9Sstevel@tonic-gate struct sys {
807c478bd9Sstevel@tonic-gate char sname[LSZ]; /* reasons for ACCOUNTING records */
817c478bd9Sstevel@tonic-gate char snum; /* number of times encountered */
827c478bd9Sstevel@tonic-gate } sy[NSYS];
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static char time_buf[50];
857c478bd9Sstevel@tonic-gate time_t datetime; /* old time if date changed, otherwise 0 */
867c478bd9Sstevel@tonic-gate time_t firstime;
877c478bd9Sstevel@tonic-gate time_t lastime;
887c478bd9Sstevel@tonic-gate int ndates; /* number of times date changed */
897c478bd9Sstevel@tonic-gate int exitcode;
907c478bd9Sstevel@tonic-gate char *report = NULL;
917c478bd9Sstevel@tonic-gate char *replin = NULL;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate uid_t namtouid();
947c478bd9Sstevel@tonic-gate dev_t lintodev();
957c478bd9Sstevel@tonic-gate static int valid(void);
967c478bd9Sstevel@tonic-gate static void fixup(FILE *);
977c478bd9Sstevel@tonic-gate static void loop(void);
987c478bd9Sstevel@tonic-gate static void bootshut(void);
997c478bd9Sstevel@tonic-gate static int iline(void);
1007c478bd9Sstevel@tonic-gate static void upall(void);
1017c478bd9Sstevel@tonic-gate static void update(struct tbuf *);
1027c478bd9Sstevel@tonic-gate static void printrep(void);
1037c478bd9Sstevel@tonic-gate static void printlin(void);
1047c478bd9Sstevel@tonic-gate static int tcmp(struct tbuf *, struct tbuf *);
1057c478bd9Sstevel@tonic-gate static int node_compare(const void *, const void *);
1067c478bd9Sstevel@tonic-gate static void enter(struct ctmp *);
1077c478bd9Sstevel@tonic-gate static void print_node(const void *, VISIT, int);
1087c478bd9Sstevel@tonic-gate static void output(void);
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate extern char *optarg;
1117c478bd9Sstevel@tonic-gate extern int optind;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate void **root = NULL;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1167c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate int c;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1217c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "l:o:")) != EOF)
1227c478bd9Sstevel@tonic-gate switch (c) {
1237c478bd9Sstevel@tonic-gate case 'l':
1247c478bd9Sstevel@tonic-gate replin = optarg;
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate case 'o':
1277c478bd9Sstevel@tonic-gate report = optarg;
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case '?':
1307c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: %s [-l lineuse] "
1317c478bd9Sstevel@tonic-gate "[-o reboot]\n", argv[0]);
1327c478bd9Sstevel@tonic-gate exit(1);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate if ((tbuf = (struct tbuf *)calloc(a_tsize,
1367c478bd9Sstevel@tonic-gate sizeof (struct tbuf))) == NULL) {
1377c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: Cannot allocate memory\n");
1387c478bd9Sstevel@tonic-gate exit(3);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * XXX - fixme - need a good way of getting the fd that getutxent would
1437c478bd9Sstevel@tonic-gate * use to access wtmpx, so we can convert this read of stdin to use
1447c478bd9Sstevel@tonic-gate * the APIs and remove the dependence on the existence of the file.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate while (fread(&wb, sizeof (wb), 1, stdin) == 1) {
1477c478bd9Sstevel@tonic-gate if (firstime == 0)
1487c478bd9Sstevel@tonic-gate firstime = wb.ut_xtime;
1497c478bd9Sstevel@tonic-gate if (valid())
1507c478bd9Sstevel@tonic-gate loop();
1517c478bd9Sstevel@tonic-gate else
1527c478bd9Sstevel@tonic-gate fixup(stderr);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate wb.ut_name[0] = '\0';
1557c478bd9Sstevel@tonic-gate strcpy(wb.ut_line, "acctcon");
1567c478bd9Sstevel@tonic-gate wb.ut_type = ACCOUNTING;
1577c478bd9Sstevel@tonic-gate wb.ut_xtime = lastime;
1587c478bd9Sstevel@tonic-gate loop();
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate output();
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (report != NULL)
1637c478bd9Sstevel@tonic-gate printrep();
1647c478bd9Sstevel@tonic-gate if (replin != NULL)
1657c478bd9Sstevel@tonic-gate printlin();
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate exit(exitcode);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * valid: check input wtmpx record, return 1 if looks OK
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate static int
valid()1757c478bd9Sstevel@tonic-gate valid()
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate int i, c;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /* XPG say that user names should not start with a "-" */
1807c478bd9Sstevel@tonic-gate if ((c = wb.ut_name[0]) == '-')
1817c478bd9Sstevel@tonic-gate return (0);
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate for (i = 0; i < NSZ; i++) {
1847c478bd9Sstevel@tonic-gate c = wb.ut_name[i];
1857c478bd9Sstevel@tonic-gate if (isalnum(c) || c == '$' || c == ' ' || c == '.' ||
1867c478bd9Sstevel@tonic-gate c == '_' || c == '-')
1877c478bd9Sstevel@tonic-gate continue;
1887c478bd9Sstevel@tonic-gate else if (c == '\0')
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate else
1917c478bd9Sstevel@tonic-gate return (0);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if ((wb.ut_type >= EMPTY) && (wb.ut_type <= UTMAXTYPE))
1957c478bd9Sstevel@tonic-gate return (1);
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate static void
fixup(FILE * stream)2017c478bd9Sstevel@tonic-gate fixup(FILE *stream)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate fprintf(stream, "bad wtmpx: offset %lu.\n", ftell(stdin)-sizeof (wb));
2047c478bd9Sstevel@tonic-gate fprintf(stream, "bad record is: %.*s\t%.*s\t%lu",
2057c478bd9Sstevel@tonic-gate sizeof (wb.ut_line),
2067c478bd9Sstevel@tonic-gate wb.ut_line,
2077c478bd9Sstevel@tonic-gate sizeof (wb.ut_name),
2087c478bd9Sstevel@tonic-gate wb.ut_name,
2097c478bd9Sstevel@tonic-gate wb.ut_xtime);
2107c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2117c478bd9Sstevel@tonic-gate fprintf(stream, "\t%s", time_buf);
2127c478bd9Sstevel@tonic-gate exitcode = 1;
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate static void
loop()2167c478bd9Sstevel@tonic-gate loop()
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate int timediff;
2197c478bd9Sstevel@tonic-gate struct tbuf *tp;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate if (wb.ut_line[0] == '\0') /* It's an init admin process */
2227c478bd9Sstevel@tonic-gate return; /* no connect accounting data here */
2237c478bd9Sstevel@tonic-gate switch (wb.ut_type) {
2247c478bd9Sstevel@tonic-gate case OLD_TIME:
2257c478bd9Sstevel@tonic-gate datetime = wb.ut_xtime;
2267c478bd9Sstevel@tonic-gate return;
2277c478bd9Sstevel@tonic-gate case NEW_TIME:
2287c478bd9Sstevel@tonic-gate if (datetime == 0)
2297c478bd9Sstevel@tonic-gate return;
2307c478bd9Sstevel@tonic-gate timediff = wb.ut_xtime - datetime;
2317c478bd9Sstevel@tonic-gate for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
2327c478bd9Sstevel@tonic-gate tp->ttime += timediff;
2337c478bd9Sstevel@tonic-gate datetime = 0;
2347c478bd9Sstevel@tonic-gate ndates++;
2357c478bd9Sstevel@tonic-gate return;
236*21d7f835Sgm149974 case DOWN_TIME:
237*21d7f835Sgm149974 return;
2387c478bd9Sstevel@tonic-gate case BOOT_TIME:
2397c478bd9Sstevel@tonic-gate upall();
2407c478bd9Sstevel@tonic-gate case ACCOUNTING:
2417c478bd9Sstevel@tonic-gate case RUN_LVL:
2427c478bd9Sstevel@tonic-gate lastime = wb.ut_xtime;
2437c478bd9Sstevel@tonic-gate bootshut();
2447c478bd9Sstevel@tonic-gate return;
2457c478bd9Sstevel@tonic-gate case USER_PROCESS:
2467c478bd9Sstevel@tonic-gate case LOGIN_PROCESS:
2477c478bd9Sstevel@tonic-gate case INIT_PROCESS:
2487c478bd9Sstevel@tonic-gate case DEAD_PROCESS: /* WHCC mod 3/86 */
2497c478bd9Sstevel@tonic-gate update(&tbuf[iline()]);
2507c478bd9Sstevel@tonic-gate return;
2517c478bd9Sstevel@tonic-gate case EMPTY:
2527c478bd9Sstevel@tonic-gate return;
2537c478bd9Sstevel@tonic-gate default:
2547c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2557c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: invalid type %d for %s %s %s",
2567c478bd9Sstevel@tonic-gate wb.ut_type,
2577c478bd9Sstevel@tonic-gate wb.ut_name,
2587c478bd9Sstevel@tonic-gate wb.ut_line,
2597c478bd9Sstevel@tonic-gate time_buf);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate * bootshut: record reboot (or shutdown)
2657c478bd9Sstevel@tonic-gate * bump count, looking up wb.ut_line in sy table
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate static void
bootshut()2687c478bd9Sstevel@tonic-gate bootshut()
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate int i;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
2737c478bd9Sstevel@tonic-gate ;
2747c478bd9Sstevel@tonic-gate if (i >= nsys) {
2757c478bd9Sstevel@tonic-gate if (++nsys > NSYS) {
2767c478bd9Sstevel@tonic-gate fprintf(stderr,
2777c478bd9Sstevel@tonic-gate "acctcon: recompile with larger NSYS\n");
2787c478bd9Sstevel@tonic-gate nsys = NSYS;
2797c478bd9Sstevel@tonic-gate return;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate CPYN(sy[i].sname, wb.ut_line);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate sy[i].snum++;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate * iline: look up/enter current line name in tbuf, return index
2887c478bd9Sstevel@tonic-gate * (used to avoid system dependencies on naming)
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate static int
iline()2917c478bd9Sstevel@tonic-gate iline()
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate int i;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate for (i = 0; i <= tsize; i++)
2967c478bd9Sstevel@tonic-gate if (EQN(wb.ut_line, tbuf[i].tline))
2977c478bd9Sstevel@tonic-gate return (i);
2987c478bd9Sstevel@tonic-gate if (++tsize >= a_tsize) {
2997c478bd9Sstevel@tonic-gate a_tsize = a_tsize + A_TSIZE;
3007c478bd9Sstevel@tonic-gate if ((tbuf = (struct tbuf *)realloc(tbuf, a_tsize *
3017c478bd9Sstevel@tonic-gate sizeof (struct tbuf))) == NULL) {
3027c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: Cannot reallocate memory\n");
3037c478bd9Sstevel@tonic-gate exit(2);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate CPYN(tbuf[tsize].tline, wb.ut_line);
3087c478bd9Sstevel@tonic-gate tbuf[tsize].tdev = lintodev(wb.ut_line);
3097c478bd9Sstevel@tonic-gate return (tsize);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate static void
upall()3137c478bd9Sstevel@tonic-gate upall()
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate struct tbuf *tp;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate wb.ut_type = DEAD_PROCESS; /* fudge a logoff for reboot record. */
3187c478bd9Sstevel@tonic-gate for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
3197c478bd9Sstevel@tonic-gate update(tp);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate * update tbuf with new time, write ctmp record for end of session
3247c478bd9Sstevel@tonic-gate */
3257c478bd9Sstevel@tonic-gate static void
update(struct tbuf * tp)3267c478bd9Sstevel@tonic-gate update(struct tbuf *tp)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate time_t told, /* last time for tbuf record */
3297c478bd9Sstevel@tonic-gate tnew; /* time of this record */
3307c478bd9Sstevel@tonic-gate /* Difference is connect time */
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate told = tp->ttime;
3337c478bd9Sstevel@tonic-gate tnew = wb.ut_xtime;
3347c478bd9Sstevel@tonic-gate if (told > tnew) {
3357c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &told);
3367c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: bad times: old: %s", time_buf);
3377c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &tnew);
3387c478bd9Sstevel@tonic-gate fprintf(stderr, "new: %s", time_buf);
3397c478bd9Sstevel@tonic-gate exitcode = 1;
3407c478bd9Sstevel@tonic-gate tp->ttime = tnew;
3417c478bd9Sstevel@tonic-gate return;
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate tp->ttime = tnew;
3447c478bd9Sstevel@tonic-gate switch (wb.ut_type) {
3457c478bd9Sstevel@tonic-gate case USER_PROCESS:
3467c478bd9Sstevel@tonic-gate tp->tlsess++;
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate * Someone logged in without logging off. Put out record.
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate if (tp->tname[0] != '\0') {
3517c478bd9Sstevel@tonic-gate cb.ct_tty = tp->tdev;
3527c478bd9Sstevel@tonic-gate CPYN(cb.ct_name, tp->tname);
3537c478bd9Sstevel@tonic-gate cb.ct_uid = namtouid(cb.ct_name);
3547c478bd9Sstevel@tonic-gate cb.ct_start = told;
3557c478bd9Sstevel@tonic-gate if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
3567c478bd9Sstevel@tonic-gate cb.ct_con) == 0) {
3577c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: could not calculate "
3587c478bd9Sstevel@tonic-gate "prime/non-prime hours\n");
3597c478bd9Sstevel@tonic-gate exit(1);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate enter(&cb);
3627c478bd9Sstevel@tonic-gate tp->ttotal += tnew-told;
3637c478bd9Sstevel@tonic-gate } else /* Someone just logged in */
3647c478bd9Sstevel@tonic-gate tp->tlon++;
3657c478bd9Sstevel@tonic-gate CPYN(tp->tname, wb.ut_name);
3667c478bd9Sstevel@tonic-gate break;
3677c478bd9Sstevel@tonic-gate case DEAD_PROCESS:
3687c478bd9Sstevel@tonic-gate tp->tloff++;
3697c478bd9Sstevel@tonic-gate if (tp->tname[0] != '\0') { /* Someone logged off */
3707c478bd9Sstevel@tonic-gate /* Set up and print ctmp record */
3717c478bd9Sstevel@tonic-gate cb.ct_tty = tp->tdev;
3727c478bd9Sstevel@tonic-gate CPYN(cb.ct_name, tp->tname);
3737c478bd9Sstevel@tonic-gate cb.ct_uid = namtouid(cb.ct_name);
3747c478bd9Sstevel@tonic-gate cb.ct_start = told;
3757c478bd9Sstevel@tonic-gate if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
3767c478bd9Sstevel@tonic-gate cb.ct_con) == 0) {
3777c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: could not calculate "
3787c478bd9Sstevel@tonic-gate "prime/non-prime hours\n");
3797c478bd9Sstevel@tonic-gate exit(1);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate enter(&cb);
3827c478bd9Sstevel@tonic-gate tp->ttotal += tnew-told;
3837c478bd9Sstevel@tonic-gate tp->tname[0] = '\0';
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate static void
printrep()3897c478bd9Sstevel@tonic-gate printrep()
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate int i;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate freopen(report, "w", stdout);
3947c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &firstime);
3957c478bd9Sstevel@tonic-gate printf("from %s", time_buf);
3967c478bd9Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &lastime);
3977c478bd9Sstevel@tonic-gate printf("to %s", time_buf);
3987c478bd9Sstevel@tonic-gate if (ndates)
3997c478bd9Sstevel@tonic-gate printf("%d\tdate change%c\n", ndates, (ndates > 1 ? 's' :
4007c478bd9Sstevel@tonic-gate '\0'));
4017c478bd9Sstevel@tonic-gate for (i = 0; i < nsys; i++)
4027c478bd9Sstevel@tonic-gate printf("%d\t%.*s\n", sy[i].snum,
4037c478bd9Sstevel@tonic-gate sizeof (sy[i].sname), sy[i].sname);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * print summary of line usage
4097c478bd9Sstevel@tonic-gate * accuracy only guaranteed for wtmpx file started fresh
4107c478bd9Sstevel@tonic-gate */
4117c478bd9Sstevel@tonic-gate static void
printlin()4127c478bd9Sstevel@tonic-gate printlin()
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate struct tbuf *tp;
4157c478bd9Sstevel@tonic-gate double ttime;
4167c478bd9Sstevel@tonic-gate int tsess, ton, toff;
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate freopen(replin, "w", stdout);
4197c478bd9Sstevel@tonic-gate ttime = 0.0;
4207c478bd9Sstevel@tonic-gate tsess = ton = toff = 0;
4217c478bd9Sstevel@tonic-gate timet = MINS(lastime-firstime);
4227c478bd9Sstevel@tonic-gate printf("TOTAL DURATION IS %.0f MINUTES\n", timet);
4237c478bd9Sstevel@tonic-gate printf("LINE MINUTES PERCENT # SESS # ON # OFF\n");
424414388d7Ssl108498 qsort((char *)tbuf, tsize + 1, sizeof (tbuf[0]),
425414388d7Ssl108498 (int (*)(const void *, const void *))tcmp);
4267c478bd9Sstevel@tonic-gate for (tp = tbuf; tp <= &tbuf[tsize]; tp++) {
4277c478bd9Sstevel@tonic-gate timei = MINS(tp->ttotal);
4287c478bd9Sstevel@tonic-gate ttime += timei;
4297c478bd9Sstevel@tonic-gate tsess += tp->tlsess;
4307c478bd9Sstevel@tonic-gate ton += tp->tlon;
4317c478bd9Sstevel@tonic-gate toff += tp->tloff;
4327c478bd9Sstevel@tonic-gate printf("%-*.*s %-7.0f %-7.0f %-6d %-4d %-5d\n",
4337c478bd9Sstevel@tonic-gate OUTPUT_LSZ,
4347c478bd9Sstevel@tonic-gate OUTPUT_LSZ,
4357c478bd9Sstevel@tonic-gate tp->tline,
4367c478bd9Sstevel@tonic-gate timei,
4377c478bd9Sstevel@tonic-gate (timet > 0.)? 100*timei/timet : 0.,
4387c478bd9Sstevel@tonic-gate tp->tlsess,
4397c478bd9Sstevel@tonic-gate tp->tlon,
4407c478bd9Sstevel@tonic-gate tp->tloff);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate printf("TOTALS %-7.0f -- %-6d %-4d %-5d\n",
4437c478bd9Sstevel@tonic-gate ttime, tsess, ton, toff);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate static int
tcmp(struct tbuf * t1,struct tbuf * t2)4477c478bd9Sstevel@tonic-gate tcmp(struct tbuf *t1, struct tbuf *t2)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate return (strncmp(t1->tline, t2->tline, LSZ));
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate static int
node_compare(const void * node1,const void * node2)4537c478bd9Sstevel@tonic-gate node_compare(const void *node1, const void *node2)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate if (((const struct ctab *)node1)->ct_uid >
4567c478bd9Sstevel@tonic-gate ((const struct ctab *)node2)->ct_uid)
4577c478bd9Sstevel@tonic-gate return (1);
4587c478bd9Sstevel@tonic-gate else if (((const struct ctab *)node1)->ct_uid <
4597c478bd9Sstevel@tonic-gate ((const struct ctab *)node2)->ct_uid)
4607c478bd9Sstevel@tonic-gate return (-1);
4617c478bd9Sstevel@tonic-gate else
4627c478bd9Sstevel@tonic-gate return (0);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate static void
enter(struct ctmp * c)4667c478bd9Sstevel@tonic-gate enter(struct ctmp *c)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate unsigned i;
4697c478bd9Sstevel@tonic-gate int j;
4707c478bd9Sstevel@tonic-gate struct ctab **pt;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate if ((pctab = (struct ctab *)malloc(sizeof (struct ctab))) == NULL) {
4737c478bd9Sstevel@tonic-gate fprintf(stderr, "acctcon: malloc fail!\n");
4747c478bd9Sstevel@tonic-gate exit(2);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate pctab->ct_uid = c->ct_uid;
4787c478bd9Sstevel@tonic-gate CPYN(pctab->ct_name, c->ct_name);
4797c478bd9Sstevel@tonic-gate pctab->ct_con[0] = c->ct_con[0];
4807c478bd9Sstevel@tonic-gate pctab->ct_con[1] = c->ct_con[1];
4817c478bd9Sstevel@tonic-gate pctab->ct_sess = 1;
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate if (*(pt = (struct ctab **)tsearch((void *)pctab, (void **)&root, \
4847c478bd9Sstevel@tonic-gate node_compare)) == NULL) {
4857c478bd9Sstevel@tonic-gate fprintf(stderr, "Not enough space available to build tree\n");
4867c478bd9Sstevel@tonic-gate exit(1);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate if (*pt != pctab) {
4907c478bd9Sstevel@tonic-gate (*pt)->ct_con[0] += c->ct_con[0];
4917c478bd9Sstevel@tonic-gate (*pt)->ct_con[1] += c->ct_con[1];
4927c478bd9Sstevel@tonic-gate (*pt)->ct_sess++;
4937c478bd9Sstevel@tonic-gate free(pctab);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate static void
print_node(const void * node,VISIT order,int level)4997c478bd9Sstevel@tonic-gate print_node(const void *node, VISIT order, int level)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate if (order == postorder || order == leaf) {
5027c478bd9Sstevel@tonic-gate tb.ta_uid = (*(struct ctab **)node)->ct_uid;
5037c478bd9Sstevel@tonic-gate CPYN(tb.ta_name, (*(struct ctab **)node)->ct_name);
5047c478bd9Sstevel@tonic-gate tb.ta_con[0] = ((*(struct ctab **)node)->ct_con[0]) / 60.0;
5057c478bd9Sstevel@tonic-gate tb.ta_con[1] = ((*(struct ctab **)node)->ct_con[1]) / 60.0;
5067c478bd9Sstevel@tonic-gate tb.ta_sc = (*(struct ctab **)node)->ct_sess;
5077c478bd9Sstevel@tonic-gate fwrite(&tb, sizeof (tb), 1, stdout);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate static void
output()5127c478bd9Sstevel@tonic-gate output()
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate twalk((struct ctab *)root, print_node);
5157c478bd9Sstevel@tonic-gate }
516