1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-03 06:45:37 -08:00

1) Prototypes. pretty.

2) setlocale(LC_ALL, "") from kleink@netbsd
3) Add XPG4.2 `-H' flag (show column headers) from kleink@netbsd
4) initialize flag variables before getopt
5) some Wall cleanup.
This commit is contained in:
flipk 1997-07-28 17:38:53 +00:00
parent fe72350772
commit 0b08963b3d
2 changed files with 48 additions and 13 deletions

View File

@ -1,4 +1,4 @@
.\" $OpenBSD: who.1,v 1.2 1996/06/26 05:43:00 deraadt Exp $
.\" $OpenBSD: who.1,v 1.3 1997/07/28 17:38:53 flipk Exp $
.\" $NetBSD: who.1,v 1.5 1994/12/07 04:28:47 jtc Exp $
.\"
.\" Copyright (c) 1986, 1991, 1993
@ -74,6 +74,8 @@ and
if a bad line is encountered.
.It Fl u
Print the idle time for each user.
.It Fl H
Write column headings above the regular output.
.It Ar \&am I
Returns the invoker's real user name.
.It Ar file

View File

@ -1,4 +1,4 @@
/* $OpenBSD: who.c,v 1.4 1997/03/25 21:28:12 deraadt Exp $ */
/* $OpenBSD: who.c,v 1.5 1997/07/28 17:38:53 flipk Exp $ */
/* $NetBSD: who.c,v 1.4 1994/12/07 04:28:49 jtc Exp $ */
/*
@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
#endif
static char rcsid[] = "$OpenBSD: who.c,v 1.4 1997/03/25 21:28:12 deraadt Exp $";
static char rcsid[] = "$OpenBSD: who.c,v 1.5 1997/07/28 17:38:53 flipk Exp $";
#endif /* not lint */
#include <sys/types.h>
@ -60,13 +60,18 @@ static char rcsid[] = "$OpenBSD: who.c,v 1.4 1997/03/25 21:28:12 deraadt Exp $";
#include <unistd.h>
#include <time.h>
#include <err.h>
#include <locale.h>
void output __P((struct utmp *));
void output_labels __P((void));
void who_am_i __P((FILE *));
void usage __P((void));
FILE *file __P((char *));
void output __P((struct utmp *));
void who_am_i __P((FILE *));
void usage __P((void));
int only_current_term; /* show info about the current terminal only */
int show_term; /* show term state */
int show_idle; /* show idle time */
int show_labels; /* show column labels */
int
main(argc, argv)
@ -74,10 +79,13 @@ main(argc, argv)
char **argv;
{
struct utmp usr;
FILE *ufp, *file();
FILE *ufp;
int c;
while ((c = getopt(argc, argv, "mTu")) != -1) {
setlocale(LC_ALL, "");
only_current_term = show_term = show_idle = show_labels = 0;
while ((c = getopt(argc, argv, "HmTu")) != -1) {
switch (c) {
case 'm':
only_current_term = 1;
@ -88,6 +96,9 @@ main(argc, argv)
case 'u':
show_idle = 1;
break;
case 'H':
show_labels = 1;
break;
default:
usage();
/* NOTREACHED */
@ -101,6 +112,9 @@ main(argc, argv)
/* NOTREACHED */
}
if (show_labels)
output_labels();
switch (argc) {
case 0: /* who */
ufp = file(_PATH_UTMP);
@ -146,9 +160,9 @@ who_am_i(ufp)
char *t;
/* search through the utmp and find an entry for this tty */
if (p = ttyname(0)) {
if ((p = ttyname(0))) {
/* strip any directory component */
if (t = strrchr(p, '/'))
if ((t = strrchr(p, '/')))
p = t + 1;
while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
if (*usr.ut_name && !strcmp(usr.ut_line, p)) {
@ -173,9 +187,9 @@ output(up)
{
struct stat sb;
char line[sizeof (up->ut_line) + 1];
char state;
char state = '?';
static time_t now = 0;
time_t idle;
time_t idle = 0;
if (show_term || show_idle) {
if (now == 0)
@ -219,6 +233,25 @@ output(up)
(void)putchar('\n');
}
void
output_labels()
{
(void)printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, "USER");
if (show_term)
(void)printf("S ");
(void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, "LINE");
(void)printf("WHEN ");
if (show_idle)
(void)printf("IDLE ");
(void)printf("\t%.*s", UT_HOSTSIZE, "FROM");
(void)putchar('\n');
}
FILE *
file(name)
char *name;
@ -235,6 +268,6 @@ file(name)
void
usage()
{
(void)fprintf(stderr, "usage: who [-mTu] [ file ]\n who am i\n");
(void)fprintf(stderr, "usage: who [-mTuH] [ file ]\n who am i\n");
exit(1);
}