Home | History | Annotate | Line # | Download | only in lastlogin
lastlogin.c revision 1.18
      1  1.18       kim /*	$NetBSD: lastlogin.c,v 1.18 2020/05/06 19:31:32 kim Exp $	*/
      2   1.1      phil /*
      3   1.1      phil  * Copyright (c) 1996 John M. Vinopal
      4   1.1      phil  * All rights reserved.
      5   1.1      phil  *
      6   1.1      phil  * Redistribution and use in source and binary forms, with or without
      7   1.1      phil  * modification, are permitted provided that the following conditions
      8   1.1      phil  * are met:
      9   1.1      phil  * 1. Redistributions of source code must retain the above copyright
     10   1.1      phil  *    notice, this list of conditions and the following disclaimer.
     11   1.1      phil  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      phil  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      phil  *    documentation and/or other materials provided with the distribution.
     14   1.1      phil  * 3. All advertising materials mentioning features or use of this software
     15   1.1      phil  *    must display the following acknowledgement:
     16   1.1      phil  *	This product includes software developed for the NetBSD Project
     17   1.1      phil  *	by John M. Vinopal.
     18   1.1      phil  * 4. The name of the author may not be used to endorse or promote products
     19   1.1      phil  *    derived from this software without specific prior written permission.
     20   1.1      phil  *
     21   1.1      phil  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1      phil  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1      phil  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1      phil  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1      phil  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26   1.1      phil  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27   1.1      phil  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28   1.1      phil  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29   1.1      phil  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1      phil  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1      phil  * SUCH DAMAGE.
     32   1.1      phil  */
     33   1.1      phil 
     34   1.3     lukem #include <sys/cdefs.h>
     35   1.3     lukem #ifndef lint
     36  1.18       kim __RCSID("$NetBSD: lastlogin.c,v 1.18 2020/05/06 19:31:32 kim Exp $");
     37   1.3     lukem #endif
     38   1.3     lukem 
     39   1.1      phil #include <sys/types.h>
     40   1.3     lukem #include <err.h>
     41   1.9  christos #include <db.h>
     42   1.1      phil #include <errno.h>
     43   1.1      phil #include <pwd.h>
     44   1.1      phil #include <stdio.h>
     45   1.5      matt #include <stdlib.h>
     46   1.7     elric #include <string.h>
     47   1.9  christos #include <fcntl.h>
     48   1.3     lukem #include <time.h>
     49   1.9  christos #include <arpa/inet.h>
     50   1.9  christos #include <sys/socket.h>
     51   1.9  christos #ifdef SUPPORT_UTMP
     52   1.3     lukem #include <utmp.h>
     53   1.9  christos #endif
     54   1.9  christos #ifdef SUPPORT_UTMPX
     55   1.9  christos #include <utmpx.h>
     56   1.9  christos #endif
     57   1.4     perry #include <unistd.h>
     58  1.11  christos #include <util.h>
     59   1.1      phil 
     60  1.16       kim #ifndef UT_NAMESIZE
     61  1.16       kim # define UT_NAMESIZE	8
     62  1.16       kim #endif
     63  1.16       kim #ifndef UT_LINESIZE
     64  1.16       kim # define UT_LINESIZE	8
     65  1.16       kim #endif
     66  1.16       kim #ifndef UT_HOSTSIZE
     67  1.16       kim # define UT_HOSTSIZE	16
     68  1.16       kim #endif
     69  1.16       kim 
     70  1.16       kim #ifndef UTX_USERSIZE
     71  1.16       kim # define UTX_USERSIZE	64
     72  1.16       kim #endif
     73  1.16       kim #ifndef UTX_LINESIZE
     74  1.16       kim # define UTX_LINESIZE	64
     75  1.16       kim #endif
     76  1.16       kim #ifndef UTX_HOSTSIZE
     77  1.16       kim # define UTX_HOSTSIZE	256
     78  1.16       kim #endif
     79  1.16       kim 
     80  1.18       kim /*
     81  1.18       kim  * Fields in the structure below are 1 byte longer than the maximum possible
     82  1.18       kim  * for NUL-termination.
     83  1.18       kim  */
     84   1.7     elric struct output {
     85   1.9  christos 	struct timeval	 o_tv;
     86  1.16       kim 	char		 o_name[UTX_USERSIZE+1];
     87  1.16       kim 	char		 o_line[UTX_LINESIZE+1];
     88  1.16       kim 	char		 o_host[UTX_HOSTSIZE+1];
     89   1.7     elric 	struct output	*next;
     90   1.7     elric };
     91   1.7     elric 
     92   1.7     elric #define SORT_NONE	0x0000
     93   1.7     elric #define SORT_REVERSE	0x0001
     94   1.7     elric #define SORT_TIME	0x0002
     95   1.7     elric #define DOSORT(x)	((x) & (SORT_TIME))
     96   1.7     elric static	int sortlog = SORT_NONE;
     97   1.7     elric static	struct output *outstack = NULL;
     98   1.7     elric 
     99  1.16       kim static int fixed = 0;
    100  1.16       kim #define FIXED_NAMELEN	UT_NAMESIZE
    101  1.16       kim #define FIXED_LINELEN	UT_LINESIZE
    102  1.16       kim /*
    103  1.16       kim  * This makes the "fixed" output fit in 79 columns.
    104  1.16       kim  * Using UT_HOSTSIZE (16) seems too conservative.
    105  1.16       kim  */
    106  1.16       kim #define FIXED_HOSTLEN	32
    107  1.16       kim 
    108   1.9  christos static int numeric = 0;
    109  1.16       kim static size_t namelen = 0;
    110  1.16       kim static size_t linelen = 0;
    111  1.16       kim static size_t hostlen = 0;
    112  1.16       kim #define SIZECOLUMNS	(!(namelen && linelen && hostlen))
    113   1.9  christos 
    114  1.16       kim static	int		comparelog(const void *, const void *);
    115  1.16       kim static	void		output_record(struct output *);
    116   1.9  christos #ifdef SUPPORT_UTMP
    117  1.16       kim static	void		process_entry(struct passwd *, struct lastlog *);
    118  1.16       kim static	void		dolastlog(const char *, int, char *[]);
    119   1.9  christos #endif
    120   1.9  christos #ifdef SUPPORT_UTMPX
    121  1.16       kim static	void		process_entryx(struct passwd *, struct lastlogx *);
    122  1.16       kim static	void		dolastlogx(const char *, int, char *[]);
    123   1.9  christos #endif
    124  1.16       kim static	void		push_record(struct output *);
    125  1.16       kim static	void		sizecolumns(struct output *);
    126  1.16       kim static	void		output_stack(struct output *);
    127  1.16       kim static	void		sort_and_output_stack(struct output *);
    128  1.15     joerg __dead static	void	usage(void);
    129   1.1      phil 
    130   1.1      phil int
    131  1.15     joerg main(int argc, char *argv[])
    132   1.1      phil {
    133  1.14     lukem 	const char *logfile =
    134  1.14     lukem #if defined(SUPPORT_UTMPX)
    135  1.14     lukem 	    _PATH_LASTLOGX;
    136  1.14     lukem #elif defined(SUPPORT_UTMP)
    137  1.14     lukem 	    _PATH_LASTLOG;
    138  1.14     lukem #else
    139  1.14     lukem 	#error "either SUPPORT_UTMP or SUPPORT_UTMPX must be defined"
    140  1.14     lukem #endif
    141   1.9  christos 	int	ch;
    142   1.9  christos 	size_t	len;
    143   1.1      phil 
    144  1.16       kim 	while ((ch = getopt(argc, argv, "f:FH:L:nN:rt")) != -1) {
    145   1.7     elric 		switch (ch) {
    146   1.9  christos 		case 'H':
    147   1.9  christos 			hostlen = atoi(optarg);
    148   1.9  christos 			break;
    149   1.9  christos 		case 'f':
    150   1.9  christos 			logfile = optarg;
    151   1.9  christos 			break;
    152  1.16       kim 		case 'F':
    153  1.16       kim 			fixed++;
    154  1.16       kim 			break;
    155   1.9  christos 		case 'L':
    156   1.9  christos 			linelen = atoi(optarg);
    157   1.9  christos 			break;
    158   1.9  christos 		case 'n':
    159   1.9  christos 			numeric++;
    160   1.9  christos 			break;
    161   1.9  christos 		case 'N':
    162   1.9  christos 			namelen = atoi(optarg);
    163   1.9  christos 			break;
    164   1.7     elric 		case 'r':
    165   1.7     elric 			sortlog |= SORT_REVERSE;
    166   1.7     elric 			break;
    167   1.7     elric 		case 't':
    168   1.7     elric 			sortlog |= SORT_TIME;
    169   1.7     elric 			break;
    170   1.7     elric 		default:
    171   1.7     elric 			usage();
    172   1.7     elric 		}
    173   1.1      phil 	}
    174   1.7     elric 	argc -= optind;
    175   1.7     elric 	argv += optind;
    176   1.1      phil 
    177  1.16       kim 	if (fixed) {
    178  1.16       kim 		if (!namelen)
    179  1.16       kim 			namelen = FIXED_NAMELEN;
    180  1.16       kim 		if (!linelen)
    181  1.16       kim 			linelen = FIXED_LINELEN;
    182  1.16       kim 		if (!hostlen)
    183  1.16       kim 			hostlen = FIXED_HOSTLEN;
    184  1.16       kim 	}
    185  1.16       kim 
    186   1.9  christos 	len = strlen(logfile);
    187   1.9  christos 
    188   1.9  christos 	setpassent(1);	/* Keep passwd file pointers open */
    189   1.9  christos 
    190   1.9  christos #if defined(SUPPORT_UTMPX)
    191   1.9  christos 	if (len > 0 && logfile[len - 1] == 'x')
    192   1.9  christos 		dolastlogx(logfile, argc, argv);
    193   1.9  christos 	else
    194   1.9  christos #endif
    195   1.9  christos #if defined(SUPPORT_UTMP)
    196   1.9  christos 		dolastlog(logfile, argc, argv);
    197   1.9  christos #endif
    198   1.9  christos 
    199   1.9  christos 	setpassent(0);	/* Close passwd file pointers */
    200   1.9  christos 
    201  1.16       kim 	if (outstack) {
    202  1.16       kim 		if (SIZECOLUMNS)
    203  1.16       kim 			sizecolumns(outstack);
    204  1.16       kim 
    205  1.16       kim 		if (DOSORT(sortlog))
    206  1.16       kim 			sort_and_output_stack(outstack);
    207  1.16       kim 		else
    208  1.16       kim 			output_stack(outstack);
    209  1.16       kim 	}
    210   1.9  christos 
    211   1.9  christos 	return 0;
    212   1.9  christos }
    213   1.9  christos 
    214   1.9  christos #ifdef SUPPORT_UTMP
    215   1.9  christos static void
    216   1.9  christos dolastlog(const char *logfile, int argc, char **argv)
    217   1.9  christos {
    218   1.9  christos 	int i;
    219   1.9  christos 	FILE *fp = fopen(logfile, "r");
    220   1.9  christos 	struct passwd	*passwd;
    221   1.9  christos 	struct lastlog l;
    222   1.9  christos 
    223   1.1      phil 	if (fp == NULL)
    224   1.1      phil 		err(1, "%s", logfile);
    225   1.1      phil 
    226   1.1      phil 	/* Process usernames given on the command line. */
    227   1.8      wulf 	if (argc > 0) {
    228   1.9  christos 		off_t offset;
    229   1.8      wulf 		for (i = 0; i < argc; i++) {
    230   1.1      phil 			if ((passwd = getpwnam(argv[i])) == NULL) {
    231   1.1      phil 				warnx("user '%s' not found", argv[i]);
    232   1.1      phil 				continue;
    233   1.1      phil 			}
    234   1.1      phil 			/* Calculate the offset into the lastlog file. */
    235   1.9  christos 			offset = passwd->pw_uid * sizeof(l);
    236   1.9  christos 			if (fseeko(fp, offset, SEEK_SET)) {
    237   1.1      phil 				warn("fseek error");
    238   1.1      phil 				continue;
    239   1.1      phil 			}
    240   1.9  christos 			if (fread(&l, sizeof(l), 1, fp) != 1) {
    241   1.1      phil 				warnx("fread error on '%s'", passwd->pw_name);
    242   1.1      phil 				clearerr(fp);
    243   1.1      phil 				continue;
    244   1.1      phil 			}
    245   1.9  christos 			process_entry(passwd, &l);
    246   1.1      phil 		}
    247   1.1      phil 	}
    248   1.1      phil 	/* Read all lastlog entries, looking for active ones */
    249   1.1      phil 	else {
    250   1.9  christos 		for (i = 0; fread(&l, sizeof(l), 1, fp) == 1; i++) {
    251   1.9  christos 			if (l.ll_time == 0)
    252   1.2  christos 				continue;
    253  1.13    atatat 			if ((passwd = getpwuid(i)) == NULL) {
    254  1.13    atatat 				static struct passwd p;
    255  1.13    atatat 				static char n[32];
    256  1.13    atatat 				snprintf(n, sizeof(n), "(%d)", i);
    257  1.13    atatat 				p.pw_uid = i;
    258  1.13    atatat 				p.pw_name = n;
    259  1.13    atatat 				passwd = &p;
    260  1.13    atatat 			}
    261  1.13    atatat 			process_entry(passwd, &l);
    262   1.1      phil 		}
    263   1.1      phil 		if (ferror(fp))
    264   1.1      phil 			warnx("fread error");
    265   1.1      phil 	}
    266   1.1      phil 
    267   1.9  christos 	(void)fclose(fp);
    268   1.9  christos }
    269   1.9  christos 
    270   1.9  christos static void
    271   1.9  christos process_entry(struct passwd *p, struct lastlog *l)
    272   1.9  christos {
    273   1.9  christos 	struct output	o;
    274   1.9  christos 
    275  1.18       kim 	memset(&o, 0, sizeof(o));
    276  1.17       kim 	if (numeric > 1)
    277  1.17       kim 		(void)snprintf(o.o_name, sizeof(o.o_name), "%d", p->pw_uid);
    278  1.17       kim 	else
    279  1.17       kim 		(void)strlcpy(o.o_name, p->pw_name, sizeof(o.o_name));
    280  1.18       kim 	(void)memcpy(o.o_line, l->ll_line, sizeof(l->ll_line));
    281  1.18       kim 	(void)memcpy(o.o_host, l->ll_host, sizeof(l->ll_host));
    282   1.9  christos 	o.o_tv.tv_sec = l->ll_time;
    283   1.9  christos 	o.o_tv.tv_usec = 0;
    284   1.9  christos 	o.next = NULL;
    285   1.1      phil 
    286   1.9  christos 	/*
    287  1.16       kim 	 * If we are dynamically sizing the columns or sorting the log,
    288  1.16       kim 	 * we need all the entries in memory so push the current entry
    289  1.16       kim 	 * onto a stack.  Otherwise, we can just output it.
    290   1.9  christos 	 */
    291  1.16       kim 	if (SIZECOLUMNS || DOSORT(sortlog))
    292  1.16       kim 		push_record(&o);
    293   1.9  christos 	else
    294  1.16       kim 		output_record(&o);
    295   1.9  christos }
    296   1.9  christos #endif
    297   1.9  christos 
    298   1.9  christos #ifdef SUPPORT_UTMPX
    299   1.9  christos static void
    300   1.9  christos dolastlogx(const char *logfile, int argc, char **argv)
    301   1.9  christos {
    302   1.9  christos 	int i = 0;
    303   1.9  christos 	DB *db = dbopen(logfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
    304   1.9  christos 	DBT key, data;
    305   1.9  christos 	struct lastlogx l;
    306   1.9  christos 	struct passwd	*passwd;
    307   1.9  christos 
    308   1.9  christos 	if (db == NULL)
    309   1.9  christos 		err(1, "%s", logfile);
    310   1.9  christos 
    311   1.9  christos 	if (argc > 0) {
    312   1.9  christos 		for (i = 0; i < argc; i++) {
    313   1.9  christos 			if ((passwd = getpwnam(argv[i])) == NULL) {
    314   1.9  christos 				warnx("User `%s' not found", argv[i]);
    315   1.9  christos 				continue;
    316   1.9  christos 			}
    317   1.9  christos 			key.data = &passwd->pw_uid;
    318   1.9  christos 			key.size = sizeof(passwd->pw_uid);
    319   1.9  christos 
    320   1.9  christos 			switch ((*db->get)(db, &key, &data, 0)) {
    321   1.9  christos 			case 0:
    322   1.9  christos 				break;
    323   1.9  christos 			case 1:
    324   1.9  christos 				warnx("User `%s' not found", passwd->pw_name);
    325   1.9  christos 				continue;
    326   1.9  christos 			case -1:
    327   1.9  christos 				warn("Error looking up `%s'", passwd->pw_name);
    328   1.9  christos 				continue;
    329   1.9  christos 			default:
    330   1.9  christos 				abort();
    331   1.9  christos 			}
    332   1.9  christos 
    333   1.9  christos 			if (data.size != sizeof(l)) {
    334   1.9  christos 				errno = EFTYPE;
    335   1.9  christos 				err(1, "%s", logfile);
    336   1.9  christos 			}
    337   1.9  christos 			(void)memcpy(&l, data.data, sizeof(l));
    338   1.9  christos 
    339   1.9  christos 			process_entryx(passwd, &l);
    340   1.9  christos 		}
    341   1.9  christos 	}
    342   1.9  christos 	/* Read all lastlog entries, looking for active ones */
    343   1.9  christos 	else {
    344   1.9  christos 		switch ((*db->seq)(db, &key, &data, R_FIRST)) {
    345   1.9  christos 		case 0:
    346   1.9  christos 			break;
    347   1.9  christos 		case 1:
    348   1.9  christos 			warnx("No entries found");
    349   1.9  christos 			(*db->close)(db);
    350   1.9  christos 			return;
    351   1.9  christos 		case -1:
    352   1.9  christos 			warn("Error seeking to first entry");
    353   1.9  christos 			(*db->close)(db);
    354   1.9  christos 			return;
    355   1.9  christos 		default:
    356   1.9  christos 			abort();
    357   1.9  christos 		}
    358   1.9  christos 
    359   1.9  christos 		do {
    360   1.9  christos 			uid_t uid;
    361   1.9  christos 
    362   1.9  christos 			if (key.size != sizeof(uid) || data.size != sizeof(l)) {
    363   1.9  christos 				errno = EFTYPE;
    364   1.9  christos 				err(1, "%s", logfile);
    365   1.9  christos 			}
    366   1.9  christos 			(void)memcpy(&uid, key.data, sizeof(uid));
    367   1.9  christos 
    368   1.9  christos 			if ((passwd = getpwuid(uid)) == NULL) {
    369  1.17       kim 				static struct passwd p;
    370  1.17       kim 				static char n[32];
    371  1.17       kim 				snprintf(n, sizeof(n), "(%d)", i);
    372  1.17       kim 				p.pw_uid = i;
    373  1.17       kim 				p.pw_name = n;
    374  1.17       kim 				passwd = &p;
    375   1.9  christos 			}
    376   1.9  christos 			(void)memcpy(&l, data.data, sizeof(l));
    377   1.9  christos 			process_entryx(passwd, &l);
    378   1.9  christos 		} while ((i = (*db->seq)(db, &key, &data, R_NEXT)) == 0);
    379   1.9  christos 
    380   1.9  christos 		switch (i) {
    381   1.9  christos 		case 1:
    382   1.9  christos 			break;
    383   1.9  christos 		case -1:
    384   1.9  christos 			warn("Error seeking to last entry");
    385   1.9  christos 			break;
    386   1.9  christos 		case 0:
    387   1.9  christos 		default:
    388   1.9  christos 			abort();
    389   1.9  christos 		}
    390   1.9  christos 	}
    391   1.7     elric 
    392   1.9  christos 	(*db->close)(db);
    393   1.1      phil }
    394   1.1      phil 
    395   1.7     elric static void
    396   1.9  christos process_entryx(struct passwd *p, struct lastlogx *l)
    397   1.7     elric {
    398   1.7     elric 	struct output	o;
    399   1.7     elric 
    400  1.18       kim 	memset(&o, 0, sizeof(o));
    401  1.13    atatat 	if (numeric > 1)
    402  1.13    atatat 		(void)snprintf(o.o_name, sizeof(o.o_name), "%d", p->pw_uid);
    403  1.13    atatat 	else
    404  1.13    atatat 		(void)strlcpy(o.o_name, p->pw_name, sizeof(o.o_name));
    405  1.18       kim 	(void)memcpy(o.o_line, l->ll_line, sizeof(l->ll_line));
    406  1.17       kim 	if (numeric)
    407  1.16       kim 		(void)sockaddr_snprintf(o.o_host, sizeof(o.o_host), "%a",
    408  1.16       kim 		    (struct sockaddr *)&l->ll_ss);
    409  1.17       kim 	else
    410  1.18       kim 		(void)memcpy(o.o_host, l->ll_host, sizeof(l->ll_host));
    411   1.9  christos 	o.o_tv = l->ll_tv;
    412   1.7     elric 	o.next = NULL;
    413   1.7     elric 
    414   1.7     elric 	/*
    415  1.16       kim 	 * If we are dynamically sizing the columns or sorting the log,
    416  1.16       kim 	 * we need all the entries in memory so push the current entry
    417  1.16       kim 	 * onto a stack.  Otherwise, we can just output it.
    418   1.7     elric 	 */
    419  1.16       kim 	if (SIZECOLUMNS || DOSORT(sortlog))
    420  1.16       kim 		push_record(&o);
    421   1.7     elric 	else
    422  1.16       kim 		output_record(&o);
    423   1.7     elric }
    424   1.9  christos #endif
    425   1.7     elric 
    426   1.7     elric static void
    427  1.16       kim push_record(struct output *o)
    428   1.7     elric {
    429   1.7     elric 	struct output	*out;
    430   1.7     elric 
    431   1.7     elric 	out = malloc(sizeof(*out));
    432   1.7     elric 	if (!out)
    433   1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    434   1.9  christos 	(void)memcpy(out, o, sizeof(*out));
    435   1.7     elric 	out->next = NULL;
    436   1.7     elric 
    437   1.7     elric 	if (outstack) {
    438   1.7     elric 		out->next = outstack;
    439   1.7     elric 		outstack = out;
    440   1.7     elric 	} else {
    441   1.7     elric 		outstack = out;
    442   1.7     elric 	}
    443   1.7     elric }
    444   1.7     elric 
    445   1.7     elric static void
    446  1.16       kim sizecolumns(struct output *stack)
    447  1.16       kim {
    448  1.16       kim 	struct	output *o;
    449  1.16       kim 	size_t	len;
    450  1.16       kim 
    451  1.16       kim 	if (!namelen)
    452  1.16       kim 		for (o = stack; o; o = o->next) {
    453  1.16       kim 			len = strlen(o->o_name);
    454  1.16       kim 			if (namelen < len)
    455  1.16       kim 				namelen = len;
    456  1.16       kim 		}
    457  1.16       kim 
    458  1.16       kim 	if (!linelen)
    459  1.16       kim 		for (o = stack; o; o = o->next) {
    460  1.16       kim 			len = strlen(o->o_line);
    461  1.16       kim 			if (linelen < len)
    462  1.16       kim 				linelen = len;
    463  1.16       kim 		}
    464  1.16       kim 
    465  1.16       kim 	if (!hostlen)
    466  1.16       kim 		for (o = stack; o; o = o->next) {
    467  1.16       kim 			len = strlen(o->o_host);
    468  1.16       kim 			if (hostlen < len)
    469  1.16       kim 				hostlen = len;
    470  1.16       kim 		}
    471  1.16       kim }
    472  1.16       kim 
    473  1.16       kim static void
    474  1.16       kim output_stack(struct output *stack)
    475  1.16       kim {
    476  1.16       kim 	struct	output *o;
    477  1.16       kim 
    478  1.16       kim 	for (o = stack; o; o = o->next)
    479  1.16       kim 		output_record(o);
    480  1.16       kim }
    481  1.16       kim 
    482  1.16       kim static void
    483  1.16       kim sort_and_output_stack(struct output *o)
    484   1.7     elric {
    485   1.7     elric 	struct	output **outs;
    486   1.7     elric 	struct	output *tmpo;
    487   1.7     elric 	int	num;
    488   1.7     elric 	int	i;
    489   1.7     elric 
    490   1.7     elric 	/* count the number of entries to display */
    491  1.16       kim 	for (num=0, tmpo = o; tmpo; tmpo = tmpo->next, num++)
    492   1.7     elric 		;
    493   1.7     elric 
    494   1.7     elric 	outs = malloc(sizeof(*outs) * num);
    495   1.7     elric 	if (!outs)
    496   1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    497   1.7     elric 	for (i=0, tmpo = o; i < num; tmpo=tmpo->next, i++)
    498   1.7     elric 		outs[i] = tmpo;
    499   1.7     elric 
    500   1.7     elric 	mergesort(outs, num, sizeof(*outs), comparelog);
    501   1.7     elric 
    502   1.7     elric 	for (i=0; i < num; i++)
    503  1.16       kim 		output_record(outs[i]);
    504   1.7     elric }
    505   1.7     elric 
    506   1.7     elric static int
    507   1.7     elric comparelog(const void *left, const void *right)
    508   1.7     elric {
    509  1.14     lukem 	const struct output *l = *(const struct output * const *)left;
    510  1.14     lukem 	const struct output *r = *(const struct output * const *)right;
    511   1.7     elric 	int order = (sortlog&SORT_REVERSE)?-1:1;
    512   1.7     elric 
    513   1.9  christos 	if (l->o_tv.tv_sec < r->o_tv.tv_sec)
    514   1.7     elric 		return 1 * order;
    515   1.9  christos 	if (l->o_tv.tv_sec == r->o_tv.tv_sec)
    516   1.7     elric 		return 0;
    517   1.7     elric 	return -1 * order;
    518   1.7     elric }
    519   1.7     elric 
    520   1.1      phil /* Duplicate the output of last(1) */
    521   1.1      phil static void
    522  1.16       kim output_record(struct output *o)
    523   1.1      phil {
    524   1.9  christos 	time_t t = (time_t)o->o_tv.tv_sec;
    525  1.16       kim 	printf("%-*.*s  %-*.*s  %-*.*s  %s",
    526  1.10        he 		(int)namelen, (int)namelen, o->o_name,
    527  1.10        he 		(int)linelen, (int)linelen, o->o_line,
    528  1.16       kim 		(int)hostlen, (int)hostlen, o->o_host,
    529   1.9  christos 		t ? ctime(&t) : "Never logged in\n");
    530   1.1      phil }
    531   1.1      phil 
    532   1.1      phil static void
    533   1.9  christos usage(void)
    534   1.1      phil {
    535   1.9  christos 	(void)fprintf(stderr, "Usage: %s [-nrt] [-f <filename>] "
    536   1.9  christos 	    "[-H <hostlen>] [-L <linelen>] [-N <namelen>] [user ...]\n",
    537   1.9  christos 	    getprogname());
    538   1.1      phil 	exit(1);
    539   1.1      phil }
    540