Home | History | Annotate | Line # | Download | only in last
last.c revision 1.29
      1  1.29  christos /*	$NetBSD: last.c,v 1.29 2006/01/22 15:47:10 christos Exp $	*/
      2   1.5       jtc 
      3   1.1       cgd /*
      4   1.5       jtc  * Copyright (c) 1987, 1993, 1994
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.8    kleink #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.8    kleink __COPYRIGHT(
     35   1.5       jtc "@(#) Copyright (c) 1987, 1993, 1994\n\
     36   1.8    kleink 	The Regents of the University of California.  All rights reserved.\n");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40   1.5       jtc #if 0
     41   1.5       jtc static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
     42   1.5       jtc #endif
     43  1.29  christos __RCSID("$NetBSD: last.c,v 1.29 2006/01/22 15:47:10 christos Exp $");
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd #include <sys/param.h>
     47   1.1       cgd #include <sys/stat.h>
     48   1.5       jtc 
     49   1.5       jtc #include <err.h>
     50   1.5       jtc #include <fcntl.h>
     51   1.5       jtc #include <paths.h>
     52   1.1       cgd #include <signal.h>
     53   1.5       jtc #include <stdio.h>
     54   1.5       jtc #include <stdlib.h>
     55   1.5       jtc #include <string.h>
     56   1.1       cgd #include <time.h>
     57   1.5       jtc #include <tzfile.h>
     58   1.5       jtc #include <unistd.h>
     59  1.23  christos #include <arpa/inet.h>
     60  1.16  christos #ifdef SUPPORT_UTMPX
     61  1.16  christos #include <utmpx.h>
     62  1.16  christos #endif
     63  1.16  christos #ifdef SUPPORT_UTMP
     64   1.1       cgd #include <utmp.h>
     65  1.16  christos #endif
     66  1.25  christos #include <util.h>
     67  1.16  christos 
     68  1.16  christos #ifndef UT_NAMESIZE
     69  1.16  christos #define UT_NAMESIZE 8
     70  1.16  christos #define UT_LINESIZE 8
     71  1.16  christos #define UT_HOSTSIZE 16
     72  1.16  christos #endif
     73  1.16  christos #ifndef SIGNATURE
     74  1.16  christos #define SIGNATURE -1
     75  1.16  christos #endif
     76  1.16  christos 
     77  1.16  christos 
     78   1.1       cgd 
     79  1.14    simonb #define	NO	0			/* false/no */
     80  1.14    simonb #define	YES	1			/* true/yes */
     81   1.1       cgd 
     82  1.14    simonb #define	TBUFLEN	30			/* length of time string buffer */
     83  1.15    simonb #define	TFMT	"%a %b %d %R"		/* strftime format string */
     84  1.15    simonb #define	LTFMT	"%a %b %d %Y %T"	/* strftime long format string */
     85  1.14    simonb #define	TFMTS	"%R"			/* strftime format string - time only */
     86  1.14    simonb #define	LTFMTS	"%T"			/* strftime long format string - " */
     87  1.14    simonb 
     88  1.14    simonb /* fmttime() flags */
     89  1.14    simonb #define	FULLTIME	0x1		/* show year, seconds */
     90  1.14    simonb #define	TIMEONLY	0x2		/* show time only, not date */
     91  1.14    simonb #define	GMT		0x4		/* show time at GMT, for offsets only */
     92  1.14    simonb 
     93  1.27  christos #define MAXUTMP		1024
     94   1.1       cgd 
     95   1.1       cgd typedef struct arg {
     96  1.14    simonb 	char	*name;			/* argument */
     97   1.1       cgd #define	HOST_TYPE	-2
     98   1.1       cgd #define	TTY_TYPE	-3
     99   1.1       cgd #define	USER_TYPE	-4
    100  1.14    simonb 	int	type;			/* type of arg */
    101  1.14    simonb 	struct arg	*next;		/* linked list pointer */
    102   1.1       cgd } ARG;
    103  1.16  christos static ARG	*arglist;		/* head of linked list */
    104   1.1       cgd 
    105   1.1       cgd typedef struct ttytab {
    106  1.14    simonb 	time_t	logout;			/* log out time */
    107  1.16  christos 	char	tty[128];		/* terminal name */
    108  1.14    simonb 	struct ttytab	*next;		/* linked list pointer */
    109   1.1       cgd } TTY;
    110  1.16  christos static TTY	*ttylist;		/* head of linked list */
    111   1.1       cgd 
    112  1.14    simonb static time_t	currentout;		/* current logout value */
    113  1.14    simonb static long	maxrec;			/* records to display */
    114  1.14    simonb static int	fulltime = 0;		/* Display seconds? */
    115  1.26  christos static int	xflag;			/* Assume file is wtmpx format */
    116   1.1       cgd 
    117  1.16  christos int	 main(int, char *[]);
    118  1.16  christos 
    119  1.16  christos static void	 addarg(int, char *);
    120  1.16  christos static TTY	*addtty(const char *);
    121  1.16  christos static void	 hostconv(char *);
    122  1.16  christos static char	*ttyconv(char *);
    123  1.16  christos #ifdef SUPPORT_UTMPX
    124  1.23  christos static void	 wtmpx(const char *, int, int, int, int);
    125  1.16  christos #endif
    126  1.16  christos #ifdef SUPPORT_UTMP
    127  1.23  christos static void	 wtmp(const char *, int, int, int, int);
    128  1.16  christos #endif
    129  1.16  christos static char	*fmttime(time_t, int);
    130  1.16  christos static void	 usage(void);
    131  1.16  christos 
    132  1.16  christos static
    133  1.16  christos void usage(void)
    134  1.16  christos {
    135  1.26  christos 	(void)fprintf(stderr, "usage: %s [-#%s] [-nTx] [-f file]"
    136  1.18       wiz 	    " [-H hostsize] [-h host] [-L linesize]\n"
    137  1.18       wiz 	    "\t    [-N namesize] [-t tty] [user ...]\n", getprogname(),
    138  1.18       wiz #ifdef NOTYET_SUPPORT_UTMPX
    139  1.16  christos 	    "w"
    140  1.16  christos #else
    141  1.16  christos 	    ""
    142  1.16  christos #endif
    143  1.16  christos 	);
    144  1.16  christos 	exit(1);
    145  1.16  christos }
    146   1.5       jtc 
    147   1.5       jtc int
    148  1.16  christos main(int argc, char *argv[])
    149   1.1       cgd {
    150   1.1       cgd 	int ch;
    151   1.5       jtc 	char *p;
    152  1.16  christos 	char	*file = NULL;
    153  1.16  christos 	int namesize = UT_NAMESIZE;
    154  1.16  christos 	int linesize = UT_LINESIZE;
    155  1.16  christos 	int hostsize = UT_HOSTSIZE;
    156  1.23  christos 	int numeric = 0;
    157   1.1       cgd 
    158   1.1       cgd 	maxrec = -1;
    159  1.16  christos 
    160  1.26  christos 	while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:x")) != -1)
    161   1.5       jtc 		switch (ch) {
    162   1.1       cgd 		case '0': case '1': case '2': case '3': case '4':
    163   1.1       cgd 		case '5': case '6': case '7': case '8': case '9':
    164   1.1       cgd 			/*
    165   1.1       cgd 			 * kludge: last was originally designed to take
    166   1.1       cgd 			 * a number after a dash.
    167   1.1       cgd 			 */
    168   1.1       cgd 			if (maxrec == -1) {
    169   1.1       cgd 				p = argv[optind - 1];
    170   1.1       cgd 				if (p[0] == '-' && p[1] == ch && !p[2])
    171   1.1       cgd 					maxrec = atol(++p);
    172  1.29  christos 				else if (optind < argc)
    173   1.1       cgd 					maxrec = atol(argv[optind] + 1);
    174  1.29  christos 				else
    175  1.29  christos 					usage();
    176   1.1       cgd 				if (!maxrec)
    177  1.29  christos 					return 0;
    178   1.1       cgd 			}
    179   1.1       cgd 			break;
    180   1.1       cgd 		case 'f':
    181   1.1       cgd 			file = optarg;
    182   1.1       cgd 			break;
    183  1.18       wiz 		case 'H':
    184  1.18       wiz 			hostsize = atoi(optarg);
    185  1.18       wiz 			break;
    186   1.1       cgd 		case 'h':
    187   1.1       cgd 			hostconv(optarg);
    188   1.1       cgd 			addarg(HOST_TYPE, optarg);
    189   1.1       cgd 			break;
    190  1.16  christos 		case 'L':
    191  1.16  christos 			linesize = atoi(optarg);
    192  1.16  christos 			break;
    193  1.18       wiz 		case 'N':
    194  1.18       wiz 			namesize = atoi(optarg);
    195  1.16  christos 			break;
    196  1.23  christos 		case 'n':
    197  1.23  christos 			numeric = 1;
    198  1.23  christos 			break;
    199   1.7     perry 		case 'T':
    200   1.7     perry 			fulltime = 1;
    201  1.18       wiz 			break;
    202  1.18       wiz 		case 't':
    203  1.18       wiz 			addarg(TTY_TYPE, ttyconv(optarg));
    204   1.7     perry 			break;
    205  1.26  christos 		case 'x':
    206  1.26  christos 			xflag = 1;
    207  1.26  christos 			break;
    208   1.1       cgd 		case '?':
    209   1.1       cgd 		default:
    210  1.16  christos 			usage();
    211   1.1       cgd 		}
    212   1.1       cgd 
    213   1.1       cgd 	if (argc) {
    214   1.1       cgd 		setlinebuf(stdout);
    215   1.1       cgd 		for (argv += optind; *argv; ++argv) {
    216   1.1       cgd #define	COMPATIBILITY
    217   1.1       cgd #ifdef	COMPATIBILITY
    218   1.1       cgd 			/* code to allow "last p5" to work */
    219   1.1       cgd 			addarg(TTY_TYPE, ttyconv(*argv));
    220   1.1       cgd #endif
    221   1.1       cgd 			addarg(USER_TYPE, *argv);
    222   1.1       cgd 		}
    223   1.1       cgd 	}
    224  1.16  christos 	if (file == NULL) {
    225  1.16  christos #ifdef SUPPORT_UTMPX
    226  1.16  christos 		if (access(_PATH_WTMPX, R_OK) == 0)
    227  1.16  christos 			file = _PATH_WTMPX;
    228  1.16  christos 		else
    229  1.16  christos #endif
    230  1.16  christos #ifdef SUPPORT_UTMP
    231  1.16  christos 		if (access(_PATH_WTMP, R_OK) == 0)
    232  1.16  christos 			file = _PATH_WTMP;
    233  1.16  christos #endif
    234  1.16  christos 		if (file == NULL)
    235  1.16  christos #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    236  1.16  christos 			errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
    237  1.16  christos 			    _PATH_WTMP);
    238  1.16  christos #elif defined(SUPPORT_UTMPX)
    239  1.16  christos 			errx(1, "Cannot access `%s'", _PATH_WTMPX);
    240  1.16  christos #elif defined(SUPPORT_UTMP)
    241  1.16  christos 			errx(1, "Cannot access `%s'", _PATH_WTMP);
    242  1.16  christos #else
    243  1.16  christos 			errx(1, "No utmp or utmpx support compiled in.");
    244  1.16  christos #endif
    245  1.16  christos 	}
    246  1.16  christos #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    247  1.26  christos 	if (file[strlen(file) - 1] == 'x' || xflag)
    248  1.23  christos 		wtmpx(file, namesize, linesize, hostsize, numeric);
    249  1.16  christos 	else
    250  1.23  christos 		wtmp(file, namesize, linesize, hostsize, numeric);
    251  1.16  christos #elif defined(SUPPORT_UTMPX)
    252  1.23  christos 	wtmpx(file, namesize, linesize, hostsize, numeric);
    253  1.16  christos #elif defined(SUPPORT_UTMP)
    254  1.23  christos 	wtmp(file, namesize, linesize, hostsize, numeric);
    255  1.16  christos #else
    256  1.19       wiz 	errx(1, "No utmp or utmpx support compiled in.");
    257  1.16  christos #endif
    258   1.1       cgd 	exit(0);
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd 
    262   1.1       cgd /*
    263   1.1       cgd  * addarg --
    264   1.1       cgd  *	add an entry to a linked list of arguments
    265   1.1       cgd  */
    266  1.16  christos static void
    267  1.16  christos addarg(int type, char *arg)
    268   1.1       cgd {
    269   1.5       jtc 	ARG *cur;
    270   1.1       cgd 
    271   1.5       jtc 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
    272   1.5       jtc 		err(1, "malloc failure");
    273   1.1       cgd 	cur->next = arglist;
    274   1.1       cgd 	cur->type = type;
    275   1.1       cgd 	cur->name = arg;
    276   1.1       cgd 	arglist = cur;
    277   1.1       cgd }
    278   1.1       cgd 
    279   1.1       cgd /*
    280   1.1       cgd  * addtty --
    281   1.1       cgd  *	add an entry to a linked list of ttys
    282   1.1       cgd  */
    283  1.16  christos static TTY *
    284  1.16  christos addtty(const char *ttyname)
    285   1.1       cgd {
    286   1.5       jtc 	TTY *cur;
    287   1.1       cgd 
    288   1.5       jtc 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
    289   1.5       jtc 		err(1, "malloc failure");
    290   1.1       cgd 	cur->next = ttylist;
    291   1.1       cgd 	cur->logout = currentout;
    292  1.16  christos 	memmove(cur->tty, ttyname, sizeof(cur->tty));
    293   1.5       jtc 	return (ttylist = cur);
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.1       cgd /*
    297   1.1       cgd  * hostconv --
    298   1.1       cgd  *	convert the hostname to search pattern; if the supplied host name
    299   1.1       cgd  *	has a domain attached that is the same as the current domain, rip
    300   1.1       cgd  *	off the domain suffix since that's what login(1) does.
    301   1.1       cgd  */
    302  1.16  christos static void
    303  1.16  christos hostconv(char *arg)
    304   1.1       cgd {
    305   1.1       cgd 	static int first = 1;
    306  1.10       mrg 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
    307   1.5       jtc 	char *argdot;
    308   1.1       cgd 
    309   1.5       jtc 	if (!(argdot = strchr(arg, '.')))
    310   1.1       cgd 		return;
    311   1.1       cgd 	if (first) {
    312   1.1       cgd 		first = 0;
    313   1.5       jtc 		if (gethostname(name, sizeof(name)))
    314   1.5       jtc 			err(1, "gethostname");
    315  1.10       mrg 		name[sizeof(name) - 1] = '\0';
    316   1.5       jtc 		hostdot = strchr(name, '.');
    317   1.1       cgd 	}
    318   1.1       cgd 	if (hostdot && !strcasecmp(hostdot, argdot))
    319   1.1       cgd 		*argdot = '\0';
    320   1.1       cgd }
    321   1.1       cgd 
    322   1.1       cgd /*
    323   1.1       cgd  * ttyconv --
    324   1.1       cgd  *	convert tty to correct name.
    325   1.1       cgd  */
    326  1.16  christos static char *
    327  1.16  christos ttyconv(char *arg)
    328   1.1       cgd {
    329   1.5       jtc 	char *mval;
    330   1.1       cgd 
    331   1.1       cgd 	/*
    332   1.1       cgd 	 * kludge -- we assume that all tty's end with
    333   1.1       cgd 	 * a two character suffix.
    334   1.1       cgd 	 */
    335   1.1       cgd 	if (strlen(arg) == 2) {
    336  1.20    itojun 		if (!strcmp(arg, "co"))
    337  1.20    itojun 			mval = strdup("console");
    338  1.20    itojun 		else
    339  1.20    itojun 			asprintf(&mval, "tty%s", arg);
    340  1.20    itojun 		if (!mval)
    341   1.5       jtc 			err(1, "malloc failure");
    342   1.5       jtc 		return (mval);
    343   1.1       cgd 	}
    344   1.1       cgd 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
    345   1.5       jtc 		return (arg + 5);
    346   1.5       jtc 	return (arg);
    347   1.1       cgd }
    348   1.1       cgd 
    349   1.1       cgd /*
    350  1.14    simonb  * fmttime --
    351  1.14    simonb  *	return pointer to (static) formatted time string.
    352  1.14    simonb  */
    353  1.16  christos static char *
    354  1.16  christos fmttime(time_t t, int flags)
    355  1.14    simonb {
    356  1.14    simonb 	struct tm *tm;
    357  1.14    simonb 	static char tbuf[TBUFLEN];
    358  1.14    simonb 
    359  1.14    simonb 	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
    360  1.14    simonb 	strftime(tbuf, sizeof(tbuf),
    361  1.14    simonb 	    (flags & TIMEONLY)
    362  1.14    simonb 	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
    363  1.14    simonb 	     : (flags & FULLTIME ? LTFMT : TFMT),
    364  1.14    simonb 	    tm);
    365  1.14    simonb 	return (tbuf);
    366  1.14    simonb }
    367  1.14    simonb 
    368  1.16  christos #ifdef SUPPORT_UTMP
    369  1.16  christos #define TYPE(a)	0
    370  1.16  christos #define NAMESIZE UT_NAMESIZE
    371  1.16  christos #define LINESIZE UT_LINESIZE
    372  1.16  christos #define HOSTSIZE UT_HOSTSIZE
    373  1.16  christos #define ut_timefld ut_time
    374  1.16  christos #define FIRSTVALID 0
    375  1.16  christos #include "want.c"
    376  1.17      matt #undef TYPE /*(a)*/
    377  1.16  christos #undef NAMESIZE
    378  1.16  christos #undef LINESIZE
    379  1.16  christos #undef HOSTSIZE
    380  1.16  christos #undef ut_timefld
    381  1.16  christos #undef FIRSTVALID
    382  1.16  christos #endif
    383   1.1       cgd 
    384  1.16  christos #ifdef SUPPORT_UTMPX
    385  1.16  christos #define utmp utmpx
    386  1.16  christos #define want wantx
    387  1.16  christos #define wtmp wtmpx
    388  1.23  christos #define gethost gethostx
    389  1.16  christos #define buf bufx
    390  1.16  christos #define onintr onintrx
    391  1.16  christos #define TYPE(a) (a)->ut_type
    392  1.16  christos #define NAMESIZE UTX_USERSIZE
    393  1.16  christos #define LINESIZE UTX_LINESIZE
    394  1.16  christos #define HOSTSIZE UTX_HOSTSIZE
    395  1.16  christos #define ut_timefld ut_xtime
    396  1.16  christos #define FIRSTVALID 1
    397  1.16  christos #include "want.c"
    398  1.16  christos #endif
    399