From 422f400c2182ef0cdcdf0e237aac2bb9a5a9dd03 Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 14 Feb 2014 15:03:48 +0000 Subject: [PATCH] Import who from sbase --- Makefile | 3 ++- who.1 | 27 ++++++++++++++++++++++++ who.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 who.1 create mode 100644 who.c diff --git a/Makefile b/Makefile index 0cf14d2..f7c3cf8 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,8 @@ SRC = \ umount.c \ unshare.c \ uptime.c \ - watch.c + watch.c \ + who.c MAN1 = \ chvt.1 \ diff --git a/who.1 b/who.1 new file mode 100644 index 0000000..e266ddb --- /dev/null +++ b/who.1 @@ -0,0 +1,27 @@ +.TH WHO 1 sbase\-VERSION +.SH NAME +who \- print who has logged on +.SH SYNOPSIS +.B who +.RB [ -m ] + +.SH DESCRIPTION +.B who +prints a list of who has logged on, their controlling tty, and the +time at which they logged on. +.SH OPTIONS +.TP +.B \-m +only show users on current tty +.TP +.B \-l +also print LOGIN processes + +.SH BUGS +.B who +relies on the utmp file to be updated responsibly. This +doesn't always happen, which can cause who to print completely +bogus data. + +.SH SEE ALSO +.IR utmp (5) diff --git a/who.c b/who.c new file mode 100644 index 0000000..f05cf54 --- /dev/null +++ b/who.c @@ -0,0 +1,63 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: who [-ml]\n"); +} + +int +main(int argc, char **argv) +{ + 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("who: stdin:"); + if ((ttmp = strrchr(tty, '/'))) + tty = ttmp+1; + break; + case 'l': + lflag = 1; + break; + default: + usage(); + } ARGEND; + + if (argc > 0) + usage(); + + if (!(ufp = fopen("/var/run/utmp", "r"))) + eprintf("who: '%s':", "/var/run/utmp"); + + while(fread(&usr, sizeof(usr), 1, ufp) == 1) { + if (!*usr.ut_name || !*usr.ut_line || + usr.ut_line[0] == '~') + continue; + if (mflag && strcmp(usr.ut_line, tty)) + continue; + if (strcmp(usr.ut_name, "LOGIN") == lflag) + 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; +} +