2014-02-14 15:03:48 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <unistd.h>
|
2014-02-14 15:03:48 +00:00
|
|
|
#include <utmp.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2014-07-01 14:48:35 +00:00
|
|
|
#include "config.h"
|
2014-02-14 15:03:48 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-02 18:58:00 +00:00
|
|
|
eprintf("usage: %s [-ml]\n", argv0);
|
2014-02-14 15:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-04-18 10:49:10 +00:00
|
|
|
main(int argc, char *argv[])
|
2014-02-14 15:03:48 +00:00
|
|
|
{
|
|
|
|
struct utmp usr;
|
|
|
|
FILE *ufp;
|
|
|
|
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
|
|
|
|
char *tty, *ttmp;
|
|
|
|
int mflag = 0, lflag = 0;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
2014-11-30 13:12:15 +00:00
|
|
|
tty = ttyname(0);
|
2014-02-14 15:03:48 +00:00
|
|
|
if (!tty)
|
2014-04-11 22:45:49 +00:00
|
|
|
eprintf("ttyname: stdin:");
|
2014-02-14 15:03:48 +00:00
|
|
|
if ((ttmp = strrchr(tty, '/')))
|
|
|
|
tty = ttmp+1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
usage();
|
|
|
|
|
2014-07-01 14:48:35 +00:00
|
|
|
if (!(ufp = fopen(UTMP_PATH, "r")))
|
|
|
|
eprintf("fopen: %s:", UTMP_PATH);
|
2014-02-14 15:03:48 +00:00
|
|
|
|
2014-07-09 15:39:32 +00:00
|
|
|
while (fread(&usr, sizeof(usr), 1, ufp) == 1) {
|
2014-02-14 15:03:48 +00:00
|
|
|
if (!*usr.ut_name || !*usr.ut_line ||
|
|
|
|
usr.ut_line[0] == '~')
|
|
|
|
continue;
|
2014-03-25 12:38:47 +00:00
|
|
|
if (mflag != 0 && strcmp(usr.ut_line, tty) != 0)
|
2014-02-14 15:03:48 +00:00
|
|
|
continue;
|
2014-03-25 12:38:47 +00:00
|
|
|
if (!!strcmp(usr.ut_name, "LOGIN") == lflag)
|
2014-02-14 15:03:48 +00:00
|
|
|
continue;
|
|
|
|
t = usr.ut_time;
|
|
|
|
strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
|
|
|
|
printf("%-8s %-12s %-16s\n", usr.ut_name, usr.ut_line, timebuf);
|
|
|
|
}
|
|
|
|
fclose(ufp);
|
2014-10-02 22:45:25 +00:00
|
|
|
return 0;
|
2014-02-14 15:03:48 +00:00
|
|
|
}
|