xref: /illumos-gate/usr/src/cmd/acct/acctcon1.c (revision 55fea89d)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27414388d7Ssl  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
30e544b3c5SToomas Soome 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	acctcon1 [-p] [-t] [-l file] [-o file] <wtmpx-file >ctmp-file
337c478bd9Sstevel@tonic-gate  *	-p	print input only, no processing
347c478bd9Sstevel@tonic-gate  *	-t	test mode: use latest time found in input, rather than
357c478bd9Sstevel@tonic-gate  *		current time when computing times of lines still on
367c478bd9Sstevel@tonic-gate  *		(only way to get repeatable data from old files)
377c478bd9Sstevel@tonic-gate  *	-l file	causes output of line usage summary
387c478bd9Sstevel@tonic-gate  *	-o file	causes first/last/reboots report to be written to file
397c478bd9Sstevel@tonic-gate  *	reads input (normally /var/adm/wtmpx), produces
407c478bd9Sstevel@tonic-gate  *	list of sessions, sorted by ending time in ctmp.h/ascii format
417c478bd9Sstevel@tonic-gate  *	A_TSIZE is max # distinct ttys
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include "acctdef.h"
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <time.h>
497c478bd9Sstevel@tonic-gate #include <utmpx.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
51414388d7Ssl #include <stdlib.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int	a_tsize	= A_TSIZE;
547c478bd9Sstevel@tonic-gate int	tsize	= -1;	/* used slots in tbuf table */
557c478bd9Sstevel@tonic-gate struct  utmpx	wb;	/* record structure read into */
567c478bd9Sstevel@tonic-gate struct	ctmp	cb;	/* record structure written out of */
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 #define DATE_FMT	"%a %b %e %H:%M:%S %Y\n"
707c478bd9Sstevel@tonic-gate int	nsys;
717c478bd9Sstevel@tonic-gate struct sys {
727c478bd9Sstevel@tonic-gate 	char	sname[LSZ];	/* reasons for ACCOUNTING records */
737c478bd9Sstevel@tonic-gate 	char	snum;		/* number of times encountered */
747c478bd9Sstevel@tonic-gate } sy[NSYS];
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate time_t	datetime;	/* old time if date changed, otherwise 0 */
777c478bd9Sstevel@tonic-gate time_t	firstime;
787c478bd9Sstevel@tonic-gate time_t	lastime;
797c478bd9Sstevel@tonic-gate int	ndates;		/* number of times date changed */
807c478bd9Sstevel@tonic-gate int	exitcode;
817c478bd9Sstevel@tonic-gate char	*report	= NULL;
827c478bd9Sstevel@tonic-gate char	*replin = NULL;
837c478bd9Sstevel@tonic-gate int	printonly;
847c478bd9Sstevel@tonic-gate int	tflag;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static char time_buf[50];
877c478bd9Sstevel@tonic-gate uid_t	namtouid();
887c478bd9Sstevel@tonic-gate dev_t	lintodev();
897c478bd9Sstevel@tonic-gate static size_t wread(void);
907c478bd9Sstevel@tonic-gate static int valid(void);
917c478bd9Sstevel@tonic-gate static void fixup(FILE *);
927c478bd9Sstevel@tonic-gate static void loop(void);
937c478bd9Sstevel@tonic-gate static void bootshut(void);
947c478bd9Sstevel@tonic-gate static int iline(void);
957c478bd9Sstevel@tonic-gate static void upall(void);
967c478bd9Sstevel@tonic-gate static void update(struct tbuf *);
977c478bd9Sstevel@tonic-gate static void printrep(void);
987c478bd9Sstevel@tonic-gate static void printlin(void);
997c478bd9Sstevel@tonic-gate static void prctmp(struct ctmp *);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)102*55fea89dSDan Cross main(int argc, char **argv)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	char *prog = argv[0];
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
1077c478bd9Sstevel@tonic-gate 	while (--argc > 0 && **++argv == '-')
1087c478bd9Sstevel@tonic-gate 		switch(*++*argv) {
1097c478bd9Sstevel@tonic-gate 		case 'l':
1107c478bd9Sstevel@tonic-gate 			if (--argc > 0)
1117c478bd9Sstevel@tonic-gate 				replin = *++argv;
1127c478bd9Sstevel@tonic-gate 			continue;
1137c478bd9Sstevel@tonic-gate 		case 'o':
1147c478bd9Sstevel@tonic-gate 			if (--argc > 0)
1157c478bd9Sstevel@tonic-gate 				report = *++argv;
1167c478bd9Sstevel@tonic-gate 			continue;
1177c478bd9Sstevel@tonic-gate 		case 'p':
1187c478bd9Sstevel@tonic-gate 			printonly++;
1197c478bd9Sstevel@tonic-gate 			continue;
1207c478bd9Sstevel@tonic-gate 		case 't':
1217c478bd9Sstevel@tonic-gate 			tflag++;
1227c478bd9Sstevel@tonic-gate 			continue;
1237c478bd9Sstevel@tonic-gate 		default:
1247c478bd9Sstevel@tonic-gate 			fprintf(stderr, "usage: %s [-p] [-t] [-l lineuse] [-o reboot]\n", prog);
1257c478bd9Sstevel@tonic-gate 			exit(1);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((tbuf = (struct tbuf *) calloc(a_tsize,
1307c478bd9Sstevel@tonic-gate 		sizeof (struct tbuf))) == NULL) {
1317c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: Cannot allocate memory\n");
1327c478bd9Sstevel@tonic-gate 		exit(3);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (printonly) {
1367c478bd9Sstevel@tonic-gate 		while (wread()) {
1377c478bd9Sstevel@tonic-gate 			if (valid()) {
1387c478bd9Sstevel@tonic-gate 				printf("%.*s\t%.*s\t%lu",
1397c478bd9Sstevel@tonic-gate 				    sizeof (wb.ut_line),
1407c478bd9Sstevel@tonic-gate 				    wb.ut_line,
1417c478bd9Sstevel@tonic-gate 				    sizeof (wb.ut_name),
1427c478bd9Sstevel@tonic-gate 				    wb.ut_name,
1437c478bd9Sstevel@tonic-gate 				    wb.ut_xtime);
1447c478bd9Sstevel@tonic-gate 				cftime(time_buf, DATE_FMT, &wb.ut_xtime);
1457c478bd9Sstevel@tonic-gate 				printf("\t%s", time_buf);
1467c478bd9Sstevel@tonic-gate 			} else
1477c478bd9Sstevel@tonic-gate 				fixup(stdout);
148*55fea89dSDan Cross 
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		exit(exitcode);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	while (wread()) {
1547c478bd9Sstevel@tonic-gate 		if (firstime == 0)
1557c478bd9Sstevel@tonic-gate 			firstime = wb.ut_xtime;
1567c478bd9Sstevel@tonic-gate 		if (valid())
1577c478bd9Sstevel@tonic-gate 			loop();
1587c478bd9Sstevel@tonic-gate 		else
1597c478bd9Sstevel@tonic-gate 			fixup(stderr);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	wb.ut_name[0] = '\0';
1627c478bd9Sstevel@tonic-gate 	strcpy(wb.ut_line, "acctcon1");
1637c478bd9Sstevel@tonic-gate 	wb.ut_type = ACCOUNTING;
1647c478bd9Sstevel@tonic-gate 	if (tflag)
1657c478bd9Sstevel@tonic-gate 		wb.ut_xtime = lastime;
1667c478bd9Sstevel@tonic-gate 	else
1677c478bd9Sstevel@tonic-gate 		time(&wb.ut_xtime);
1687c478bd9Sstevel@tonic-gate 	loop();
1697c478bd9Sstevel@tonic-gate 	if (report != NULL)
1707c478bd9Sstevel@tonic-gate 		printrep();
1717c478bd9Sstevel@tonic-gate 	if (replin != NULL)
1727c478bd9Sstevel@tonic-gate 		printlin();
1737c478bd9Sstevel@tonic-gate 	exit(exitcode);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static size_t
wread()1777c478bd9Sstevel@tonic-gate wread()
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	return (fread(&wb, sizeof(wb), 1, stdin) == 1);
180*55fea89dSDan Cross 
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * valid: check input wtmp record, return 1 if looks OK
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate static int
valid()1877c478bd9Sstevel@tonic-gate valid()
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int i, c;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* XPG say that user names should not start with a "-". */
1927c478bd9Sstevel@tonic-gate         if ((c = wb.ut_name[0]) == '-')
1937c478bd9Sstevel@tonic-gate 		return(0);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
1967c478bd9Sstevel@tonic-gate 		c = wb.ut_name[i];
1977c478bd9Sstevel@tonic-gate 		if (isalnum(c) || c == '$' || c == ' ' || c == '_' || c == '-')
1987c478bd9Sstevel@tonic-gate 			continue;
1997c478bd9Sstevel@tonic-gate 		else if (c == '\0')
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 		else
2027c478bd9Sstevel@tonic-gate 			return(0);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if((wb.ut_type >= EMPTY) && (wb.ut_type <= UTMAXTYPE))
2067c478bd9Sstevel@tonic-gate 		return(1);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return(0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  *	fixup assumes that V6 wtmp (16 bytes long) is mixed in with
2137c478bd9Sstevel@tonic-gate  *	V7 records (20 bytes each)
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  *	Starting with Release 5.0 of UNIX, this routine will no
2167c478bd9Sstevel@tonic-gate  *	longer reset the read pointer.  This has a snowball effect
2177c478bd9Sstevel@tonic-gate  *	On the following records until the offset corrects itself.
2187c478bd9Sstevel@tonic-gate  *	If a message is printed from here, it should be regarded as
2197c478bd9Sstevel@tonic-gate  *	a bad record and not as a V6 record.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static void
fixup(FILE * stream)2227c478bd9Sstevel@tonic-gate fixup(FILE *stream)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad wtmpx: offset %lu.\n", ftell(stdin)-sizeof(wb));
2257c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad record is:  %.*s\t%.*s\t%lu",
2267c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_line),
2277c478bd9Sstevel@tonic-gate 	    wb.ut_line,
2287c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_name),
2297c478bd9Sstevel@tonic-gate 	    wb.ut_name,
2307c478bd9Sstevel@tonic-gate 	    wb.ut_xtime);
2317c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2327c478bd9Sstevel@tonic-gate 	fprintf(stream, "\t%s", time_buf);
2337c478bd9Sstevel@tonic-gate #ifdef	V6
2347c478bd9Sstevel@tonic-gate 	fseek(stdin, (long)-4, 1);
2357c478bd9Sstevel@tonic-gate #endif
2367c478bd9Sstevel@tonic-gate 	exitcode = 1;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static void
loop()2407c478bd9Sstevel@tonic-gate loop()
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int timediff;
2437c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if(wb.ut_line[0] == '\0' )	/* It's an init admin process */
2467c478bd9Sstevel@tonic-gate 		return;			/* no connect accounting data here */
2477c478bd9Sstevel@tonic-gate 	switch(wb.ut_type) {
2487c478bd9Sstevel@tonic-gate 	case OLD_TIME:
2497c478bd9Sstevel@tonic-gate 		datetime = wb.ut_xtime;
2507c478bd9Sstevel@tonic-gate 		return;
2517c478bd9Sstevel@tonic-gate 	case NEW_TIME:
2527c478bd9Sstevel@tonic-gate 		if(datetime == 0)
2537c478bd9Sstevel@tonic-gate 			return;
2547c478bd9Sstevel@tonic-gate 		timediff = wb.ut_xtime - datetime;
2557c478bd9Sstevel@tonic-gate 		for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
2567c478bd9Sstevel@tonic-gate 			tp->ttime += timediff;
2577c478bd9Sstevel@tonic-gate 		datetime = 0;
2587c478bd9Sstevel@tonic-gate 		ndates++;
2597c478bd9Sstevel@tonic-gate 		return;
2607c478bd9Sstevel@tonic-gate 	case BOOT_TIME:
2617c478bd9Sstevel@tonic-gate 		upall();
262e544b3c5SToomas Soome 		/* FALLTHROUGH */
2637c478bd9Sstevel@tonic-gate 	case ACCOUNTING:
2647c478bd9Sstevel@tonic-gate 	case RUN_LVL:
2657c478bd9Sstevel@tonic-gate 		lastime = wb.ut_xtime;
2667c478bd9Sstevel@tonic-gate 		bootshut();
2677c478bd9Sstevel@tonic-gate 		return;
2687c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
2697c478bd9Sstevel@tonic-gate 	case LOGIN_PROCESS:
2707c478bd9Sstevel@tonic-gate 	case INIT_PROCESS:
2717c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:
2727c478bd9Sstevel@tonic-gate 		update(&tbuf[iline()]);
2737c478bd9Sstevel@tonic-gate 		return;
2747c478bd9Sstevel@tonic-gate 	case EMPTY:
2757c478bd9Sstevel@tonic-gate 		return;
2767c478bd9Sstevel@tonic-gate 	default:
2777c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &wb.ut_xtime);
2787c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: invalid type %d for %s %s %s",
2797c478bd9Sstevel@tonic-gate 		    wb.ut_type,
2807c478bd9Sstevel@tonic-gate 		    wb.ut_name,
2817c478bd9Sstevel@tonic-gate 		    wb.ut_line,
2827c478bd9Sstevel@tonic-gate 		    time_buf);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * bootshut: record reboot (or shutdown)
2887c478bd9Sstevel@tonic-gate  * bump count, looking up wb.ut_line in sy table
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate static void
bootshut()2917c478bd9Sstevel@tonic-gate bootshut()
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	int i;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
2967c478bd9Sstevel@tonic-gate 		;
2977c478bd9Sstevel@tonic-gate 	if (i >= nsys) {
2987c478bd9Sstevel@tonic-gate 		if (++nsys > NSYS) {
2997c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3007c478bd9Sstevel@tonic-gate 				"acctcon1: recompile with larger NSYS\n");
3017c478bd9Sstevel@tonic-gate 			nsys = NSYS;
3027c478bd9Sstevel@tonic-gate 			return;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		CPYN(sy[i].sname, wb.ut_line);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	sy[i].snum++;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * iline: look up/enter current line name in tbuf, return index
3117c478bd9Sstevel@tonic-gate  * (used to avoid system dependencies on naming)
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate static int
iline()3147c478bd9Sstevel@tonic-gate iline()
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int i;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	for (i = 0; i <= tsize; i++)
3197c478bd9Sstevel@tonic-gate 		if (EQN(wb.ut_line, tbuf[i].tline))
3207c478bd9Sstevel@tonic-gate 			return(i);
3217c478bd9Sstevel@tonic-gate 	if (++tsize >= a_tsize) {
3227c478bd9Sstevel@tonic-gate 		a_tsize = a_tsize + A_TSIZE;
3237c478bd9Sstevel@tonic-gate 		if ((tbuf = (struct tbuf *) realloc(tbuf, a_tsize *
3247c478bd9Sstevel@tonic-gate 			sizeof (struct tbuf))) == NULL) {
3257c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctcon1: Cannot reallocate memory\n");
3267c478bd9Sstevel@tonic-gate 			exit(2);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	CPYN(tbuf[tsize].tline, wb.ut_line);
3317c478bd9Sstevel@tonic-gate 	tbuf[tsize].tdev = lintodev(wb.ut_line);
3327c478bd9Sstevel@tonic-gate 	return(tsize);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static void
upall()3367c478bd9Sstevel@tonic-gate upall()
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	wb.ut_type = INIT_PROCESS;	/* fudge a logoff for reboot record */
3417c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
3427c478bd9Sstevel@tonic-gate 		update(tp);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * update tbuf with new time, write ctmp record for end of session
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate static void
update(struct tbuf * tp)3497c478bd9Sstevel@tonic-gate update(struct tbuf *tp)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	time_t	told,	/* last time for tbuf record */
3527c478bd9Sstevel@tonic-gate 		tnew;	/* time of this record */
3537c478bd9Sstevel@tonic-gate 			/* Difference is connect time */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	told = tp->ttime;
3567c478bd9Sstevel@tonic-gate 	tnew = wb.ut_xtime;
3577c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &told);
3587c478bd9Sstevel@tonic-gate 	fprintf(stderr, "The old time is: %s", time_buf);
3597c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &tnew);
3607c478bd9Sstevel@tonic-gate 	fprintf(stderr, "the new time is: %s", time_buf);
3617c478bd9Sstevel@tonic-gate 	if (told > tnew) {
3627c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &told);
3637c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon1: bad times: old: %s", time_buf);
3647c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &tnew);
3657c478bd9Sstevel@tonic-gate 		fprintf(stderr, "new: %s", time_buf);
3667c478bd9Sstevel@tonic-gate 		exitcode = 1;
3677c478bd9Sstevel@tonic-gate 		tp->ttime = tnew;
3687c478bd9Sstevel@tonic-gate 		return;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	tp->ttime = tnew;
3717c478bd9Sstevel@tonic-gate 	switch(wb.ut_type) {
3727c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
3737c478bd9Sstevel@tonic-gate 		tp->tlsess++;
3747c478bd9Sstevel@tonic-gate 		if(tp->tname[0] != '\0') { /* Someone logged in without */
3757c478bd9Sstevel@tonic-gate 					   /* logging off. Put out record. */
3767c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
3777c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
3787c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
3797c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
3807c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
3817c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
3827c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon1: could not calculate prime/non-prime hours\n");
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 				exit(1);
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 			prctmp(&cb);
3877c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		else	/* Someone just logged in */
3907c478bd9Sstevel@tonic-gate 			tp->tlon++;
3917c478bd9Sstevel@tonic-gate 		CPYN(tp->tname, wb.ut_name);
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 	case INIT_PROCESS:
3947c478bd9Sstevel@tonic-gate 	case LOGIN_PROCESS:
3957c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:
3967c478bd9Sstevel@tonic-gate 		tp->tloff++;
3977c478bd9Sstevel@tonic-gate 		if(tp->tname[0] != '\0') { /* Someone logged off */
3987c478bd9Sstevel@tonic-gate 			/* Set up and print ctmp record */
3997c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
4007c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
4017c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
4027c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
4037c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
4047c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
4057c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon1: could not calculate prime/non-prime hours\n");
4067c478bd9Sstevel@tonic-gate 				exit(1);
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			prctmp(&cb);
4097c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
4107c478bd9Sstevel@tonic-gate 			tp->tname[0] = '\0';
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate static void
printrep()4167c478bd9Sstevel@tonic-gate printrep()
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	int i;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	freopen(report, "w", stdout);
4217c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &firstime);
4227c478bd9Sstevel@tonic-gate 	printf("from %s", time_buf);
4237c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &lastime);
4247c478bd9Sstevel@tonic-gate 	printf("to   %s", time_buf);
4257c478bd9Sstevel@tonic-gate 	if (ndates)
4267c478bd9Sstevel@tonic-gate 		printf("%d\tdate change%c\n",ndates,(ndates>1 ? 's' : '\0'));
4277c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
4287c478bd9Sstevel@tonic-gate 		printf("%d\t%.*s\n", sy[i].snum,
4297c478bd9Sstevel@tonic-gate 		    sizeof (sy[i].sname), sy[i].sname);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  *	print summary of line usage
4347c478bd9Sstevel@tonic-gate  *	accuracy only guaranteed for wtmpx file started fresh
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static void
printlin()4377c478bd9Sstevel@tonic-gate printlin()
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
4407c478bd9Sstevel@tonic-gate 	double timet, timei;
4417c478bd9Sstevel@tonic-gate 	double ttime;
4427c478bd9Sstevel@tonic-gate 	int tsess, ton, toff;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	freopen(replin, "w", stdout);
4457c478bd9Sstevel@tonic-gate 	ttime = 0.0;
4467c478bd9Sstevel@tonic-gate 	tsess = ton = toff = 0;
4477c478bd9Sstevel@tonic-gate 	timet = MINS(lastime-firstime);
4487c478bd9Sstevel@tonic-gate 	printf("TOTAL DURATION IS %.0f MINUTES\n", timet);
4497c478bd9Sstevel@tonic-gate 	printf("LINE         MINUTES  PERCENT  # SESS  # ON  # OFF\n");
4507c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++) {
4517c478bd9Sstevel@tonic-gate 		timei = MINS(tp->ttotal);
4527c478bd9Sstevel@tonic-gate 		ttime += timei;
4537c478bd9Sstevel@tonic-gate 		tsess += tp->tlsess;
4547c478bd9Sstevel@tonic-gate 		ton += tp->tlon;
4557c478bd9Sstevel@tonic-gate 		toff += tp->tloff;
4567c478bd9Sstevel@tonic-gate 		printf("%-*.*s %-7.0f  %-7.0f  %-6d  %-4d  %-5d\n",
4577c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
4587c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
4597c478bd9Sstevel@tonic-gate 		    tp->tline,
4607c478bd9Sstevel@tonic-gate 		    timei,
4617c478bd9Sstevel@tonic-gate 		    (timet > 0.)? 100*timei/timet : 0.,
4627c478bd9Sstevel@tonic-gate 		    tp->tlsess,
4637c478bd9Sstevel@tonic-gate 		    tp->tlon,
4647c478bd9Sstevel@tonic-gate 		    tp->tloff);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	printf("TOTALS       %-7.0f  --       %-6d  %-4d  %-5d\n",
4677c478bd9Sstevel@tonic-gate 	    ttime, tsess, ton, toff);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate static void
prctmp(struct ctmp * t)4717c478bd9Sstevel@tonic-gate prctmp(struct ctmp *t)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	printf("%u\t%ld\t%.*s\t%lu\t%lu\t%lu",
4757c478bd9Sstevel@tonic-gate 	    t->ct_tty,
4767c478bd9Sstevel@tonic-gate 	    t->ct_uid,
4777c478bd9Sstevel@tonic-gate 	    OUTPUT_NSZ,
4787c478bd9Sstevel@tonic-gate 	    t->ct_name,
4797c478bd9Sstevel@tonic-gate 	    t->ct_con[0],
4807c478bd9Sstevel@tonic-gate 	    t->ct_con[1],
4817c478bd9Sstevel@tonic-gate 	    t->ct_start);
4827c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &t->ct_start);
4837c478bd9Sstevel@tonic-gate 	printf("\t%s", time_buf);
4847c478bd9Sstevel@tonic-gate }
485