lastlogin.c revision 200303
168723Sru/* 268723Sru * Copyright (c) 1996 John M. Vinopal 368723Sru * All rights reserved. 468723Sru * 568723Sru * Redistribution and use in source and binary forms, with or without 668723Sru * modification, are permitted provided that the following conditions 768723Sru * are met: 868723Sru * 1. Redistributions of source code must retain the above copyright 968723Sru * notice, this list of conditions and the following disclaimer. 1068723Sru * 2. Redistributions in binary form must reproduce the above copyright 1168723Sru * notice, this list of conditions and the following disclaimer in the 1268723Sru * documentation and/or other materials provided with the distribution. 1368723Sru * 3. All advertising materials mentioning features or use of this software 1468723Sru * must display the following acknowledgement: 1568723Sru * This product includes software developed for the NetBSD Project 1668723Sru * by John M. Vinopal. 1768723Sru * 4. The name of the author may not be used to endorse or promote products 1868723Sru * derived from this software without specific prior written permission. 1968723Sru * 2068723Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2168723Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2268723Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2368723Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2468723Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2568723Sru * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2668723Sru * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2768723Sru * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2868723Sru * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2968723Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3068723Sru * SUCH DAMAGE. 3168723Sru */ 3268723Sru 3368723Sru#include <sys/cdefs.h> 3468723Sru#ifndef lint 3568723Sru__RCSID("$FreeBSD: head/usr.sbin/lastlogin/lastlogin.c 200303 2009-12-09 20:05:37Z ed $"); 3668723Sru__RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $"); 3768723Sru#endif 3868723Sru 3968723Sru#include <err.h> 4068723Sru#include <pwd.h> 4168723Sru#include <stdio.h> 4278719Sdd#include <stdlib.h> 4368723Sru#include <time.h> 44200303Sed#include <ulog.h> 4568723Sru#include <unistd.h> 4668723Sru 4799800Salfred int main(int, char **); 48200303Sedstatic void output(struct ulog_utmpx *); 4999800Salfredstatic void usage(void); 5068723Sru 5168723Sruint 5268723Srumain(argc, argv) 5368723Sru int argc; 5468723Sru char *argv[]; 5568723Sru{ 5668723Sru int ch, i; 57200303Sed struct ulog_utmpx *u; 5868723Sru 5968723Sru while ((ch = getopt(argc, argv, "")) != -1) { 6068723Sru usage(); 6168723Sru } 6268723Sru 63200303Sed if (ulog_setutxfile(UTXF_LASTLOG, NULL) != 0) 64200303Sed errx(1, "failed to open lastlog database"); 6568723Sru 6668723Sru setpassent(1); /* Keep passwd file pointers open */ 6768723Sru 6868723Sru /* Process usernames given on the command line. */ 6968723Sru if (argc > 1) { 7068723Sru for (i = 1; i < argc; ++i) { 71200303Sed if ((u = ulog_getutxuser(argv[i])) == NULL) { 7268723Sru warnx("user '%s' not found", argv[i]); 7368723Sru continue; 7468723Sru } 75200303Sed output(u); 7668723Sru } 7768723Sru } 7868723Sru /* Read all lastlog entries, looking for active ones */ 7968723Sru else { 80200303Sed while ((u = ulog_getutxent()) != NULL) { 81200303Sed if (u->ut_type != USER_PROCESS) 8268723Sru continue; 83200303Sed output(u); 8468723Sru } 8568723Sru } 8668723Sru 8768723Sru setpassent(0); /* Close passwd file pointers */ 8868723Sru 89200303Sed ulog_endutxent(); 9068723Sru exit(0); 9168723Sru} 9268723Sru 9368723Sru/* Duplicate the output of last(1) */ 9468723Srustatic void 95200303Sedoutput(struct ulog_utmpx *u) 9668723Sru{ 97200303Sed time_t t = u->ut_tv.tv_sec; 98200303Sed 99200303Sed printf("%-16s %-8s %-16s %s", 100200303Sed u->ut_user, u->ut_line, u->ut_host, 101200303Sed (u->ut_type == USER_PROCESS) ? ctime(&t) : "Never logged in\n"); 10268723Sru} 10368723Sru 10468723Srustatic void 10568723Sruusage() 10668723Sru{ 10769828Scharnier fprintf(stderr, "usage: lastlogin [user ...]\n"); 10868723Sru exit(1); 10968723Sru} 110