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*0a1278f2SGary Mills * Copyright (c) 2013 Gary Mills 24*0a1278f2SGary Mills * 257c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate * Use is subject to license terms. 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 317c478bd9Sstevel@tonic-gate * All Rights Reserved 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 367c478bd9Sstevel@tonic-gate * The Regents of the University of California 377c478bd9Sstevel@tonic-gate * All Rights Reserved 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 407c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 417c478bd9Sstevel@tonic-gate * contributors. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * last 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate #include <sys/types.h> 487c478bd9Sstevel@tonic-gate #include <stdio.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate #include <strings.h> 527c478bd9Sstevel@tonic-gate #include <signal.h> 537c478bd9Sstevel@tonic-gate #include <sys/stat.h> 547c478bd9Sstevel@tonic-gate #include <pwd.h> 557c478bd9Sstevel@tonic-gate #include <fcntl.h> 567c478bd9Sstevel@tonic-gate #include <utmpx.h> 577c478bd9Sstevel@tonic-gate #include <locale.h> 587c478bd9Sstevel@tonic-gate #include <ctype.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 61*0a1278f2SGary Mills * Use the full lengths from utmpx for NMAX, LMAX and HMAX . 627c478bd9Sstevel@tonic-gate */ 63*0a1278f2SGary Mills #define NMAX (sizeof (((struct utmpx *)0)->ut_user)) 64*0a1278f2SGary Mills #define LMAX (sizeof (((struct utmpx *)0)->ut_line)) 657c478bd9Sstevel@tonic-gate #define HMAX (sizeof (((struct utmpx *)0)->ut_host)) 66*0a1278f2SGary Mills 67*0a1278f2SGary Mills /* Print minimum field widths. */ 68*0a1278f2SGary Mills #define LOGIN_WIDTH 8 69*0a1278f2SGary Mills #define LINE_WIDTH 12 70*0a1278f2SGary Mills 717c478bd9Sstevel@tonic-gate #define SECDAY (24*60*60) 727c478bd9Sstevel@tonic-gate #define CHUNK_SIZE 256 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define lineq(a, b) (strncmp(a, b, LMAX) == 0) 757c478bd9Sstevel@tonic-gate #define nameq(a, b) (strncmp(a, b, NMAX) == 0) 767c478bd9Sstevel@tonic-gate #define hosteq(a, b) (strncmp(a, b, HMAX) == 0) 777c478bd9Sstevel@tonic-gate #define linehostnameq(a, b, c, d) \ 787c478bd9Sstevel@tonic-gate (lineq(a, b)&&hosteq(a+LMAX+1, c)&&nameq(a+LMAX+HMAX+2, d)) 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate #define USAGE "usage: last [-n number] [-f filename] [-a ] [name | tty] ...\n" 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* Beware: These are set in main() to exclude the executable name. */ 837c478bd9Sstevel@tonic-gate static char **argv; 847c478bd9Sstevel@tonic-gate static int argc; 857c478bd9Sstevel@tonic-gate static char **names; 867c478bd9Sstevel@tonic-gate static int names_num; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate static struct utmpx buf[128]; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * ttnames and logouts are allocated in the blocks of 927c478bd9Sstevel@tonic-gate * CHUNK_SIZE lines whenever needed. The count of the 937c478bd9Sstevel@tonic-gate * current size is maintained in the variable "lines" 947c478bd9Sstevel@tonic-gate * The variable bootxtime is used to hold the time of 957c478bd9Sstevel@tonic-gate * the last BOOT_TIME 967c478bd9Sstevel@tonic-gate * All elements of the logouts are initialised to bootxtime 977c478bd9Sstevel@tonic-gate * everytime the buffer is reallocated. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate static char **ttnames; 1017c478bd9Sstevel@tonic-gate static time_t *logouts; 1027c478bd9Sstevel@tonic-gate static time_t bootxtime; 1037c478bd9Sstevel@tonic-gate static int lines; 1047c478bd9Sstevel@tonic-gate static char timef[128]; 1057c478bd9Sstevel@tonic-gate static char hostf[HMAX + 1]; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static char *strspl(char *, char *); 1087c478bd9Sstevel@tonic-gate static void onintr(int); 1097c478bd9Sstevel@tonic-gate static void reallocate_buffer(); 1107c478bd9Sstevel@tonic-gate static void memory_alloc(int); 1117c478bd9Sstevel@tonic-gate static int want(struct utmpx *, char **, char **); 1127c478bd9Sstevel@tonic-gate static void record_time(time_t *, int *, int, struct utmpx *); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate int 1157c478bd9Sstevel@tonic-gate main(int ac, char **av) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate int i, j; 1187c478bd9Sstevel@tonic-gate int aflag = 0; 1197c478bd9Sstevel@tonic-gate int fpos; /* current position in time format buffer */ 1207c478bd9Sstevel@tonic-gate int chrcnt; /* # of chars formatted by current sprintf */ 1217c478bd9Sstevel@tonic-gate int bl, wtmp; 1227c478bd9Sstevel@tonic-gate char *ct; 1237c478bd9Sstevel@tonic-gate char *ut_host; 1247c478bd9Sstevel@tonic-gate char *ut_user; 1257c478bd9Sstevel@tonic-gate struct utmpx *bp; 1267c478bd9Sstevel@tonic-gate time_t otime; 1277c478bd9Sstevel@tonic-gate struct stat stb; 1287c478bd9Sstevel@tonic-gate int print = 0; 1297c478bd9Sstevel@tonic-gate char *crmsg = (char *)0; 1307c478bd9Sstevel@tonic-gate long outrec = 0; 1317c478bd9Sstevel@tonic-gate long maxrec = 0x7fffffffL; 1327c478bd9Sstevel@tonic-gate char *wtmpfile = "/var/adm/wtmpx"; 1337c478bd9Sstevel@tonic-gate size_t hostf_len; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1367c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1377c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't. */ 1387c478bd9Sstevel@tonic-gate #endif 1397c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate (void) time(&buf[0].ut_xtime); 1427c478bd9Sstevel@tonic-gate ac--, av++; 1437c478bd9Sstevel@tonic-gate argc = ac; 1447c478bd9Sstevel@tonic-gate argv = av; 1457c478bd9Sstevel@tonic-gate names = malloc(argc * sizeof (char *)); 1467c478bd9Sstevel@tonic-gate if (names == NULL) { 1477c478bd9Sstevel@tonic-gate perror("last"); 1487c478bd9Sstevel@tonic-gate exit(2); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate names_num = 0; 1517c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) { 1527c478bd9Sstevel@tonic-gate if (argv[i][0] == '-') { 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* -[0-9]* sets max # records to print */ 1557c478bd9Sstevel@tonic-gate if (isdigit(argv[i][1])) { 1567c478bd9Sstevel@tonic-gate maxrec = atoi(argv[i]+1); 1577c478bd9Sstevel@tonic-gate continue; 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate for (j = 1; argv[i][j] != '\0'; ++j) { 1617c478bd9Sstevel@tonic-gate switch (argv[i][j]) { 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* -f name sets filename of wtmp file */ 1647c478bd9Sstevel@tonic-gate case 'f': 1657c478bd9Sstevel@tonic-gate if (argv[i][j+1] != '\0') { 1667c478bd9Sstevel@tonic-gate wtmpfile = &argv[i][j+1]; 1677c478bd9Sstevel@tonic-gate } else if (i+1 < argc) { 1687c478bd9Sstevel@tonic-gate wtmpfile = argv[++i]; 1697c478bd9Sstevel@tonic-gate } else { 1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1717c478bd9Sstevel@tonic-gate gettext("last: argument to " 1727c478bd9Sstevel@tonic-gate "-f is missing\n")); 1737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1747c478bd9Sstevel@tonic-gate gettext(USAGE)); 1757c478bd9Sstevel@tonic-gate exit(1); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate goto next_word; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* -n number sets max # records to print */ 1807c478bd9Sstevel@tonic-gate case 'n': { 1817c478bd9Sstevel@tonic-gate char *arg; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (argv[i][j+1] != '\0') { 1847c478bd9Sstevel@tonic-gate arg = &argv[i][j+1]; 1857c478bd9Sstevel@tonic-gate } else if (i+1 < argc) { 1867c478bd9Sstevel@tonic-gate arg = argv[++i]; 1877c478bd9Sstevel@tonic-gate } else { 1887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1897c478bd9Sstevel@tonic-gate gettext("last: argument to " 1907c478bd9Sstevel@tonic-gate "-n is missing\n")); 1917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1927c478bd9Sstevel@tonic-gate gettext(USAGE)); 1937c478bd9Sstevel@tonic-gate exit(1); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (!isdigit(*arg)) { 1977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1987c478bd9Sstevel@tonic-gate gettext("last: argument to " 1997c478bd9Sstevel@tonic-gate "-n is not a number\n")); 2007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2017c478bd9Sstevel@tonic-gate gettext(USAGE)); 2027c478bd9Sstevel@tonic-gate exit(1); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate maxrec = atoi(arg); 2057c478bd9Sstevel@tonic-gate goto next_word; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* -a displays hostname last on the line */ 2097c478bd9Sstevel@tonic-gate case 'a': 2107c478bd9Sstevel@tonic-gate aflag++; 2117c478bd9Sstevel@tonic-gate break; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate default: 2147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(USAGE)); 2157c478bd9Sstevel@tonic-gate exit(1); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate next_word: 2207c478bd9Sstevel@tonic-gate continue; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (strlen(argv[i]) > 2 || strcmp(argv[i], "~") == 0 || 2247c478bd9Sstevel@tonic-gate getpwnam(argv[i]) != NULL) { 2257c478bd9Sstevel@tonic-gate /* Not a tty number. */ 2267c478bd9Sstevel@tonic-gate names[names_num] = argv[i]; 2277c478bd9Sstevel@tonic-gate ++names_num; 2287c478bd9Sstevel@tonic-gate } else { 2297c478bd9Sstevel@tonic-gate /* tty number. Prepend "tty". */ 2307c478bd9Sstevel@tonic-gate names[names_num] = strspl("tty", argv[i]); 2317c478bd9Sstevel@tonic-gate ++names_num; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate wtmp = open(wtmpfile, 0); 2367c478bd9Sstevel@tonic-gate if (wtmp < 0) { 2377c478bd9Sstevel@tonic-gate perror(wtmpfile); 2387c478bd9Sstevel@tonic-gate exit(1); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate (void) fstat(wtmp, &stb); 2417c478bd9Sstevel@tonic-gate bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf); 2427c478bd9Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2437c478bd9Sstevel@tonic-gate (void) signal(SIGINT, onintr); 2447c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, onintr); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate lines = CHUNK_SIZE; 2477c478bd9Sstevel@tonic-gate ttnames = calloc(lines, sizeof (char *)); 2487c478bd9Sstevel@tonic-gate logouts = calloc(lines, sizeof (time_t)); 2497c478bd9Sstevel@tonic-gate if (ttnames == NULL || logouts == NULL) { 2507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Out of memory \n ")); 2517c478bd9Sstevel@tonic-gate exit(2); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate for (bl--; bl >= 0; bl--) { 2547c478bd9Sstevel@tonic-gate (void) lseek(wtmp, (off_t)(bl * sizeof (buf)), 0); 2557c478bd9Sstevel@tonic-gate bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof (buf[0]) - 1]; 2567c478bd9Sstevel@tonic-gate for (; bp >= buf; bp--) { 2577c478bd9Sstevel@tonic-gate if (want(bp, &ut_host, &ut_user)) { 2587c478bd9Sstevel@tonic-gate for (i = 0; i <= lines; i++) { 2597c478bd9Sstevel@tonic-gate if (i == lines) 2607c478bd9Sstevel@tonic-gate reallocate_buffer(); 2617c478bd9Sstevel@tonic-gate if (ttnames[i] == NULL) { 2627c478bd9Sstevel@tonic-gate memory_alloc(i); 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * LMAX+HMAX+NMAX+3 bytes have been 2657c478bd9Sstevel@tonic-gate * allocated for ttnames[i]. 2667c478bd9Sstevel@tonic-gate * If bp->ut_line is longer than LMAX, 2677c478bd9Sstevel@tonic-gate * ut_host is longer than HMAX, 2687c478bd9Sstevel@tonic-gate * and ut_user is longer than NMAX, 2697c478bd9Sstevel@tonic-gate * truncate it to fit ttnames[i]. 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate (void) strlcpy(ttnames[i], bp->ut_line, 2727c478bd9Sstevel@tonic-gate LMAX+1); 2737c478bd9Sstevel@tonic-gate (void) strlcpy(ttnames[i]+LMAX+1, 2747c478bd9Sstevel@tonic-gate ut_host, HMAX+1); 2757c478bd9Sstevel@tonic-gate (void) strlcpy(ttnames[i]+LMAX+HMAX+2, 2767c478bd9Sstevel@tonic-gate ut_user, NMAX+1); 2777c478bd9Sstevel@tonic-gate record_time(&otime, &print, 2787c478bd9Sstevel@tonic-gate i, bp); 2797c478bd9Sstevel@tonic-gate break; 2807c478bd9Sstevel@tonic-gate } else if (linehostnameq(ttnames[i], 2817c478bd9Sstevel@tonic-gate bp->ut_line, ut_host, ut_user)) { 2827c478bd9Sstevel@tonic-gate record_time(&otime, 2837c478bd9Sstevel@tonic-gate &print, i, bp); 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate if (print) { 2897c478bd9Sstevel@tonic-gate if (strncmp(bp->ut_line, "ftp", 3) == 0) 2907c478bd9Sstevel@tonic-gate bp->ut_line[3] = '\0'; 2917c478bd9Sstevel@tonic-gate if (strncmp(bp->ut_line, "uucp", 4) == 0) 2927c478bd9Sstevel@tonic-gate bp->ut_line[4] = '\0'; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate ct = ctime(&bp->ut_xtime); 2957c478bd9Sstevel@tonic-gate (void) printf(gettext("%-*.*s %-*.*s "), 296*0a1278f2SGary Mills LOGIN_WIDTH, NMAX, bp->ut_name, 297*0a1278f2SGary Mills LINE_WIDTH, LMAX, bp->ut_line); 2987c478bd9Sstevel@tonic-gate hostf_len = strlen(bp->ut_host); 2997c478bd9Sstevel@tonic-gate (void) snprintf(hostf, sizeof (hostf), 3007c478bd9Sstevel@tonic-gate "%-*.*s", hostf_len, hostf_len, 3017c478bd9Sstevel@tonic-gate bp->ut_host); 3027c478bd9Sstevel@tonic-gate fpos = snprintf(timef, sizeof (timef), 3037c478bd9Sstevel@tonic-gate "%10.10s %5.5s ", 3047c478bd9Sstevel@tonic-gate ct, 11 + ct); 3057c478bd9Sstevel@tonic-gate if (!lineq(bp->ut_line, "system boot") && 3067c478bd9Sstevel@tonic-gate !lineq(bp->ut_line, "system down")) { 3077c478bd9Sstevel@tonic-gate if (otime == 0 && 3087c478bd9Sstevel@tonic-gate bp->ut_type == USER_PROCESS) { 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate if (fpos < sizeof (timef)) { 3117c478bd9Sstevel@tonic-gate /* timef still has room */ 3127c478bd9Sstevel@tonic-gate (void) snprintf(timef + fpos, sizeof (timef) - fpos, 3137c478bd9Sstevel@tonic-gate gettext(" still logged in")); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate } else { 3177c478bd9Sstevel@tonic-gate time_t delta; 3187c478bd9Sstevel@tonic-gate if (otime < 0) { 3197c478bd9Sstevel@tonic-gate otime = -otime; 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 3227c478bd9Sstevel@tonic-gate * See other notes on "down" 3237c478bd9Sstevel@tonic-gate * and "- %5.5s". 3247c478bd9Sstevel@tonic-gate * "-" means "until". This 3257c478bd9Sstevel@tonic-gate * is displayed after the 3267c478bd9Sstevel@tonic-gate * starting time as in: 3277c478bd9Sstevel@tonic-gate * 16:20 - down 3287c478bd9Sstevel@tonic-gate * You probably don't want to 3297c478bd9Sstevel@tonic-gate * translate this. Should you 3307c478bd9Sstevel@tonic-gate * decide to translate this, 3317c478bd9Sstevel@tonic-gate * translate "- %5.5s" too. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if (fpos < sizeof (timef)) { 3357c478bd9Sstevel@tonic-gate /* timef still has room */ 3367c478bd9Sstevel@tonic-gate chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos, 3377c478bd9Sstevel@tonic-gate gettext("- %s"), crmsg); 3387c478bd9Sstevel@tonic-gate fpos += chrcnt; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate } else { 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (fpos < sizeof (timef)) { 3447c478bd9Sstevel@tonic-gate /* timef still has room */ 3457c478bd9Sstevel@tonic-gate chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos, 3467c478bd9Sstevel@tonic-gate gettext("- %5.5s"), ctime(&otime) + 11); 3477c478bd9Sstevel@tonic-gate fpos += chrcnt; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate delta = otime - bp->ut_xtime; 3527c478bd9Sstevel@tonic-gate if (delta < SECDAY) { 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (fpos < sizeof (timef)) { 3557c478bd9Sstevel@tonic-gate /* timef still has room */ 3567c478bd9Sstevel@tonic-gate (void) snprintf(timef + fpos, sizeof (timef) - fpos, 3577c478bd9Sstevel@tonic-gate gettext(" (%5.5s)"), asctime(gmtime(&delta)) + 11); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate } else { 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if (fpos < sizeof (timef)) { 3637c478bd9Sstevel@tonic-gate /* timef still has room */ 3647c478bd9Sstevel@tonic-gate (void) snprintf(timef + fpos, sizeof (timef) - fpos, 3657c478bd9Sstevel@tonic-gate gettext(" (%ld+%5.5s)"), delta / SECDAY, 3667c478bd9Sstevel@tonic-gate asctime(gmtime(&delta)) + 11); 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate if (aflag) 3737c478bd9Sstevel@tonic-gate (void) printf("%-35.35s %-.*s\n", 3747c478bd9Sstevel@tonic-gate timef, strlen(hostf), hostf); 3757c478bd9Sstevel@tonic-gate else 3767c478bd9Sstevel@tonic-gate (void) printf("%-16.16s %-.35s\n", 3777c478bd9Sstevel@tonic-gate hostf, timef); 3787c478bd9Sstevel@tonic-gate (void) fflush(stdout); 3797c478bd9Sstevel@tonic-gate if (++outrec >= maxrec) 3807c478bd9Sstevel@tonic-gate exit(0); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate /* 3837c478bd9Sstevel@tonic-gate * when the system is down or crashed. 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate if (bp->ut_type == BOOT_TIME) { 3867c478bd9Sstevel@tonic-gate for (i = 0; i < lines; i++) 3877c478bd9Sstevel@tonic-gate logouts[i] = -bp->ut_xtime; 3887c478bd9Sstevel@tonic-gate bootxtime = -bp->ut_xtime; 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 3917c478bd9Sstevel@tonic-gate * Translation of this "down " will replace 3927c478bd9Sstevel@tonic-gate * the %s in "- %s". "down" is used instead 3937c478bd9Sstevel@tonic-gate * of the real time session was ended, probably 3947c478bd9Sstevel@tonic-gate * because the session ended by a sudden crash. 3957c478bd9Sstevel@tonic-gate */ 3967c478bd9Sstevel@tonic-gate crmsg = gettext("down "); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate print = 0; /* reset the print flag */ 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate ct = ctime(&buf[0].ut_xtime); 4027c478bd9Sstevel@tonic-gate (void) printf(gettext("\nwtmp begins %10.10s %5.5s \n"), ct, ct + 11); 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* free() called to prevent lint warning about names */ 4057c478bd9Sstevel@tonic-gate free(names); 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate return (0); 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate static void 4117c478bd9Sstevel@tonic-gate reallocate_buffer() 4127c478bd9Sstevel@tonic-gate { 4137c478bd9Sstevel@tonic-gate int j; 4147c478bd9Sstevel@tonic-gate static char **tmpttnames; 4157c478bd9Sstevel@tonic-gate static time_t *tmplogouts; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate lines += CHUNK_SIZE; 4187c478bd9Sstevel@tonic-gate tmpttnames = realloc(ttnames, sizeof (char *)*lines); 4197c478bd9Sstevel@tonic-gate tmplogouts = realloc(logouts, sizeof (time_t)*lines); 4207c478bd9Sstevel@tonic-gate if (tmpttnames == NULL || tmplogouts == NULL) { 4217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Out of memory \n")); 4227c478bd9Sstevel@tonic-gate exit(2); 4237c478bd9Sstevel@tonic-gate } else { 4247c478bd9Sstevel@tonic-gate ttnames = tmpttnames; 4257c478bd9Sstevel@tonic-gate logouts = tmplogouts; 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate for (j = lines-CHUNK_SIZE; j < lines; j++) { 4287c478bd9Sstevel@tonic-gate ttnames[j] = NULL; 4297c478bd9Sstevel@tonic-gate logouts[j] = bootxtime; 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate static void 4347c478bd9Sstevel@tonic-gate memory_alloc(int i) 4357c478bd9Sstevel@tonic-gate { 4367c478bd9Sstevel@tonic-gate ttnames[i] = (char *)malloc(LMAX + HMAX + NMAX + 3); 4377c478bd9Sstevel@tonic-gate if (ttnames[i] == NULL) { 4387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Out of memory \n ")); 4397c478bd9Sstevel@tonic-gate exit(2); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate static void 4447c478bd9Sstevel@tonic-gate onintr(int signo) 4457c478bd9Sstevel@tonic-gate { 4467c478bd9Sstevel@tonic-gate char *ct; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if (signo == SIGQUIT) 4497c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, (void(*)())onintr); 4507c478bd9Sstevel@tonic-gate ct = ctime(&buf[0].ut_xtime); 4517c478bd9Sstevel@tonic-gate (void) printf(gettext("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11); 4527c478bd9Sstevel@tonic-gate (void) fflush(stdout); 4537c478bd9Sstevel@tonic-gate if (signo == SIGINT) 4547c478bd9Sstevel@tonic-gate exit(1); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate static int 4587c478bd9Sstevel@tonic-gate want(struct utmpx *bp, char **host, char **user) 4597c478bd9Sstevel@tonic-gate { 4607c478bd9Sstevel@tonic-gate char **name; 4617c478bd9Sstevel@tonic-gate int i; 4627c478bd9Sstevel@tonic-gate char *zerostr = "\0"; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate *host = zerostr; *user = zerostr; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* if ut_line = dtremote for the users who did dtremote login */ 4677c478bd9Sstevel@tonic-gate if (strncmp(bp->ut_line, "dtremote", 8) == 0) { 4687c478bd9Sstevel@tonic-gate *host = bp->ut_host; 4697c478bd9Sstevel@tonic-gate *user = bp->ut_user; 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate /* if ut_line = dtlocal for the users who did a dtlocal login */ 4727c478bd9Sstevel@tonic-gate else if (strncmp(bp->ut_line, "dtlocal", 7) == 0) { 4737c478bd9Sstevel@tonic-gate *host = bp->ut_host; 4747c478bd9Sstevel@tonic-gate *user = bp->ut_user; 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * Both dtremote and dtlocal can have multiple entries in 4787c478bd9Sstevel@tonic-gate * /var/adm/wtmpx with these values, so the user and host 4797c478bd9Sstevel@tonic-gate * entries are also checked 4807c478bd9Sstevel@tonic-gate */ 4817c478bd9Sstevel@tonic-gate if ((bp->ut_type == BOOT_TIME) || (bp->ut_type == DOWN_TIME)) 4827c478bd9Sstevel@tonic-gate (void) strcpy(bp->ut_user, "reboot"); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS && 4857c478bd9Sstevel@tonic-gate bp->ut_type != BOOT_TIME && bp->ut_type != DOWN_TIME) 4867c478bd9Sstevel@tonic-gate return (0); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate if (bp->ut_user[0] == '.') 4897c478bd9Sstevel@tonic-gate return (0); 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate if (names_num == 0) { 4927c478bd9Sstevel@tonic-gate if (bp->ut_line[0] != '\0') 4937c478bd9Sstevel@tonic-gate return (1); 4947c478bd9Sstevel@tonic-gate } else { 4957c478bd9Sstevel@tonic-gate name = names; 4967c478bd9Sstevel@tonic-gate for (i = 0; i < names_num; i++, name++) { 4977c478bd9Sstevel@tonic-gate if (nameq(*name, bp->ut_name) || 4987c478bd9Sstevel@tonic-gate lineq(*name, bp->ut_line) || 4997c478bd9Sstevel@tonic-gate (lineq(*name, "ftp") && 5007c478bd9Sstevel@tonic-gate (strncmp(bp->ut_line, "ftp", 3) == 0))) { 5017c478bd9Sstevel@tonic-gate return (1); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate return (0); 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate static char * 5097c478bd9Sstevel@tonic-gate strspl(char *left, char *right) 5107c478bd9Sstevel@tonic-gate { 5117c478bd9Sstevel@tonic-gate size_t ressize = strlen(left) + strlen(right) + 1; 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate char *res = malloc(ressize); 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate if (res == NULL) { 5167c478bd9Sstevel@tonic-gate perror("last"); 5177c478bd9Sstevel@tonic-gate exit(2); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate (void) strlcpy(res, left, ressize); 5207c478bd9Sstevel@tonic-gate (void) strlcat(res, right, ressize); 5217c478bd9Sstevel@tonic-gate return (res); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate static void 5257c478bd9Sstevel@tonic-gate record_time(time_t *otime, int *print, int i, struct utmpx *bp) 5267c478bd9Sstevel@tonic-gate { 5277c478bd9Sstevel@tonic-gate *otime = logouts[i]; 5287c478bd9Sstevel@tonic-gate logouts[i] = bp->ut_xtime; 5297c478bd9Sstevel@tonic-gate if ((bp->ut_type == USER_PROCESS && bp->ut_user[0] != '\0') || 5307c478bd9Sstevel@tonic-gate (bp->ut_type == BOOT_TIME) || (bp->ut_type == DOWN_TIME)) 5317c478bd9Sstevel@tonic-gate *print = 1; 5327c478bd9Sstevel@tonic-gate else 5337c478bd9Sstevel@tonic-gate *print = 0; 5347c478bd9Sstevel@tonic-gate } 535