Implement the following two XSI extensions:

-q: quick mode, list only the names and the number of users currently
logged on.

-s: default mode, list only the name, line and time fields.
This commit is contained in:
peter 2005-07-22 14:23:05 +00:00
parent c9b40229dd
commit 0d03348661
2 changed files with 65 additions and 23 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: who.1,v 1.15 2004/11/22 17:20:02 peter Exp $
.\" $NetBSD: who.1,v 1.16 2005/07/22 14:23:05 peter Exp $
.\"
.\" Copyright (c) 1986, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)who.1 8.2 (Berkeley) 12/30/93
.\"
.Dd December 6, 1994
.Dd July 22, 2005
.Dt WHO 1
.Os
.Sh NAME
@ -37,7 +37,7 @@
.Nd display who is logged in
.Sh SYNOPSIS
.Nm
.Op Fl HmTu
.Op Fl HmqsTu
.Op Ar file
.Nm
.Ar am i
@ -60,6 +60,13 @@ This is the
way of saying
.Nm
.Ar am i .
.It Fl q
.Dq Quick mode :
List only the names and the number of users currently logged on.
When this option is used, all other options are ignored.
.It Fl s
List only the name, line and time fields.
This is the default.
.It Fl T
Print a character after the user name indicating the state of the
terminal line:
@ -119,7 +126,8 @@ special characters, see
.Xr mesg 1 ,
.Xr users 1 ,
.Xr getuid 2 ,
.Xr utmp 5
.Xr utmp 5 ,
.Xr utmpx 5
.Sh STANDARDS
The
.Nm

View File

@ -1,4 +1,4 @@
/* $NetBSD: who.c,v 1.15 2005/06/26 17:10:28 christos Exp $ */
/* $NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $ */
/*
* Copyright (c) 1989, 1993
@ -43,11 +43,12 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: who.c,v 1.15 2005/06/26 17:10:28 christos Exp $");
__RCSID("$NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <locale.h>
#include <pwd.h>
@ -56,6 +57,7 @@ __RCSID("$NetBSD: who.c,v 1.15 2005/06/26 17:10:28 christos Exp $");
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "utmpentry.h"
static void output_labels(void);
@ -63,8 +65,7 @@ static void who_am_i(const char *, int);
static void usage(void);
static void process(const char *, int);
static void print(const char *, const char *, time_t, const char *);
int main(int, char **);
static void quick(const char *);
static int show_term; /* show term state */
static int show_idle; /* show idle time */
@ -72,14 +73,16 @@ static int show_idle; /* show idle time */
extern int maxname, maxline, maxhost;
int
main(int argc, char **argv)
main(int argc, char *argv[])
{
int c, only_current_term, show_labels;
int c, only_current_term, show_labels, quick_mode, default_mode;
setlocale(LC_ALL, "");
only_current_term = show_term = show_idle = show_labels = 0;
while ((c = getopt(argc, argv, "HmTu")) != -1) {
quick_mode = default_mode = 0;
while ((c = getopt(argc, argv, "HmqsTu")) != -1) {
switch (c) {
case 'H':
show_labels = 1;
@ -87,6 +90,12 @@ main(int argc, char **argv)
case 'm':
only_current_term = 1;
break;
case 'q':
quick_mode = 1;
break;
case 's':
default_mode = 1;
break;
case 'T':
show_term = 1;
break;
@ -102,20 +111,27 @@ main(int argc, char **argv)
argv += optind;
if (chdir("/dev")) {
err(1, "cannot change directory to /dev");
err(EXIT_FAILURE, "cannot change directory to /dev");
/* NOTREACHED */
}
if (default_mode)
only_current_term = show_term = show_idle = 0;
switch (argc) {
case 0: /* who */
if (only_current_term) {
if (quick_mode) {
quick(NULL);
} else if (only_current_term) {
who_am_i(NULL, show_labels);
} else {
process(NULL, show_labels);
}
break;
case 1: /* who utmp_file */
if (only_current_term) {
if (quick_mode) {
quick(*argv);
} else if (only_current_term) {
who_am_i(*argv, show_labels);
} else {
process(*argv, show_labels);
@ -128,7 +144,8 @@ main(int argc, char **argv)
usage();
/* NOTREACHED */
}
exit(0);
return 0;
}
static void
@ -201,9 +218,8 @@ print(const char *name, const char *line, time_t t, const char *host)
(void)printf("%-*.*s ", maxname, maxname, name);
if (show_term) {
if (show_term)
(void)printf("%c ", state);
}
(void)printf("%-*.*s ", maxline, maxline, line);
(void)printf("%.12s ", ctime(&t) + 4);
@ -220,12 +236,12 @@ print(const char *name, const char *line, time_t t, const char *host)
}
if (*host)
printf("\t(%.*s)", maxhost, host);
(void)printf("\t(%.*s)", maxhost, host);
(void)putchar('\n');
}
static void
output_labels()
output_labels(void)
{
(void)printf("%-*.*s ", maxname, maxname, "USER");
@ -244,9 +260,27 @@ output_labels()
}
static void
usage()
quick(const char *fname)
{
(void)fprintf(stderr, "usage: %s [-HmTu] [file]\n %s am i\n",
getprogname(), getprogname());
exit(1);
struct utmpentry *ehead, *ep;
int num = 0;
(void)getutentries(fname, &ehead);
for (ep = ehead; ep != NULL; ep = ep->next) {
(void)printf("%-*s ", maxname, ep->name);
if ((++num % 8) == 0)
(void)putchar('\n');
}
if (num % 8)
(void)putchar('\n');
(void)printf("# users = %d\n", num);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: %s [-HmqsTu] [file]\n %s am i\n",
getprogname(), getprogname());
exit(EXIT_FAILURE);
}