Home | History | Annotate | Line # | Download | only in last
last.c revision 1.3
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1987 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1987 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)last.c	5.18 (Berkeley) 3/1/91";*/
     42  1.3      cgd static char rcsid[] = "$Id: last.c,v 1.3 1994/03/28 07:26:41 cgd Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /*
     46  1.1      cgd  * last
     47  1.1      cgd  */
     48  1.1      cgd #include <sys/param.h>
     49  1.1      cgd #include <sys/stat.h>
     50  1.1      cgd #include <sys/file.h>
     51  1.1      cgd #include <signal.h>
     52  1.1      cgd #include <time.h>
     53  1.1      cgd #include <utmp.h>
     54  1.1      cgd #include <stdio.h>
     55  1.1      cgd #include <paths.h>
     56  1.1      cgd 
     57  1.1      cgd #define	SECDAY	(24*60*60)			/* seconds in a day */
     58  1.1      cgd #define	NO	0				/* false/no */
     59  1.1      cgd #define	YES	1				/* true/yes */
     60  1.1      cgd 
     61  1.1      cgd static struct utmp	buf[1024];		/* utmp read buffer */
     62  1.1      cgd 
     63  1.1      cgd typedef struct arg {
     64  1.1      cgd 	char	*name;				/* argument */
     65  1.1      cgd #define	HOST_TYPE	-2
     66  1.1      cgd #define	TTY_TYPE	-3
     67  1.1      cgd #define	USER_TYPE	-4
     68  1.1      cgd 	int	type;				/* type of arg */
     69  1.1      cgd 	struct arg	*next;			/* linked list pointer */
     70  1.1      cgd } ARG;
     71  1.1      cgd ARG	*arglist;				/* head of linked list */
     72  1.1      cgd 
     73  1.1      cgd typedef struct ttytab {
     74  1.1      cgd 	long	logout;				/* log out time */
     75  1.1      cgd 	char	tty[UT_LINESIZE + 1];		/* terminal name */
     76  1.1      cgd 	struct ttytab	*next;			/* linked list pointer */
     77  1.1      cgd } TTY;
     78  1.1      cgd TTY	*ttylist;				/* head of linked list */
     79  1.1      cgd 
     80  1.1      cgd static long	currentout,			/* current logout value */
     81  1.1      cgd 		maxrec;				/* records to display */
     82  1.1      cgd static char	*file = _PATH_WTMP;		/* wtmp file */
     83  1.1      cgd 
     84  1.1      cgd main(argc, argv)
     85  1.1      cgd 	int argc;
     86  1.1      cgd 	char **argv;
     87  1.1      cgd {
     88  1.1      cgd 	extern int optind;
     89  1.1      cgd 	extern char *optarg;
     90  1.1      cgd 	int ch;
     91  1.1      cgd 	long atol();
     92  1.1      cgd 	char *p, *ttyconv();
     93  1.1      cgd 
     94  1.1      cgd 	maxrec = -1;
     95  1.1      cgd 	while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF)
     96  1.1      cgd 		switch((char)ch) {
     97  1.1      cgd 		case '0': case '1': case '2': case '3': case '4':
     98  1.1      cgd 		case '5': case '6': case '7': case '8': case '9':
     99  1.1      cgd 			/*
    100  1.1      cgd 			 * kludge: last was originally designed to take
    101  1.1      cgd 			 * a number after a dash.
    102  1.1      cgd 			 */
    103  1.1      cgd 			if (maxrec == -1) {
    104  1.1      cgd 				p = argv[optind - 1];
    105  1.1      cgd 				if (p[0] == '-' && p[1] == ch && !p[2])
    106  1.1      cgd 					maxrec = atol(++p);
    107  1.1      cgd 				else
    108  1.1      cgd 					maxrec = atol(argv[optind] + 1);
    109  1.1      cgd 				if (!maxrec)
    110  1.1      cgd 					exit(0);
    111  1.1      cgd 			}
    112  1.1      cgd 			break;
    113  1.1      cgd 		case 'f':
    114  1.1      cgd 			file = optarg;
    115  1.1      cgd 			break;
    116  1.1      cgd 		case 'h':
    117  1.1      cgd 			hostconv(optarg);
    118  1.1      cgd 			addarg(HOST_TYPE, optarg);
    119  1.1      cgd 			break;
    120  1.1      cgd 		case 't':
    121  1.1      cgd 			addarg(TTY_TYPE, ttyconv(optarg));
    122  1.1      cgd 			break;
    123  1.1      cgd 		case '?':
    124  1.1      cgd 		default:
    125  1.1      cgd 			fputs("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n", stderr);
    126  1.1      cgd 			exit(1);
    127  1.1      cgd 		}
    128  1.1      cgd 
    129  1.1      cgd 	if (argc) {
    130  1.1      cgd 		setlinebuf(stdout);
    131  1.1      cgd 		for (argv += optind; *argv; ++argv) {
    132  1.1      cgd #define	COMPATIBILITY
    133  1.1      cgd #ifdef	COMPATIBILITY
    134  1.1      cgd 			/* code to allow "last p5" to work */
    135  1.1      cgd 			addarg(TTY_TYPE, ttyconv(*argv));
    136  1.1      cgd #endif
    137  1.1      cgd 			addarg(USER_TYPE, *argv);
    138  1.1      cgd 		}
    139  1.1      cgd 	}
    140  1.1      cgd 	wtmp();
    141  1.1      cgd 	exit(0);
    142  1.1      cgd }
    143  1.1      cgd 
    144  1.1      cgd /*
    145  1.1      cgd  * wtmp --
    146  1.1      cgd  *	read through the wtmp file
    147  1.1      cgd  */
    148  1.1      cgd wtmp()
    149  1.1      cgd {
    150  1.1      cgd 	register struct utmp	*bp;		/* current structure */
    151  1.1      cgd 	register TTY	*T;			/* tty list entry */
    152  1.1      cgd 	struct stat	stb;			/* stat of file for size */
    153  1.1      cgd 	long	bl, delta,			/* time difference */
    154  1.3      cgd 		time();
    155  1.1      cgd 	int	bytes, wfd;
    156  1.1      cgd 	char	*ct, *crmsg,
    157  1.1      cgd 		*asctime(), *ctime(), *strcpy();
    158  1.1      cgd 	TTY	*addtty();
    159  1.1      cgd 	void	onintr();
    160  1.1      cgd 
    161  1.1      cgd 	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) {
    162  1.1      cgd 		perror(file);
    163  1.1      cgd 		exit(1);
    164  1.1      cgd 	}
    165  1.1      cgd 	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
    166  1.1      cgd 
    167  1.1      cgd 	(void)time(&buf[0].ut_time);
    168  1.1      cgd 	(void)signal(SIGINT, onintr);
    169  1.1      cgd 	(void)signal(SIGQUIT, onintr);
    170  1.1      cgd 
    171  1.1      cgd 	while (--bl >= 0) {
    172  1.1      cgd 		if (lseek(wfd, (long)(bl * sizeof(buf)), L_SET) == -1 ||
    173  1.1      cgd 		    (bytes = read(wfd, (char *)buf, sizeof(buf))) == -1) {
    174  1.1      cgd 			fprintf(stderr, "last: %s: ", file);
    175  1.1      cgd 			perror((char *)NULL);
    176  1.1      cgd 			exit(1);
    177  1.1      cgd 		}
    178  1.1      cgd 		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
    179  1.1      cgd 			/*
    180  1.1      cgd 			 * if the terminal line is '~', the machine stopped.
    181  1.1      cgd 			 * see utmp(5) for more info.
    182  1.1      cgd 			 */
    183  1.1      cgd 			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
    184  1.1      cgd 				/* everybody just logged out */
    185  1.1      cgd 				for (T = ttylist; T; T = T->next)
    186  1.1      cgd 					T->logout = -bp->ut_time;
    187  1.1      cgd 				currentout = -bp->ut_time;
    188  1.1      cgd 				crmsg = strncmp(bp->ut_name, "shutdown",
    189  1.1      cgd 				    UT_NAMESIZE) ? "crash" : "shutdown";
    190  1.1      cgd 				if (want(bp, NO)) {
    191  1.1      cgd 					ct = ctime(&bp->ut_time);
    192  1.1      cgd 					printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
    193  1.1      cgd 					if (maxrec != -1 && !--maxrec)
    194  1.1      cgd 						return;
    195  1.1      cgd 				}
    196  1.1      cgd 				continue;
    197  1.1      cgd 			}
    198  1.1      cgd 			/*
    199  1.1      cgd 			 * if the line is '{' or '|', date got set; see
    200  1.1      cgd 			 * utmp(5) for more info.
    201  1.1      cgd 			 */
    202  1.1      cgd 			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
    203  1.1      cgd 			    && !bp->ut_line[1]) {
    204  1.1      cgd 				if (want(bp, NO)) {
    205  1.1      cgd 					ct = ctime(&bp->ut_time);
    206  1.1      cgd 					printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
    207  1.1      cgd 					if (maxrec && !--maxrec)
    208  1.1      cgd 						return;
    209  1.1      cgd 				}
    210  1.1      cgd 				continue;
    211  1.1      cgd 			}
    212  1.1      cgd 			/* find associated tty */
    213  1.1      cgd 			for (T = ttylist;; T = T->next) {
    214  1.1      cgd 				if (!T) {
    215  1.1      cgd 					/* add new one */
    216  1.1      cgd 					T = addtty(bp->ut_line);
    217  1.1      cgd 					break;
    218  1.1      cgd 				}
    219  1.1      cgd 				if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
    220  1.1      cgd 					break;
    221  1.1      cgd 			}
    222  1.1      cgd 			if (bp->ut_name[0] && want(bp, YES)) {
    223  1.1      cgd 				ct = ctime(&bp->ut_time);
    224  1.1      cgd 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s ", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
    225  1.1      cgd 				if (!T->logout)
    226  1.1      cgd 					puts("  still logged in");
    227  1.1      cgd 				else {
    228  1.1      cgd 					if (T->logout < 0) {
    229  1.1      cgd 						T->logout = -T->logout;
    230  1.1      cgd 						printf("- %s", crmsg);
    231  1.1      cgd 					}
    232  1.1      cgd 					else
    233  1.1      cgd 						printf("- %5.5s", ctime(&T->logout)+11);
    234  1.1      cgd 					delta = T->logout - bp->ut_time;
    235  1.1      cgd 					if (delta < SECDAY)
    236  1.1      cgd 						printf("  (%5.5s)\n", asctime(gmtime(&delta))+11);
    237  1.1      cgd 					else
    238  1.1      cgd 						printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
    239  1.1      cgd 				}
    240  1.1      cgd 				if (maxrec != -1 && !--maxrec)
    241  1.1      cgd 					return;
    242  1.1      cgd 			}
    243  1.1      cgd 			T->logout = bp->ut_time;
    244  1.1      cgd 		}
    245  1.1      cgd 	}
    246  1.1      cgd 	ct = ctime(&buf[0].ut_time);
    247  1.1      cgd 	printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
    248  1.1      cgd }
    249  1.1      cgd 
    250  1.1      cgd /*
    251  1.1      cgd  * want --
    252  1.1      cgd  *	see if want this entry
    253  1.1      cgd  */
    254  1.1      cgd want(bp, check)
    255  1.1      cgd 	register struct utmp *bp;
    256  1.1      cgd 	int check;
    257  1.1      cgd {
    258  1.1      cgd 	register ARG *step;
    259  1.1      cgd 
    260  1.1      cgd 	if (check)
    261  1.1      cgd 		/*
    262  1.1      cgd 		 * when uucp and ftp log in over a network, the entry in
    263  1.1      cgd 		 * the utmp file is the name plus their process id.  See
    264  1.1      cgd 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    265  1.1      cgd 		 */
    266  1.1      cgd 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    267  1.1      cgd 			bp->ut_line[3] = '\0';
    268  1.1      cgd 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    269  1.1      cgd 			bp->ut_line[4] = '\0';
    270  1.1      cgd 	if (!arglist)
    271  1.1      cgd 		return(YES);
    272  1.1      cgd 
    273  1.1      cgd 	for (step = arglist; step; step = step->next)
    274  1.1      cgd 		switch(step->type) {
    275  1.1      cgd 		case HOST_TYPE:
    276  1.1      cgd 			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
    277  1.1      cgd 				return(YES);
    278  1.1      cgd 			break;
    279  1.1      cgd 		case TTY_TYPE:
    280  1.1      cgd 			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
    281  1.1      cgd 				return(YES);
    282  1.1      cgd 			break;
    283  1.1      cgd 		case USER_TYPE:
    284  1.1      cgd 			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
    285  1.1      cgd 				return(YES);
    286  1.1      cgd 			break;
    287  1.1      cgd 	}
    288  1.1      cgd 	return(NO);
    289  1.1      cgd }
    290  1.1      cgd 
    291  1.1      cgd /*
    292  1.1      cgd  * addarg --
    293  1.1      cgd  *	add an entry to a linked list of arguments
    294  1.1      cgd  */
    295  1.1      cgd addarg(type, arg)
    296  1.1      cgd 	int type;
    297  1.1      cgd 	char *arg;
    298  1.1      cgd {
    299  1.1      cgd 	register ARG *cur;
    300  1.1      cgd 	char *malloc();
    301  1.1      cgd 
    302  1.1      cgd 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) {
    303  1.1      cgd 		fputs("last: malloc failure.\n", stderr);
    304  1.1      cgd 		exit(1);
    305  1.1      cgd 	}
    306  1.1      cgd 	cur->next = arglist;
    307  1.1      cgd 	cur->type = type;
    308  1.1      cgd 	cur->name = arg;
    309  1.1      cgd 	arglist = cur;
    310  1.1      cgd }
    311  1.1      cgd 
    312  1.1      cgd /*
    313  1.1      cgd  * addtty --
    314  1.1      cgd  *	add an entry to a linked list of ttys
    315  1.1      cgd  */
    316  1.1      cgd TTY *
    317  1.1      cgd addtty(ttyname)
    318  1.1      cgd 	char *ttyname;
    319  1.1      cgd {
    320  1.1      cgd 	register TTY *cur;
    321  1.1      cgd 	char *malloc();
    322  1.1      cgd 
    323  1.1      cgd 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY)))) {
    324  1.1      cgd 		fputs("last: malloc failure.\n", stderr);
    325  1.1      cgd 		exit(1);
    326  1.1      cgd 	}
    327  1.1      cgd 	cur->next = ttylist;
    328  1.1      cgd 	cur->logout = currentout;
    329  1.1      cgd 	bcopy(ttyname, cur->tty, UT_LINESIZE);
    330  1.1      cgd 	return(ttylist = cur);
    331  1.1      cgd }
    332  1.1      cgd 
    333  1.1      cgd /*
    334  1.1      cgd  * hostconv --
    335  1.1      cgd  *	convert the hostname to search pattern; if the supplied host name
    336  1.1      cgd  *	has a domain attached that is the same as the current domain, rip
    337  1.1      cgd  *	off the domain suffix since that's what login(1) does.
    338  1.1      cgd  */
    339  1.1      cgd hostconv(arg)
    340  1.1      cgd 	char *arg;
    341  1.1      cgd {
    342  1.1      cgd 	static int first = 1;
    343  1.1      cgd 	static char *hostdot, name[MAXHOSTNAMELEN];
    344  1.1      cgd 	char *argdot, *index();
    345  1.1      cgd 
    346  1.1      cgd 	if (!(argdot = index(arg, '.')))
    347  1.1      cgd 		return;
    348  1.1      cgd 	if (first) {
    349  1.1      cgd 		first = 0;
    350  1.1      cgd 		if (gethostname(name, sizeof(name))) {
    351  1.1      cgd 			perror("last: gethostname");
    352  1.1      cgd 			exit(1);
    353  1.1      cgd 		}
    354  1.1      cgd 		hostdot = index(name, '.');
    355  1.1      cgd 	}
    356  1.1      cgd 	if (hostdot && !strcasecmp(hostdot, argdot))
    357  1.1      cgd 		*argdot = '\0';
    358  1.1      cgd }
    359  1.1      cgd 
    360  1.1      cgd /*
    361  1.1      cgd  * ttyconv --
    362  1.1      cgd  *	convert tty to correct name.
    363  1.1      cgd  */
    364  1.1      cgd char *
    365  1.1      cgd ttyconv(arg)
    366  1.1      cgd 	char *arg;
    367  1.1      cgd {
    368  1.1      cgd 	char *mval, *malloc(), *strcpy();
    369  1.1      cgd 
    370  1.1      cgd 	/*
    371  1.1      cgd 	 * kludge -- we assume that all tty's end with
    372  1.1      cgd 	 * a two character suffix.
    373  1.1      cgd 	 */
    374  1.1      cgd 	if (strlen(arg) == 2) {
    375  1.1      cgd 		/* either 6 for "ttyxx" or 8 for "console" */
    376  1.1      cgd 		if (!(mval = malloc((u_int)8))) {
    377  1.1      cgd 			fputs("last: malloc failure.\n", stderr);
    378  1.1      cgd 			exit(1);
    379  1.1      cgd 		}
    380  1.1      cgd 		if (!strcmp(arg, "co"))
    381  1.1      cgd 			(void)strcpy(mval, "console");
    382  1.1      cgd 		else {
    383  1.1      cgd 			(void)strcpy(mval, "tty");
    384  1.1      cgd 			(void)strcpy(mval + 3, arg);
    385  1.1      cgd 		}
    386  1.1      cgd 		return(mval);
    387  1.1      cgd 	}
    388  1.1      cgd 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
    389  1.1      cgd 		return(arg + 5);
    390  1.1      cgd 	return(arg);
    391  1.1      cgd }
    392  1.1      cgd 
    393  1.1      cgd /*
    394  1.1      cgd  * onintr --
    395  1.1      cgd  *	on interrupt, we inform the user how far we've gotten
    396  1.1      cgd  */
    397  1.1      cgd void
    398  1.1      cgd onintr(signo)
    399  1.1      cgd 	int signo;
    400  1.1      cgd {
    401  1.1      cgd 	char *ct, *ctime();
    402  1.1      cgd 
    403  1.1      cgd 	ct = ctime(&buf[0].ut_time);
    404  1.1      cgd 	printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
    405  1.1      cgd 	if (signo == SIGINT)
    406  1.1      cgd 		exit(1);
    407  1.1      cgd 	(void)fflush(stdout);			/* fix required for rsh */
    408  1.1      cgd }
    409