[hackers] [sbase] Cleaning up who, adding -l and manpage || David Galos
commit fd262561a946c74885cd8b3fa42bd4a41f1c0d82
Author: David Galos <galosd83_AT_students.rowan.edu>
Date: Sun Aug 4 19:56:34 2013 -0400
Cleaning up who, adding -l and manpage
diff --git a/who.1 b/who.1
index 1e1d923..17ec534 100644
--- a/who.1
+++ b/who.1
_AT_@ -2,12 +2,20 @@
.SH NAME
who \- print who has logged on
.SH SYNOPSIS
-.B who
+.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
diff --git a/who.c b/who.c
index 6093a84..50b686a 100644
--- a/who.c
+++ b/who.c
_AT_@ -1,26 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <utmp.h>
#include "util.h"
-static void usage(void);
+static void
+usage(void)
+{
+ eprintf("usage: who [-ml]
");
+}
int
main(int argc, char **argv)
{
struct utmp usr;
FILE *ufp;
- time_t t;
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
- bool mflag = false;
+ char *tty, *ttmp;
+ int mflag = 0, lflag = 0;
+ time_t t;
ARGBEGIN {
case 'm':
- mflag = true;
+ 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();
_AT_@ -29,15 +41,16 @@ main(int argc, char **argv)
if (argc > 0)
usage();
- if (!(ufp = fopen(_PATH_UTMP, "r"))) {
- eprintf("fopen:");
- }
+ if (!(ufp = fopen(_PATH_UTMP, "r")))
+ eprintf("who: '%s':", _PATH_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,
- strrchr(ttyname(STDIN_FILENO), '/') + 1))
+ 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));
_AT_@ -47,8 +60,3 @@ main(int argc, char **argv)
return 0;
}
-void
-usage(void)
-{
- eprintf("usage: who [-m]
");
-}
Received on Wed Aug 07 2013 - 08:48:04 CEST
This archive was generated by hypermail 2.3.0
: Wed Aug 07 2013 - 09:00:25 CEST