ubase/who.c

65 lines
1.2 KiB
C
Raw Normal View History

2014-02-14 10:03:48 -05:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2014-06-30 14:03:41 -04:00
#include <unistd.h>
2014-02-14 10:03:48 -05:00
#include <utmp.h>
2014-06-30 14:03:41 -04:00
2014-07-01 10:48:35 -04:00
#include "config.h"
2014-02-14 10:03:48 -05:00
#include "util.h"
static void
usage(void)
{
eprintf("usage: who [-ml]\n");
}
int
2014-04-18 06:49:10 -04:00
main(int argc, char *argv[])
2014-02-14 10:03:48 -05: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;
tty = ttyname(STDIN_FILENO);
if (!tty)
eprintf("ttyname: stdin:");
2014-02-14 10:03:48 -05:00
if ((ttmp = strrchr(tty, '/')))
tty = ttmp+1;
break;
case 'l':
lflag = 1;
break;
default:
usage();
} ARGEND;
if (argc > 0)
usage();
2014-07-01 10:48:35 -04:00
if (!(ufp = fopen(UTMP_PATH, "r")))
eprintf("fopen: %s:", UTMP_PATH);
2014-02-14 10:03:48 -05:00
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
if (!*usr.ut_name || !*usr.ut_line ||
usr.ut_line[0] == '~')
continue;
if (mflag != 0 && strcmp(usr.ut_line, tty) != 0)
2014-02-14 10:03:48 -05:00
continue;
if (!!strcmp(usr.ut_name, "LOGIN") == lflag)
2014-02-14 10:03:48 -05: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);
return EXIT_SUCCESS;
}