lastlogin.c revision 205365
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 205365 2010-03-20 09:29:25Z 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 <stdio.h> 4178719Sdd#include <stdlib.h> 4268723Sru#include <time.h> 4368723Sru#include <unistd.h> 44202205Sed#include <utmpx.h> 4568723Sru 4699800Salfred int main(int, char **); 47202205Sedstatic void output(struct utmpx *); 4899800Salfredstatic void usage(void); 4968723Sru 5068723Sruint 51201227Sedmain(int argc, char *argv[]) 5268723Sru{ 5368723Sru int ch, i; 54202205Sed struct utmpx *u; 5568723Sru 5668723Sru while ((ch = getopt(argc, argv, "")) != -1) { 5768723Sru usage(); 5868723Sru } 5968723Sru 6068723Sru /* Process usernames given on the command line. */ 6168723Sru if (argc > 1) { 6268723Sru for (i = 1; i < argc; ++i) { 63202205Sed if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) 64202205Sed errx(1, "failed to open lastlog database"); 65202205Sed if ((u = getutxuser(argv[i])) == NULL) { 6668723Sru warnx("user '%s' not found", argv[i]); 6768723Sru continue; 6868723Sru } 69200303Sed output(u); 70202205Sed endutxent(); 7168723Sru } 7268723Sru } 7368723Sru /* Read all lastlog entries, looking for active ones */ 7468723Sru else { 75202205Sed if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) 76202205Sed errx(1, "failed to open lastlog database"); 77202205Sed while ((u = getutxent()) != NULL) { 78200303Sed if (u->ut_type != USER_PROCESS) 7968723Sru continue; 80200303Sed output(u); 8168723Sru } 82202205Sed endutxent(); 8368723Sru } 8468723Sru 8568723Sru exit(0); 8668723Sru} 8768723Sru 8868723Sru/* Duplicate the output of last(1) */ 8968723Srustatic void 90202205Sedoutput(struct utmpx *u) 9168723Sru{ 92200303Sed time_t t = u->ut_tv.tv_sec; 93200303Sed 94205365Sed printf("%-10s %-8s %-22.22s %s", 95202205Sed u->ut_user, u->ut_line, u->ut_host, ctime(&t)); 9668723Sru} 9768723Sru 9868723Srustatic void 99201227Sedusage(void) 10068723Sru{ 10169828Scharnier fprintf(stderr, "usage: lastlogin [user ...]\n"); 10268723Sru exit(1); 10368723Sru} 104