commit d59c8eea362026586d4faadb902a5b6f400981fe
parent 9e5718282a48e9d1b07b8d53e13e61670b5d86c6
Author: sin <sin@2f30.org>
Date: Mon, 5 Aug 2013 15:59:49 +0100
Implement -m for who
Diffstat:
M | who.c | | | 18 | +++++++++++++++--- |
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/who.c b/who.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <utmp.h>
@@ -15,8 +16,17 @@ main(int argc, char **argv)
FILE *ufp;
time_t t;
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
+ bool mflag = false;
- if(argc!=1)
+ ARGBEGIN {
+ case 'm':
+ mflag = true;
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc > 0)
usage();
if (!(ufp = fopen(_PATH_UTMP, "r"))) {
@@ -25,6 +35,9 @@ main(int argc, char **argv)
while(fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
if (!*usr.ut_name || !*usr.ut_line)
continue;
+ 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);
@@ -36,6 +49,5 @@ main(int argc, char **argv)
void
usage(void)
{
- eprintf("usage: who\n");
+ eprintf("usage: who [-m]\n");
}
-