version 1.4, 1997/03/25 21:28:12 |
version 1.5, 1997/07/28 17:38:53 |
|
|
#include <unistd.h> |
#include <unistd.h> |
#include <time.h> |
#include <time.h> |
#include <err.h> |
#include <err.h> |
|
#include <locale.h> |
|
|
void output __P((struct utmp *)); |
void output __P((struct utmp *)); |
void who_am_i __P((FILE *)); |
void output_labels __P((void)); |
void usage __P((void)); |
void who_am_i __P((FILE *)); |
|
void usage __P((void)); |
|
FILE *file __P((char *)); |
|
|
int only_current_term; /* show info about the current terminal only */ |
int only_current_term; /* show info about the current terminal only */ |
int show_term; /* show term state */ |
int show_term; /* show term state */ |
int show_idle; /* show idle time */ |
int show_idle; /* show idle time */ |
|
int show_labels; /* show column labels */ |
|
|
int |
int |
main(argc, argv) |
main(argc, argv) |
|
|
char **argv; |
char **argv; |
{ |
{ |
struct utmp usr; |
struct utmp usr; |
FILE *ufp, *file(); |
FILE *ufp; |
int c; |
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) { |
switch (c) { |
case 'm': |
case 'm': |
only_current_term = 1; |
only_current_term = 1; |
|
|
case 'u': |
case 'u': |
show_idle = 1; |
show_idle = 1; |
break; |
break; |
|
case 'H': |
|
show_labels = 1; |
|
break; |
default: |
default: |
usage(); |
usage(); |
/* NOTREACHED */ |
/* NOTREACHED */ |
|
|
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
|
|
if (show_labels) |
|
output_labels(); |
|
|
switch (argc) { |
switch (argc) { |
case 0: /* who */ |
case 0: /* who */ |
ufp = file(_PATH_UTMP); |
ufp = file(_PATH_UTMP); |
|
|
char *t; |
char *t; |
|
|
/* search through the utmp and find an entry for this tty */ |
/* search through the utmp and find an entry for this tty */ |
if (p = ttyname(0)) { |
if ((p = ttyname(0))) { |
/* strip any directory component */ |
/* strip any directory component */ |
if (t = strrchr(p, '/')) |
if ((t = strrchr(p, '/'))) |
p = t + 1; |
p = t + 1; |
while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) |
while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) |
if (*usr.ut_name && !strcmp(usr.ut_line, p)) { |
if (*usr.ut_name && !strcmp(usr.ut_line, p)) { |
|
|
{ |
{ |
struct stat sb; |
struct stat sb; |
char line[sizeof (up->ut_line) + 1]; |
char line[sizeof (up->ut_line) + 1]; |
char state; |
char state = '?'; |
static time_t now = 0; |
static time_t now = 0; |
time_t idle; |
time_t idle = 0; |
|
|
if (show_term || show_idle) { |
if (show_term || show_idle) { |
if (now == 0) |
if (now == 0) |
|
|
(void)putchar('\n'); |
(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 * |
file(name) |
file(name) |
char *name; |
char *name; |
|
|
void |
void |
usage() |
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); |
exit(1); |
} |
} |