lastlogin.c revision 202205
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 202205 2010-01-13 18:17:12Z 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>
4468723Sru#include <unistd.h>
45202205Sed#include <utmpx.h>
4668723Sru
4799800Salfred	int	main(int, char **);
48202205Sedstatic	void	output(struct utmpx *);
4999800Salfredstatic	void	usage(void);
5068723Sru
5168723Sruint
52201227Sedmain(int argc, char *argv[])
5368723Sru{
5468723Sru	int	ch, i;
55202205Sed	struct utmpx *u;
5668723Sru
5768723Sru	while ((ch = getopt(argc, argv, "")) != -1) {
5868723Sru		usage();
5968723Sru	}
6068723Sru
6168723Sru	setpassent(1);	/* Keep passwd file pointers open */
6268723Sru
6368723Sru	/* Process usernames given on the command line. */
6468723Sru	if (argc > 1) {
6568723Sru		for (i = 1; i < argc; ++i) {
66202205Sed			if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
67202205Sed				errx(1, "failed to open lastlog database");
68202205Sed			if ((u = getutxuser(argv[i])) == NULL) {
6968723Sru				warnx("user '%s' not found", argv[i]);
7068723Sru				continue;
7168723Sru			}
72200303Sed			output(u);
73202205Sed			endutxent();
7468723Sru		}
7568723Sru	}
7668723Sru	/* Read all lastlog entries, looking for active ones */
7768723Sru	else {
78202205Sed		if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
79202205Sed			errx(1, "failed to open lastlog database");
80202205Sed		while ((u = getutxent()) != NULL) {
81200303Sed			if (u->ut_type != USER_PROCESS)
8268723Sru				continue;
83200303Sed			output(u);
8468723Sru		}
85202205Sed		endutxent();
8668723Sru	}
8768723Sru
8868723Sru	setpassent(0);	/* Close passwd file pointers */
8968723Sru	exit(0);
9068723Sru}
9168723Sru
9268723Sru/* Duplicate the output of last(1) */
9368723Srustatic void
94202205Sedoutput(struct utmpx *u)
9568723Sru{
96200303Sed	time_t t = u->ut_tv.tv_sec;
97200303Sed
98202205Sed	printf("%-10s %-8s %-22s %s",
99202205Sed		u->ut_user, u->ut_line, u->ut_host, ctime(&t));
10068723Sru}
10168723Sru
10268723Srustatic void
103201227Sedusage(void)
10468723Sru{
10569828Scharnier	fprintf(stderr, "usage: lastlogin [user ...]\n");
10668723Sru	exit(1);
10768723Sru}
108