Home | History | Annotate | Line # | Download | only in lastlogin
lastlogin.c revision 1.1.1.1
      1 /*	$NetBSD: lastlogin.c,v 1.1.1.1 1997/02/11 18:36:51 phil 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 		i = 0;
     94 		while (fread(&last, sizeof(last), 1, fp) == 1) {
     95 			passwd = getpwuid(i);
     96 			if (passwd != NULL && (last.ll_time != 0))
     97 				output(passwd, &last);
     98 			i++;
     99 		}
    100 		if (ferror(fp))
    101 			warnx("fread error");
    102 	}
    103 
    104 	setpassent(0);	/* Close passwd file pointers */
    105 
    106 	fclose(fp);
    107 	exit(0);
    108 }
    109 
    110 /* Duplicate the output of last(1) */
    111 static void
    112 output(p, l)
    113 	struct passwd *p;
    114 	struct lastlog *l;
    115 {
    116 	printf("%-*.*s  %-*.*s %-*.*s   %s",
    117 		UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
    118 		UT_LINESIZE, UT_LINESIZE, l->ll_line,
    119 		UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
    120 		(l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
    121 }
    122 
    123 static void
    124 usage()
    125 {
    126 	fprintf(stderr, "usage: %s [user ...]\n", __progname);
    127 	exit(1);
    128 }
    129