ubase/who.c

65 lines
1.2 KiB
C
Raw Normal View History

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)
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;
if (mflag != 0 && strcmp(usr.ut_line, tty) != 0)
2014-02-14 15:03:48 +00:00
continue;
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
}