xref: /illumos-gate/usr/src/cmd/last/last.c (revision 299c9e70)
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 /*
23*299c9e70SOlaf Bohlen  * Copyright (c) 2017 Olaf Bohlen
24*299c9e70SOlaf Bohlen  *
250a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
260a1278f2SGary Mills  *
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
337c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
387c478bd9Sstevel@tonic-gate  * The Regents of the University of California
397c478bd9Sstevel@tonic-gate  * All Rights Reserved
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
427c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
437c478bd9Sstevel@tonic-gate  * contributors.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * last
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <stdio.h>
517c478bd9Sstevel@tonic-gate #include <stdlib.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <strings.h>
547c478bd9Sstevel@tonic-gate #include <signal.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <pwd.h>
577c478bd9Sstevel@tonic-gate #include <fcntl.h>
587c478bd9Sstevel@tonic-gate #include <utmpx.h>
597c478bd9Sstevel@tonic-gate #include <locale.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
630a1278f2SGary Mills  * Use the full lengths from utmpx for NMAX, LMAX and HMAX .
647c478bd9Sstevel@tonic-gate  */
650a1278f2SGary Mills #define	NMAX	(sizeof (((struct utmpx *)0)->ut_user))
660a1278f2SGary Mills #define	LMAX	(sizeof (((struct utmpx *)0)->ut_line))
677c478bd9Sstevel@tonic-gate #define	HMAX	(sizeof (((struct utmpx *)0)->ut_host))
680a1278f2SGary Mills 
690a1278f2SGary Mills /* Print minimum field widths. */
700a1278f2SGary Mills #define	LOGIN_WIDTH	8
710a1278f2SGary Mills #define	LINE_WIDTH	12
720a1278f2SGary Mills 
737c478bd9Sstevel@tonic-gate #define	SECDAY	(24*60*60)
747c478bd9Sstevel@tonic-gate #define	CHUNK_SIZE 256
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	lineq(a, b)	(strncmp(a, b, LMAX) == 0)
777c478bd9Sstevel@tonic-gate #define	nameq(a, b)	(strncmp(a, b, NMAX) == 0)
787c478bd9Sstevel@tonic-gate #define	hosteq(a, b)	(strncmp(a, b, HMAX) == 0)
797c478bd9Sstevel@tonic-gate #define	linehostnameq(a, b, c, d) \
807c478bd9Sstevel@tonic-gate 	    (lineq(a, b)&&hosteq(a+LMAX+1, c)&&nameq(a+LMAX+HMAX+2, d))
817c478bd9Sstevel@tonic-gate 
82*299c9e70SOlaf Bohlen #define	USAGE	"usage: last [-n number] [-f filename] [-a ] [ -l ] [name |\
83*299c9e70SOlaf Bohlen  tty] ...\n"
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Beware: These are set in main() to exclude the executable name.  */
867c478bd9Sstevel@tonic-gate static char	**argv;
877c478bd9Sstevel@tonic-gate static int	argc;
887c478bd9Sstevel@tonic-gate static char	**names;
897c478bd9Sstevel@tonic-gate static int	names_num;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static struct	utmpx buf[128];
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * ttnames and logouts are allocated in the blocks of
957c478bd9Sstevel@tonic-gate  * CHUNK_SIZE lines whenever needed. The count of the
967c478bd9Sstevel@tonic-gate  * current size is maintained in the variable "lines"
977c478bd9Sstevel@tonic-gate  * The variable bootxtime is used to hold the time of
987c478bd9Sstevel@tonic-gate  * the last BOOT_TIME
997c478bd9Sstevel@tonic-gate  * All elements of the logouts are initialised to bootxtime
1007c478bd9Sstevel@tonic-gate  * everytime the buffer is reallocated.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static char	**ttnames;
1047c478bd9Sstevel@tonic-gate static time_t	*logouts;
1057c478bd9Sstevel@tonic-gate static time_t	bootxtime;
1067c478bd9Sstevel@tonic-gate static int	lines;
1077c478bd9Sstevel@tonic-gate static char	timef[128];
1087c478bd9Sstevel@tonic-gate static char	hostf[HMAX + 1];
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static char *strspl(char *, char *);
1117c478bd9Sstevel@tonic-gate static void onintr(int);
1127c478bd9Sstevel@tonic-gate static void reallocate_buffer();
1137c478bd9Sstevel@tonic-gate static void memory_alloc(int);
1147c478bd9Sstevel@tonic-gate static int want(struct utmpx *, char **, char **);
1157c478bd9Sstevel@tonic-gate static void record_time(time_t *, int *, int, struct utmpx *);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
main(int ac,char ** av)1187c478bd9Sstevel@tonic-gate main(int ac, char **av)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int i, j;
1217c478bd9Sstevel@tonic-gate 	int aflag = 0;
122*299c9e70SOlaf Bohlen 	int lflag = 0;  /* parameter -l, long format with seconds and years */
1237c478bd9Sstevel@tonic-gate 	int fpos;	/* current position in time format buffer */
1247c478bd9Sstevel@tonic-gate 	int chrcnt;	/* # of chars formatted by current sprintf */
1257c478bd9Sstevel@tonic-gate 	int bl, wtmp;
1267c478bd9Sstevel@tonic-gate 	char *ct;
1277c478bd9Sstevel@tonic-gate 	char *ut_host;
1287c478bd9Sstevel@tonic-gate 	char *ut_user;
1297c478bd9Sstevel@tonic-gate 	struct utmpx *bp;
1307c478bd9Sstevel@tonic-gate 	time_t otime;
1317c478bd9Sstevel@tonic-gate 	struct stat stb;
1327c478bd9Sstevel@tonic-gate 	int print = 0;
1337c478bd9Sstevel@tonic-gate 	char *crmsg = (char *)0;
1347c478bd9Sstevel@tonic-gate 	long outrec = 0;
1357c478bd9Sstevel@tonic-gate 	long maxrec = 0x7fffffffL;
1367c478bd9Sstevel@tonic-gate 	char *wtmpfile = "/var/adm/wtmpx";
1377c478bd9Sstevel@tonic-gate 	size_t hostf_len;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1407c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1417c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't. */
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	(void) time(&buf[0].ut_xtime);
1467c478bd9Sstevel@tonic-gate 	ac--, av++;
1477c478bd9Sstevel@tonic-gate 	argc = ac;
1487c478bd9Sstevel@tonic-gate 	argv = av;
1497c478bd9Sstevel@tonic-gate 	names = malloc(argc * sizeof (char *));
1507c478bd9Sstevel@tonic-gate 	if (names == NULL) {
1517c478bd9Sstevel@tonic-gate 		perror("last");
1527c478bd9Sstevel@tonic-gate 		exit(2);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	names_num = 0;
1557c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
1567c478bd9Sstevel@tonic-gate 		if (argv[i][0] == '-') {
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 			/* -[0-9]*   sets max # records to print */
1597c478bd9Sstevel@tonic-gate 			if (isdigit(argv[i][1])) {
1607c478bd9Sstevel@tonic-gate 				maxrec = atoi(argv[i]+1);
1617c478bd9Sstevel@tonic-gate 				continue;
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 			for (j = 1; argv[i][j] != '\0'; ++j) {
1657c478bd9Sstevel@tonic-gate 				switch (argv[i][j]) {
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 				/* -f name sets filename of wtmp file */
1687c478bd9Sstevel@tonic-gate 				case 'f':
1697c478bd9Sstevel@tonic-gate 					if (argv[i][j+1] != '\0') {
1707c478bd9Sstevel@tonic-gate 						wtmpfile = &argv[i][j+1];
1717c478bd9Sstevel@tonic-gate 					} else if (i+1 < argc) {
1727c478bd9Sstevel@tonic-gate 						wtmpfile = argv[++i];
1737c478bd9Sstevel@tonic-gate 					} else {
1747c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1757c478bd9Sstevel@tonic-gate 						    gettext("last: argument to "
1767c478bd9Sstevel@tonic-gate 						    "-f is missing\n"));
1777c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1787c478bd9Sstevel@tonic-gate 						    gettext(USAGE));
1797c478bd9Sstevel@tonic-gate 						exit(1);
1807c478bd9Sstevel@tonic-gate 					}
1817c478bd9Sstevel@tonic-gate 					goto next_word;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 				/* -n number sets max # records to print */
1847c478bd9Sstevel@tonic-gate 				case 'n': {
1857c478bd9Sstevel@tonic-gate 					char *arg;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 					if (argv[i][j+1] != '\0') {
1887c478bd9Sstevel@tonic-gate 						arg = &argv[i][j+1];
1897c478bd9Sstevel@tonic-gate 					} else if (i+1 < argc) {
1907c478bd9Sstevel@tonic-gate 						arg = argv[++i];
1917c478bd9Sstevel@tonic-gate 					} else {
1927c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1937c478bd9Sstevel@tonic-gate 						    gettext("last: argument to "
1947c478bd9Sstevel@tonic-gate 						    "-n is missing\n"));
1957c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1967c478bd9Sstevel@tonic-gate 						    gettext(USAGE));
1977c478bd9Sstevel@tonic-gate 						exit(1);
1987c478bd9Sstevel@tonic-gate 					}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 					if (!isdigit(*arg)) {
2017c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
2027c478bd9Sstevel@tonic-gate 						    gettext("last: argument to "
2037c478bd9Sstevel@tonic-gate 						    "-n is not a number\n"));
2047c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
2057c478bd9Sstevel@tonic-gate 						    gettext(USAGE));
2067c478bd9Sstevel@tonic-gate 						exit(1);
2077c478bd9Sstevel@tonic-gate 					}
2087c478bd9Sstevel@tonic-gate 					maxrec = atoi(arg);
2097c478bd9Sstevel@tonic-gate 					goto next_word;
2107c478bd9Sstevel@tonic-gate 				}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 				/* -a displays hostname last on the line */
2137c478bd9Sstevel@tonic-gate 				case 'a':
2147c478bd9Sstevel@tonic-gate 					aflag++;
2157c478bd9Sstevel@tonic-gate 					break;
2167c478bd9Sstevel@tonic-gate 
217*299c9e70SOlaf Bohlen 				/* -l turns on long dates and times */
218*299c9e70SOlaf Bohlen 				case 'l':
219*299c9e70SOlaf Bohlen 					lflag++;
220*299c9e70SOlaf Bohlen 					break;
221*299c9e70SOlaf Bohlen 
2227c478bd9Sstevel@tonic-gate 				default:
2237c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(USAGE));
2247c478bd9Sstevel@tonic-gate 					exit(1);
2257c478bd9Sstevel@tonic-gate 				}
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate next_word:
2297c478bd9Sstevel@tonic-gate 			continue;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (strlen(argv[i]) > 2 || strcmp(argv[i], "~") == 0 ||
2337c478bd9Sstevel@tonic-gate 		    getpwnam(argv[i]) != NULL) {
2347c478bd9Sstevel@tonic-gate 			/* Not a tty number. */
2357c478bd9Sstevel@tonic-gate 			names[names_num] = argv[i];
2367c478bd9Sstevel@tonic-gate 			++names_num;
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			/* tty number.  Prepend "tty". */
2397c478bd9Sstevel@tonic-gate 			names[names_num] = strspl("tty", argv[i]);
2407c478bd9Sstevel@tonic-gate 			++names_num;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	wtmp = open(wtmpfile, 0);
2457c478bd9Sstevel@tonic-gate 	if (wtmp < 0) {
2467c478bd9Sstevel@tonic-gate 		perror(wtmpfile);
2477c478bd9Sstevel@tonic-gate 		exit(1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	(void) fstat(wtmp, &stb);
2507c478bd9Sstevel@tonic-gate 	bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf);
2517c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2527c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, onintr);
2537c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, onintr);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	lines = CHUNK_SIZE;
2567c478bd9Sstevel@tonic-gate 	ttnames = calloc(lines, sizeof (char *));
2577c478bd9Sstevel@tonic-gate 	logouts = calloc(lines, sizeof (time_t));
2587c478bd9Sstevel@tonic-gate 	if (ttnames == NULL || logouts == NULL) {
2597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Out of memory \n "));
2607c478bd9Sstevel@tonic-gate 		exit(2);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 		for (bl--; bl >= 0; bl--) {
2637c478bd9Sstevel@tonic-gate 		(void) lseek(wtmp, (off_t)(bl * sizeof (buf)), 0);
2647c478bd9Sstevel@tonic-gate 		bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof (buf[0]) - 1];
2657c478bd9Sstevel@tonic-gate 		for (; bp >= buf; bp--) {
2667c478bd9Sstevel@tonic-gate 			if (want(bp, &ut_host, &ut_user)) {
2677c478bd9Sstevel@tonic-gate 				for (i = 0; i <= lines; i++) {
2687c478bd9Sstevel@tonic-gate 				if (i == lines)
2690a1278f2SGary Mills 					reallocate_buffer();
2707c478bd9Sstevel@tonic-gate 				if (ttnames[i] == NULL) {
2710a1278f2SGary Mills 					memory_alloc(i);
2727c478bd9Sstevel@tonic-gate 					/*
2737c478bd9Sstevel@tonic-gate 					 * LMAX+HMAX+NMAX+3 bytes have been
2747c478bd9Sstevel@tonic-gate 					 * allocated for ttnames[i].
2757c478bd9Sstevel@tonic-gate 					 * If bp->ut_line is longer than LMAX,
2767c478bd9Sstevel@tonic-gate 					 * ut_host is longer than HMAX,
2777c478bd9Sstevel@tonic-gate 					 * and ut_user is longer than NMAX,
2787c478bd9Sstevel@tonic-gate 					 * truncate it to fit ttnames[i].
2797c478bd9Sstevel@tonic-gate 					 */
2807c478bd9Sstevel@tonic-gate 					(void) strlcpy(ttnames[i], bp->ut_line,
2810a1278f2SGary Mills 					    LMAX+1);
2827c478bd9Sstevel@tonic-gate 					(void) strlcpy(ttnames[i]+LMAX+1,
2830a1278f2SGary Mills 					    ut_host, HMAX+1);
2847c478bd9Sstevel@tonic-gate 					(void) strlcpy(ttnames[i]+LMAX+HMAX+2,
2850a1278f2SGary Mills 					    ut_user, NMAX+1);
2867c478bd9Sstevel@tonic-gate 						record_time(&otime, &print,
2870a1278f2SGary Mills 						    i, bp);
2887c478bd9Sstevel@tonic-gate 						break;
2897c478bd9Sstevel@tonic-gate 					} else if (linehostnameq(ttnames[i],
2907c478bd9Sstevel@tonic-gate 					    bp->ut_line, ut_host, ut_user)) {
2917c478bd9Sstevel@tonic-gate 						record_time(&otime,
2927c478bd9Sstevel@tonic-gate 						    &print, i, bp);
2937c478bd9Sstevel@tonic-gate 						break;
2947c478bd9Sstevel@tonic-gate 					}
2957c478bd9Sstevel@tonic-gate 				}
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			if (print) {
2987c478bd9Sstevel@tonic-gate 				if (strncmp(bp->ut_line, "ftp", 3) == 0)
2997c478bd9Sstevel@tonic-gate 					bp->ut_line[3] = '\0';
3007c478bd9Sstevel@tonic-gate 				if (strncmp(bp->ut_line, "uucp", 4) == 0)
3017c478bd9Sstevel@tonic-gate 					bp->ut_line[4] = '\0';
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 				ct = ctime(&bp->ut_xtime);
3047c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-*.*s  %-*.*s "),
3050a1278f2SGary Mills 				    LOGIN_WIDTH, NMAX, bp->ut_name,
3060a1278f2SGary Mills 				    LINE_WIDTH, LMAX, bp->ut_line);
3077c478bd9Sstevel@tonic-gate 				hostf_len = strlen(bp->ut_host);
3087c478bd9Sstevel@tonic-gate 				(void) snprintf(hostf, sizeof (hostf),
3097c478bd9Sstevel@tonic-gate 				    "%-*.*s", hostf_len, hostf_len,
3107c478bd9Sstevel@tonic-gate 				    bp->ut_host);
311*299c9e70SOlaf Bohlen 				/* write seconds and year if -l specified */
312*299c9e70SOlaf Bohlen 				if (lflag > 0) {
313*299c9e70SOlaf Bohlen 					fpos = snprintf(timef, sizeof (timef),
314*299c9e70SOlaf Bohlen 					    "%10.10s %13.13s ",
315*299c9e70SOlaf Bohlen 					    ct, 11 + ct);
316*299c9e70SOlaf Bohlen 				} else {
317*299c9e70SOlaf Bohlen 					fpos = snprintf(timef, sizeof (timef),
318*299c9e70SOlaf Bohlen 					    "%10.10s %5.5s ",
319*299c9e70SOlaf Bohlen 					    ct, 11 + ct);
320*299c9e70SOlaf Bohlen 				}
321*299c9e70SOlaf Bohlen 
3227c478bd9Sstevel@tonic-gate 				if (!lineq(bp->ut_line, "system boot") &&
3237c478bd9Sstevel@tonic-gate 				    !lineq(bp->ut_line, "system down")) {
3247c478bd9Sstevel@tonic-gate 					if (otime == 0 &&
3257c478bd9Sstevel@tonic-gate 					    bp->ut_type == USER_PROCESS) {
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (fpos < sizeof (timef)) {
3287c478bd9Sstevel@tonic-gate 		/* timef still has room */
3297c478bd9Sstevel@tonic-gate 		(void) snprintf(timef + fpos, sizeof (timef) - fpos,
3300a1278f2SGary Mills 		    gettext("  still logged in"));
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 					} else {
3347c478bd9Sstevel@tonic-gate 					time_t delta;
3357c478bd9Sstevel@tonic-gate 					if (otime < 0) {
3367c478bd9Sstevel@tonic-gate 						otime = -otime;
3377c478bd9Sstevel@tonic-gate 						/*
3387c478bd9Sstevel@tonic-gate 						 * TRANSLATION_NOTE
3397c478bd9Sstevel@tonic-gate 						 * See other notes on "down"
3407c478bd9Sstevel@tonic-gate 						 * and "- %5.5s".
3417c478bd9Sstevel@tonic-gate 						 * "-" means "until".  This
3427c478bd9Sstevel@tonic-gate 						 * is displayed after the
3437c478bd9Sstevel@tonic-gate 						 * starting time as in:
3447c478bd9Sstevel@tonic-gate 						 * 	16:20 - down
3457c478bd9Sstevel@tonic-gate 						 * You probably don't want to
3467c478bd9Sstevel@tonic-gate 						 * translate this.  Should you
3477c478bd9Sstevel@tonic-gate 						 * decide to translate this,
3487c478bd9Sstevel@tonic-gate 						 * translate "- %5.5s" too.
3497c478bd9Sstevel@tonic-gate 						 */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (fpos < sizeof (timef)) {
3527c478bd9Sstevel@tonic-gate 		/* timef still has room */
3537c478bd9Sstevel@tonic-gate 		chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
3540a1278f2SGary Mills 		    gettext("- %s"), crmsg);
3557c478bd9Sstevel@tonic-gate 		fpos += chrcnt;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 					} else {
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (fpos < sizeof (timef)) {
3617c478bd9Sstevel@tonic-gate 		/* timef still has room */
362*299c9e70SOlaf Bohlen 		if (lflag > 0) {
363*299c9e70SOlaf Bohlen 			chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
364*299c9e70SOlaf Bohlen 			    gettext("- %8.8s"), ctime(&otime) + 11);
365*299c9e70SOlaf Bohlen 		} else {
366*299c9e70SOlaf Bohlen 			chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
367*299c9e70SOlaf Bohlen 			    gettext("- %5.5s"), ctime(&otime) + 11);
368*299c9e70SOlaf Bohlen 		}
3697c478bd9Sstevel@tonic-gate 		fpos += chrcnt;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 					}
3737c478bd9Sstevel@tonic-gate 					delta = otime - bp->ut_xtime;
3747c478bd9Sstevel@tonic-gate 					if (delta < SECDAY) {
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (fpos < sizeof (timef)) {
3777c478bd9Sstevel@tonic-gate 		/* timef still has room */
378*299c9e70SOlaf Bohlen 		if (lflag > 0) {
379*299c9e70SOlaf Bohlen 			(void) snprintf(timef + fpos, sizeof (timef) - fpos,
380*299c9e70SOlaf Bohlen 			    gettext("  (%8.8s)"), asctime(gmtime(&delta)) + 11);
381*299c9e70SOlaf Bohlen 		} else {
382*299c9e70SOlaf Bohlen 			(void) snprintf(timef + fpos, sizeof (timef) - fpos,
383*299c9e70SOlaf Bohlen 			    gettext("  (%5.5s)"), asctime(gmtime(&delta)) + 11);
384*299c9e70SOlaf Bohlen 		}
385*299c9e70SOlaf Bohlen 
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 					} else {
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (fpos < sizeof (timef)) {
3917c478bd9Sstevel@tonic-gate 		/* timef still has room */
392*299c9e70SOlaf Bohlen 		if (lflag > 0) {
393*299c9e70SOlaf Bohlen 			(void) snprintf(timef + fpos, sizeof (timef) - fpos,
394*299c9e70SOlaf Bohlen 			    gettext(" (%ld+%8.8s)"), delta / SECDAY,
395*299c9e70SOlaf Bohlen 			    asctime(gmtime(&delta)) + 11);
396*299c9e70SOlaf Bohlen 		} else {
397*299c9e70SOlaf Bohlen 			(void) snprintf(timef + fpos, sizeof (timef) - fpos,
398*299c9e70SOlaf Bohlen 			    gettext(" (%ld+%5.5s)"), delta / SECDAY,
399*299c9e70SOlaf Bohlen 			    asctime(gmtime(&delta)) + 11);
400*299c9e70SOlaf Bohlen 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 					}
404*299c9e70SOlaf Bohlen 					}
4057c478bd9Sstevel@tonic-gate 				}
406*299c9e70SOlaf Bohlen 				if (lflag > 0) {
407*299c9e70SOlaf Bohlen 					if (aflag)
408*299c9e70SOlaf Bohlen 						(void) printf("%-.*s %-.*s\n",
409*299c9e70SOlaf Bohlen 						    strlen(timef), timef,
410*299c9e70SOlaf Bohlen 						    strlen(hostf), hostf);
411*299c9e70SOlaf Bohlen 					else
412*299c9e70SOlaf Bohlen 						(void) printf(
413*299c9e70SOlaf Bohlen 						    "%-16.16s %-.*s\n", hostf,
414*299c9e70SOlaf Bohlen 						    strlen(timef), timef);
415*299c9e70SOlaf Bohlen 				} else {
416*299c9e70SOlaf Bohlen 					if (aflag)
417*299c9e70SOlaf Bohlen 						(void) printf(
418*299c9e70SOlaf Bohlen 						    "%-35.35s %-.*s\n", timef,
419*299c9e70SOlaf Bohlen 						    strlen(hostf), hostf);
420*299c9e70SOlaf Bohlen 					else
421*299c9e70SOlaf Bohlen 						(void) printf(
422*299c9e70SOlaf Bohlen 						    "%-16.16s %-.35s\n", hostf,
423*299c9e70SOlaf Bohlen 						    timef);
4247c478bd9Sstevel@tonic-gate 				}
4257c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
4267c478bd9Sstevel@tonic-gate 				if (++outrec >= maxrec)
4277c478bd9Sstevel@tonic-gate 					exit(0);
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 			/*
4307c478bd9Sstevel@tonic-gate 			 * when the system is down or crashed.
4317c478bd9Sstevel@tonic-gate 			 */
4327c478bd9Sstevel@tonic-gate 			if (bp->ut_type == BOOT_TIME) {
4337c478bd9Sstevel@tonic-gate 				for (i = 0; i < lines; i++)
4347c478bd9Sstevel@tonic-gate 					logouts[i] = -bp->ut_xtime;
4357c478bd9Sstevel@tonic-gate 				bootxtime = -bp->ut_xtime;
4367c478bd9Sstevel@tonic-gate 				/*
4377c478bd9Sstevel@tonic-gate 				 * TRANSLATION_NOTE
4387c478bd9Sstevel@tonic-gate 				 * Translation of this "down " will replace
4397c478bd9Sstevel@tonic-gate 				 * the %s in "- %s".  "down" is used instead
4407c478bd9Sstevel@tonic-gate 				 * of the real time session was ended, probably
4417c478bd9Sstevel@tonic-gate 				 * because the session ended by a sudden crash.
4427c478bd9Sstevel@tonic-gate 				 */
4437c478bd9Sstevel@tonic-gate 				crmsg = gettext("down ");
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 			print = 0;	/* reset the print flag */
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	ct = ctime(&buf[0].ut_xtime);
449*299c9e70SOlaf Bohlen 	if (lflag > 0) {
450*299c9e70SOlaf Bohlen 		(void) printf(gettext("\nwtmp begins %10.10s %13.13s \n"), ct,
451*299c9e70SOlaf Bohlen 		    ct + 11);
452*299c9e70SOlaf Bohlen 	} else {
453*299c9e70SOlaf Bohlen 		(void) printf(gettext("\nwtmp begins %10.10s %5.5s \n"), ct,
454*299c9e70SOlaf Bohlen 		    ct + 11);
455*299c9e70SOlaf Bohlen 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* free() called to prevent lint warning about names */
4587c478bd9Sstevel@tonic-gate 	free(names);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate static void
reallocate_buffer()4647c478bd9Sstevel@tonic-gate reallocate_buffer()
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	int j;
4677c478bd9Sstevel@tonic-gate 	static char	**tmpttnames;
4687c478bd9Sstevel@tonic-gate 	static time_t	*tmplogouts;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	lines += CHUNK_SIZE;
4717c478bd9Sstevel@tonic-gate 	tmpttnames = realloc(ttnames, sizeof (char *)*lines);
4727c478bd9Sstevel@tonic-gate 	tmplogouts = realloc(logouts, sizeof (time_t)*lines);
4737c478bd9Sstevel@tonic-gate 	if (tmpttnames == NULL || tmplogouts == NULL) {
4747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Out of memory \n"));
4757c478bd9Sstevel@tonic-gate 		exit(2);
4767c478bd9Sstevel@tonic-gate 	} else {
4770a1278f2SGary Mills 		ttnames = tmpttnames;
4780a1278f2SGary Mills 		logouts = tmplogouts;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	for (j = lines-CHUNK_SIZE; j < lines; j++) {
4817c478bd9Sstevel@tonic-gate 		ttnames[j] = NULL;
4827c478bd9Sstevel@tonic-gate 		logouts[j] = bootxtime;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static void
memory_alloc(int i)4877c478bd9Sstevel@tonic-gate memory_alloc(int i)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	ttnames[i] = (char *)malloc(LMAX + HMAX + NMAX + 3);
4907c478bd9Sstevel@tonic-gate 	if (ttnames[i] == NULL) {
4917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Out of memory \n "));
4927c478bd9Sstevel@tonic-gate 		exit(2);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static void
onintr(int signo)4977c478bd9Sstevel@tonic-gate onintr(int signo)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	char *ct;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (signo == SIGQUIT)
5027c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, (void(*)())onintr);
5037c478bd9Sstevel@tonic-gate 	ct = ctime(&buf[0].ut_xtime);
5047c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11);
5057c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
5067c478bd9Sstevel@tonic-gate 	if (signo == SIGINT)
5077c478bd9Sstevel@tonic-gate 		exit(1);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static int
want(struct utmpx * bp,char ** host,char ** user)5117c478bd9Sstevel@tonic-gate want(struct utmpx *bp, char **host, char **user)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	char **name;
5147c478bd9Sstevel@tonic-gate 	int i;
5157c478bd9Sstevel@tonic-gate 	char *zerostr = "\0";
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	*host = zerostr; *user = zerostr;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		/* if ut_line = dtremote for the users who did dtremote login */
5207c478bd9Sstevel@tonic-gate 	if (strncmp(bp->ut_line, "dtremote", 8) == 0) {
5217c478bd9Sstevel@tonic-gate 		*host = bp->ut_host;
5227c478bd9Sstevel@tonic-gate 		*user = bp->ut_user;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 		/* if ut_line = dtlocal for the users who did a dtlocal login */
5257c478bd9Sstevel@tonic-gate 	else if (strncmp(bp->ut_line, "dtlocal", 7) == 0) {
5267c478bd9Sstevel@tonic-gate 		*host = bp->ut_host;
5277c478bd9Sstevel@tonic-gate 		*user = bp->ut_user;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * Both dtremote and dtlocal can have multiple entries in
5317c478bd9Sstevel@tonic-gate 		 * /var/adm/wtmpx with these values, so the user and host
5327c478bd9Sstevel@tonic-gate 		 * entries are also checked
5337c478bd9Sstevel@tonic-gate 		 */
5347c478bd9Sstevel@tonic-gate 	if ((bp->ut_type == BOOT_TIME) || (bp->ut_type == DOWN_TIME))
5357c478bd9Sstevel@tonic-gate 		(void) strcpy(bp->ut_user, "reboot");
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS &&
5387c478bd9Sstevel@tonic-gate 	    bp->ut_type != BOOT_TIME && bp->ut_type != DOWN_TIME)
5397c478bd9Sstevel@tonic-gate 		return (0);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (bp->ut_user[0] == '.')
5427c478bd9Sstevel@tonic-gate 		return (0);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (names_num == 0) {
5457c478bd9Sstevel@tonic-gate 		if (bp->ut_line[0] != '\0')
5467c478bd9Sstevel@tonic-gate 			return (1);
5477c478bd9Sstevel@tonic-gate 	} else {
5487c478bd9Sstevel@tonic-gate 		name = names;
5497c478bd9Sstevel@tonic-gate 		for (i = 0; i < names_num; i++, name++) {
5507c478bd9Sstevel@tonic-gate 			if (nameq(*name, bp->ut_name) ||
5517c478bd9Sstevel@tonic-gate 			    lineq(*name, bp->ut_line) ||
5527c478bd9Sstevel@tonic-gate 			    (lineq(*name, "ftp") &&
5537c478bd9Sstevel@tonic-gate 			    (strncmp(bp->ut_line, "ftp", 3) == 0))) {
5547c478bd9Sstevel@tonic-gate 				return (1);
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	return (0);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static char *
strspl(char * left,char * right)5627c478bd9Sstevel@tonic-gate strspl(char *left, char *right)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	size_t ressize = strlen(left) + strlen(right) + 1;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	char *res = malloc(ressize);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (res == NULL) {
5697c478bd9Sstevel@tonic-gate 		perror("last");
5707c478bd9Sstevel@tonic-gate 		exit(2);
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	(void) strlcpy(res, left, ressize);
5737c478bd9Sstevel@tonic-gate 	(void) strlcat(res, right, ressize);
5747c478bd9Sstevel@tonic-gate 	return (res);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate static void
record_time(time_t * otime,int * print,int i,struct utmpx * bp)5787c478bd9Sstevel@tonic-gate record_time(time_t *otime, int *print, int i, struct utmpx *bp)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	*otime = logouts[i];
5817c478bd9Sstevel@tonic-gate 	logouts[i] = bp->ut_xtime;
5827c478bd9Sstevel@tonic-gate 	if ((bp->ut_type == USER_PROCESS && bp->ut_user[0] != '\0') ||
5837c478bd9Sstevel@tonic-gate 	    (bp->ut_type == BOOT_TIME) || (bp->ut_type == DOWN_TIME))
5847c478bd9Sstevel@tonic-gate 		*print = 1;
5857c478bd9Sstevel@tonic-gate 	else
5867c478bd9Sstevel@tonic-gate 		*print = 0;
5877c478bd9Sstevel@tonic-gate }
588