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