2013-06-14 12:55:25 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-05 10:59:49 -04:00
|
|
|
#include <stdbool.h>
|
2013-06-14 12:55:25 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2013-07-18 15:28:08 -04:00
|
|
|
#include <utmp.h>
|
2013-06-14 12:55:25 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2013-07-18 15:28:08 -04:00
|
|
|
struct utmp usr;
|
|
|
|
FILE *ufp;
|
2013-06-14 12:55:25 -04:00
|
|
|
time_t t;
|
|
|
|
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
|
2013-08-05 10:59:49 -04:00
|
|
|
bool mflag = false;
|
2013-06-14 12:55:25 -04:00
|
|
|
|
2013-08-05 10:59:49 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'm':
|
|
|
|
mflag = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc > 0)
|
2013-06-14 12:55:25 -04:00
|
|
|
usage();
|
|
|
|
|
2013-07-18 15:28:08 -04:00
|
|
|
if (!(ufp = fopen(_PATH_UTMP, "r"))) {
|
|
|
|
eprintf("fopen:");
|
|
|
|
}
|
2013-08-05 11:04:49 -04:00
|
|
|
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
|
2013-08-05 11:12:01 -04:00
|
|
|
if (!*usr.ut_name || !*usr.ut_line ||
|
|
|
|
usr.ut_line[0] == '~')
|
2013-06-14 12:55:25 -04:00
|
|
|
continue;
|
2013-08-05 10:59:49 -04:00
|
|
|
if (mflag && strcmp(usr.ut_line,
|
|
|
|
strrchr(ttyname(STDIN_FILENO), '/') + 1))
|
|
|
|
continue;
|
2013-07-18 15:28:08 -04:00
|
|
|
t = usr.ut_time;
|
2013-06-14 12:55:25 -04:00
|
|
|
strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
|
2013-07-18 15:28:08 -04:00
|
|
|
printf("%-8s %-12s %-16s\n", usr.ut_name, usr.ut_line, timebuf);
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|
2013-07-18 15:28:08 -04:00
|
|
|
fclose(ufp);
|
2013-06-14 12:55:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:27:42 -04:00
|
|
|
void
|
2013-06-14 12:55:25 -04:00
|
|
|
usage(void)
|
|
|
|
{
|
2013-08-05 10:59:49 -04:00
|
|
|
eprintf("usage: who [-m]\n");
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|