Home | History | Annotate | Line # | Download | only in lastlogin
lastlogin.c revision 1.2
      1  1.2  christos /*	$NetBSD: lastlogin.c,v 1.2 1997/03/27 17:25:49 christos 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.1      phil #include <sys/types.h>
     35  1.1      phil #include <utmp.h>
     36  1.1      phil #include <errno.h>
     37  1.1      phil #include <err.h>
     38  1.1      phil #include <pwd.h>
     39  1.1      phil #include <time.h>
     40  1.1      phil #include <stdio.h>
     41  1.1      phil #include <stdlib.h>
     42  1.1      phil 
     43  1.1      phil extern	char *__progname;
     44  1.1      phil static	char *logfile = _PATH_LASTLOG;
     45  1.1      phil 
     46  1.1      phil static void output __P((struct passwd *, struct lastlog *));
     47  1.1      phil static void usage __P((void));
     48  1.1      phil 
     49  1.1      phil int
     50  1.1      phil main(argc, argv)
     51  1.1      phil 	int argc;
     52  1.1      phil 	char *argv[];
     53  1.1      phil {
     54  1.1      phil 	int	ch, i;
     55  1.1      phil 	FILE	*fp;
     56  1.1      phil 	struct passwd	*passwd;
     57  1.1      phil 	struct lastlog	last;
     58  1.1      phil 
     59  1.1      phil 	while ((ch = getopt(argc, argv, "")) != -1) {
     60  1.1      phil 		usage();
     61  1.1      phil 	}
     62  1.1      phil 
     63  1.1      phil 	fp = fopen(logfile, "r");
     64  1.1      phil 	if (fp == NULL)
     65  1.1      phil 		err(1, "%s", logfile);
     66  1.1      phil 
     67  1.1      phil 	setpassent(1);	/* Keep passwd file pointers open */
     68  1.1      phil 
     69  1.1      phil 	/* Process usernames given on the command line. */
     70  1.1      phil 	if (argc > 1) {
     71  1.1      phil 		long offset;
     72  1.1      phil 		for (i = 1; i < argc; ++i) {
     73  1.1      phil 			if ((passwd = getpwnam(argv[i])) == NULL) {
     74  1.1      phil 				warnx("user '%s' not found", argv[i]);
     75  1.1      phil 				continue;
     76  1.1      phil 			}
     77  1.1      phil 			/* Calculate the offset into the lastlog file. */
     78  1.1      phil 			offset = (long)(passwd->pw_uid * sizeof(last));
     79  1.1      phil 			if (fseek(fp, offset, SEEK_SET)) {
     80  1.1      phil 				warn("fseek error");
     81  1.1      phil 				continue;
     82  1.1      phil 			}
     83  1.1      phil 			if (fread(&last, sizeof(last), 1, fp) != 1) {
     84  1.1      phil 				warnx("fread error on '%s'", passwd->pw_name);
     85  1.1      phil 				clearerr(fp);
     86  1.1      phil 				continue;
     87  1.1      phil 			}
     88  1.1      phil 			output(passwd, &last);
     89  1.1      phil 		}
     90  1.1      phil 	}
     91  1.1      phil 	/* Read all lastlog entries, looking for active ones */
     92  1.1      phil 	else {
     93  1.2  christos 		for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) {
     94  1.2  christos 			if (last.ll_time == 0)
     95  1.2  christos 				continue;
     96  1.2  christos 			if ((passwd = getpwuid(i)) != NULL)
     97  1.1      phil 				output(passwd, &last);
     98  1.1      phil 		}
     99  1.1      phil 		if (ferror(fp))
    100  1.1      phil 			warnx("fread error");
    101  1.1      phil 	}
    102  1.1      phil 
    103  1.1      phil 	setpassent(0);	/* Close passwd file pointers */
    104  1.1      phil 
    105  1.1      phil 	fclose(fp);
    106  1.1      phil 	exit(0);
    107  1.1      phil }
    108  1.1      phil 
    109  1.1      phil /* Duplicate the output of last(1) */
    110  1.1      phil static void
    111  1.1      phil output(p, l)
    112  1.1      phil 	struct passwd *p;
    113  1.1      phil 	struct lastlog *l;
    114  1.1      phil {
    115  1.1      phil 	printf("%-*.*s  %-*.*s %-*.*s   %s",
    116  1.1      phil 		UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
    117  1.1      phil 		UT_LINESIZE, UT_LINESIZE, l->ll_line,
    118  1.1      phil 		UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
    119  1.1      phil 		(l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
    120  1.1      phil }
    121  1.1      phil 
    122  1.1      phil static void
    123  1.1      phil usage()
    124  1.1      phil {
    125  1.1      phil 	fprintf(stderr, "usage: %s [user ...]\n", __progname);
    126  1.1      phil 	exit(1);
    127  1.1      phil }
    128