sbase/who.c

55 lines
959 B
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-08-05 14:59:49 +00:00
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <utmp.h>
#include "util.h"
static void usage(void);
int
main(int argc, char **argv)
{
struct utmp usr;
FILE *ufp;
time_t t;
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
2013-08-05 14:59:49 +00:00
bool mflag = false;
2013-08-05 14:59:49 +00:00
ARGBEGIN {
case 'm':
mflag = true;
break;
default:
usage();
} ARGEND;
if (argc > 0)
usage();
if (!(ufp = fopen(_PATH_UTMP, "r"))) {
eprintf("fopen:");
}
2013-08-05 15:04:49 +00:00
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
2013-08-05 15:12:01 +00:00
if (!*usr.ut_name || !*usr.ut_line ||
usr.ut_line[0] == '~')
continue;
2013-08-05 14:59:49 +00:00
if (mflag && strcmp(usr.ut_line,
strrchr(ttyname(STDIN_FILENO), '/') + 1))
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);
return 0;
}
void
usage(void)
{
2013-08-05 14:59:49 +00:00
eprintf("usage: who [-m]\n");
}