Cleaning up who, adding -l and manpage
This commit is contained in:
parent
eddb6d39a5
commit
fd262561a9
10
who.1
10
who.1
|
@ -2,12 +2,20 @@
|
||||||
.SH NAME
|
.SH NAME
|
||||||
who \- print who has logged on
|
who \- print who has logged on
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B who
|
.B who
|
||||||
|
.RB [ -m ]
|
||||||
|
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.B who
|
.B who
|
||||||
prints a list of who has logged on, their controlling tty, and the
|
prints a list of who has logged on, their controlling tty, and the
|
||||||
time at which they logged on.
|
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
|
.SH BUGS
|
||||||
.B who
|
.B who
|
||||||
|
|
38
who.c
38
who.c
|
@ -1,26 +1,38 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <utmp.h>
|
#include <utmp.h>
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static void usage(void);
|
static void
|
||||||
|
usage(void)
|
||||||
|
{
|
||||||
|
eprintf("usage: who [-ml]\n");
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct utmp usr;
|
struct utmp usr;
|
||||||
FILE *ufp;
|
FILE *ufp;
|
||||||
time_t t;
|
|
||||||
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
|
char timebuf[sizeof "yyyy-mm-dd hh:mm"];
|
||||||
bool mflag = false;
|
char *tty, *ttmp;
|
||||||
|
int mflag = 0, lflag = 0;
|
||||||
|
time_t t;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'm':
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
@ -29,15 +41,16 @@ main(int argc, char **argv)
|
||||||
if (argc > 0)
|
if (argc > 0)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
if (!(ufp = fopen(_PATH_UTMP, "r"))) {
|
if (!(ufp = fopen(_PATH_UTMP, "r")))
|
||||||
eprintf("fopen:");
|
eprintf("who: '%s':", _PATH_UTMP);
|
||||||
}
|
|
||||||
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
|
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
|
||||||
if (!*usr.ut_name || !*usr.ut_line ||
|
if (!*usr.ut_name || !*usr.ut_line ||
|
||||||
usr.ut_line[0] == '~')
|
usr.ut_line[0] == '~')
|
||||||
continue;
|
continue;
|
||||||
if (mflag && strcmp(usr.ut_line,
|
if (mflag && strcmp(usr.ut_line, tty))
|
||||||
strrchr(ttyname(STDIN_FILENO), '/') + 1))
|
continue;
|
||||||
|
if (strcmp(usr.ut_name, "LOGIN") == lflag)
|
||||||
continue;
|
continue;
|
||||||
t = usr.ut_time;
|
t = usr.ut_time;
|
||||||
strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
|
strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime(&t));
|
||||||
|
@ -47,8 +60,3 @@ main(int argc, char **argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
usage(void)
|
|
||||||
{
|
|
||||||
eprintf("usage: who [-m]\n");
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user