2014-10-16 10:27:39 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
2014-10-17 15:07:49 +00:00
|
|
|
#include <libgen.h>
|
2014-10-16 10:27:39 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-10-16 10:40:57 +00:00
|
|
|
static void
|
2014-10-16 10:27:39 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2014-10-16 10:40:57 +00:00
|
|
|
eprintf("usage: %s [user]\n", argv0);
|
2014-10-16 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct utmp ut;
|
|
|
|
char *user, *file, *prog;
|
|
|
|
time_t t;
|
|
|
|
|
2014-10-16 10:40:57 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-10-16 10:27:39 +00:00
|
|
|
switch (argc) {
|
2014-10-16 10:40:57 +00:00
|
|
|
case 0:
|
2014-10-16 10:27:39 +00:00
|
|
|
user = NULL;
|
|
|
|
break;
|
2014-10-16 10:40:57 +00:00
|
|
|
case 1:
|
|
|
|
user = argv[0];
|
2014-10-16 10:27:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2014-10-16 10:40:57 +00:00
|
|
|
prog = basename(argv0);
|
2014-10-16 10:27:39 +00:00
|
|
|
file = (!strcmp(prog, "last")) ? WTMP_PATH : BTMP_PATH;
|
|
|
|
if ((fp = fopen(file, "r")) == NULL)
|
2015-02-20 15:08:55 +00:00
|
|
|
eprintf("fopen %s:", file);
|
2014-10-16 10:27:39 +00:00
|
|
|
|
|
|
|
while (fread(&ut, sizeof(ut), 1, fp) == 1) {
|
|
|
|
if (ut.ut_type != USER_PROCESS ||
|
|
|
|
(user && strcmp(user, ut.ut_name))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = ut.ut_time;
|
|
|
|
printf("%-8.8s %-8.8s %-16.16s %s",
|
|
|
|
ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t));
|
|
|
|
}
|
|
|
|
if (fclose(fp))
|
|
|
|
eprintf("fclose %s:", file);
|
|
|
|
return 0;
|
|
|
|
}
|