2013-10-07 16:03:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2013-06-14 16:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2013-07-18 19:28:08 +00:00
|
|
|
#include <utmp.h>
|
2013-06-14 16:55:25 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-08-04 23:56:34 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: who [-ml]\n");
|
|
|
|
}
|
2013-06-14 16:55:25 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2013-07-18 19:28:08 +00:00
|
|
|
struct utmp usr;
|
|
|
|
FILE *ufp;
|
2013-06-14 16:55:25 +00:00
|
|
|
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
|
2013-08-04 23:56:34 +00:00
|
|
|
char *tty, *ttmp;
|
|
|
|
int mflag = 0, lflag = 0;
|
|
|
|
time_t t;
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2013-08-05 14:59:49 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'm':
|
2013-08-04 23:56:34 +00:00
|
|
|
mflag = 1;
|
|
|
|
tty = ttyname(STDIN_FILENO);
|
|
|
|
if (!tty)
|
|
|
|
eprintf("who: stdin:");
|
|
|
|
if ((ttmp = strrchr(tty, '/')))
|
|
|
|
tty = ttmp+1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
2013-08-05 14:59:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc > 0)
|
2013-06-14 16:55:25 +00:00
|
|
|
usage();
|
|
|
|
|
2013-09-29 18:26:08 +00:00
|
|
|
if (!(ufp = fopen("/var/run/utmp", "r")))
|
|
|
|
eprintf("who: '%s':", "/var/run/utmp");
|
2013-08-04 23:56:34 +00:00
|
|
|
|
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] == '~')
|
2013-06-14 16:55:25 +00:00
|
|
|
continue;
|
2013-08-04 23:56:34 +00:00
|
|
|
if (mflag && strcmp(usr.ut_line, tty))
|
|
|
|
continue;
|
|
|
|
if (strcmp(usr.ut_name, "LOGIN") == lflag)
|
2013-08-05 14:59:49 +00:00
|
|
|
continue;
|
2013-07-18 19:28:08 +00:00
|
|
|
t = usr.ut_time;
|
2013-06-14 16:55:25 +00:00
|
|
|
strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
|
2013-07-18 19:28:08 +00:00
|
|
|
printf("%-8s %-12s %-16s\n", usr.ut_name, usr.ut_line, timebuf);
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
2013-07-18 19:28:08 +00:00
|
|
|
fclose(ufp);
|
2013-10-07 15:41:55 +00:00
|
|
|
return EXIT_SUCCESS;
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
|
|
|
|