Home | History | Annotate | Line # | Download | only in lastlogin
lastlogin.c revision 1.2
      1 /*	$NetBSD: lastlogin.c,v 1.2 1997/03/27 17:25:49 christos Exp $	*/
      2 /*
      3  * Copyright (c) 1996 John M. Vinopal
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed for the NetBSD Project
     17  *	by John M. Vinopal.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <utmp.h>
     36 #include <errno.h>
     37 #include <err.h>
     38 #include <pwd.h>
     39 #include <time.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 
     43 extern	char *__progname;
     44 static	char *logfile = _PATH_LASTLOG;
     45 
     46 static void output __P((struct passwd *, struct lastlog *));
     47 static void usage __P((void));
     48 
     49 int
     50 main(argc, argv)
     51 	int argc;
     52 	char *argv[];
     53 {
     54 	int	ch, i;
     55 	FILE	*fp;
     56 	struct passwd	*passwd;
     57 	struct lastlog	last;
     58 
     59 	while ((ch = getopt(argc, argv, "")) != -1) {
     60 		usage();
     61 	}
     62 
     63 	fp = fopen(logfile, "r");
     64 	if (fp == NULL)
     65 		err(1, "%s", logfile);
     66 
     67 	setpassent(1);	/* Keep passwd file pointers open */
     68 
     69 	/* Process usernames given on the command line. */
     70 	if (argc > 1) {
     71 		long offset;
     72 		for (i = 1; i < argc; ++i) {
     73 			if ((passwd = getpwnam(argv[i])) == NULL) {
     74 				warnx("user '%s' not found", argv[i]);
     75 				continue;
     76 			}
     77 			/* Calculate the offset into the lastlog file. */
     78 			offset = (long)(passwd->pw_uid * sizeof(last));
     79 			if (fseek(fp, offset, SEEK_SET)) {
     80 				warn("fseek error");
     81 				continue;
     82 			}
     83 			if (fread(&last, sizeof(last), 1, fp) != 1) {
     84 				warnx("fread error on '%s'", passwd->pw_name);
     85 				clearerr(fp);
     86 				continue;
     87 			}
     88 			output(passwd, &last);
     89 		}
     90 	}
     91 	/* Read all lastlog entries, looking for active ones */
     92 	else {
     93 		for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) {
     94 			if (last.ll_time == 0)
     95 				continue;
     96 			if ((passwd = getpwuid(i)) != NULL)
     97 				output(passwd, &last);
     98 		}
     99 		if (ferror(fp))
    100 			warnx("fread error");
    101 	}
    102 
    103 	setpassent(0);	/* Close passwd file pointers */
    104 
    105 	fclose(fp);
    106 	exit(0);
    107 }
    108 
    109 /* Duplicate the output of last(1) */
    110 static void
    111 output(p, l)
    112 	struct passwd *p;
    113 	struct lastlog *l;
    114 {
    115 	printf("%-*.*s  %-*.*s %-*.*s   %s",
    116 		UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
    117 		UT_LINESIZE, UT_LINESIZE, l->ll_line,
    118 		UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
    119 		(l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
    120 }
    121 
    122 static void
    123 usage()
    124 {
    125 	fprintf(stderr, "usage: %s [user ...]\n", __progname);
    126 	exit(1);
    127 }
    128