2002-08-02 03:23:07 +04:00
|
|
|
/* $NetBSD: who.c,v 1.10 2002/08/01 23:23:09 christos Exp $ */
|
1994-11-14 08:13:14 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-11-14 08:13:14 +03:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Michael Fischbein.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-24 00:08:03 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-07-24 00:08:03 +04:00
|
|
|
__COPYRIGHT(
|
1994-11-14 08:13:14 +03:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
1997-07-24 00:08:03 +04:00
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-11-14 08:13:14 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
2002-08-02 03:23:07 +04:00
|
|
|
__RCSID("$NetBSD: who.c,v 1.10 2002/08/01 23:23:09 christos Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-12-07 07:28:47 +03:00
|
|
|
#include <sys/stat.h>
|
1997-07-24 00:08:03 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <locale.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
1994-12-07 07:28:47 +03:00
|
|
|
#include <stdlib.h>
|
1997-07-24 00:08:03 +04:00
|
|
|
#include <string.h>
|
1994-12-07 07:28:47 +03:00
|
|
|
#include <time.h>
|
1997-07-24 00:08:03 +04:00
|
|
|
#include <unistd.h>
|
2002-08-02 03:23:07 +04:00
|
|
|
#include "utmpentry.h"
|
2002-07-29 01:46:34 +04:00
|
|
|
|
|
|
|
static void output_labels(void);
|
|
|
|
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 *);
|
1997-07-24 00:08:03 +04:00
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
int main(int, char **);
|
1994-12-07 07:28:47 +03:00
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
static int show_term; /* show term state */
|
|
|
|
static int show_idle; /* show idle time */
|
|
|
|
|
|
|
|
static int maxname = 8, maxline = 8, maxhost = 16;
|
1997-07-24 00:08:03 +04:00
|
|
|
|
1994-12-07 07:28:47 +03:00
|
|
|
int
|
2002-07-29 01:46:34 +04:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-07-24 00:08:03 +04:00
|
|
|
int c, only_current_term, show_labels;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
1994-12-07 07:28:47 +03:00
|
|
|
|
1997-07-24 00:08:03 +04:00
|
|
|
only_current_term = show_term = show_idle = show_labels = 0;
|
|
|
|
while ((c = getopt(argc, argv, "mTuH")) != -1) {
|
1994-12-07 07:28:47 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'm':
|
|
|
|
only_current_term = 1;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
show_term = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
show_idle = 1;
|
|
|
|
break;
|
1997-07-24 00:08:03 +04:00
|
|
|
case 'H':
|
|
|
|
show_labels = 1;
|
|
|
|
break;
|
1994-12-07 07:28:47 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (chdir("/dev")) {
|
|
|
|
err(1, "cannot change directory to /dev");
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch (argc) {
|
1994-12-07 07:28:47 +03:00
|
|
|
case 0: /* who */
|
|
|
|
if (only_current_term) {
|
2002-07-29 01:46:34 +04:00
|
|
|
who_am_i(NULL, show_labels);
|
1994-12-07 07:28:47 +03:00
|
|
|
} else {
|
2002-07-29 01:46:34 +04:00
|
|
|
process(NULL, show_labels);
|
1994-12-07 07:28:47 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: /* who utmp_file */
|
|
|
|
if (only_current_term) {
|
2002-07-29 01:46:34 +04:00
|
|
|
who_am_i(*argv, show_labels);
|
1994-12-07 07:28:47 +03:00
|
|
|
} else {
|
2002-07-29 01:46:34 +04:00
|
|
|
process(*argv, show_labels);
|
1994-12-07 07:28:47 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: /* who am i */
|
2002-07-29 01:46:34 +04:00
|
|
|
who_am_i(NULL, show_labels);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1994-12-07 07:28:47 +03:00
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
static void
|
|
|
|
who_am_i(const char *fname, int show_labels)
|
1994-12-07 07:28:47 +03:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
1997-10-20 07:20:27 +04:00
|
|
|
char *p;
|
1994-12-07 07:28:47 +03:00
|
|
|
char *t;
|
2002-07-29 01:46:34 +04:00
|
|
|
time_t now;
|
2002-08-02 03:23:07 +04:00
|
|
|
struct utmpentry *ehead, *ep;
|
1994-12-07 07:28:47 +03:00
|
|
|
|
|
|
|
/* search through the utmp and find an entry for this tty */
|
2002-07-29 01:46:34 +04:00
|
|
|
if ((p = ttyname(STDIN_FILENO)) != NULL) {
|
|
|
|
|
1994-12-07 07:28:47 +03:00
|
|
|
/* strip any directory component */
|
1997-10-20 07:20:27 +04:00
|
|
|
if ((t = strrchr(p, '/')) != NULL)
|
1994-12-07 07:28:47 +03:00
|
|
|
p = t + 1;
|
2002-08-02 03:23:07 +04:00
|
|
|
|
|
|
|
(void)getutentries(fname, &ehead);
|
|
|
|
for (ep = ehead; ep; ep = ep->next)
|
|
|
|
if (strcmp(ep->line, p) == 0) {
|
2002-07-29 01:46:34 +04:00
|
|
|
if (show_labels)
|
|
|
|
output_labels();
|
2002-08-02 03:23:07 +04:00
|
|
|
print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
|
|
|
|
ep->host);
|
2002-07-29 01:46:34 +04:00
|
|
|
return;
|
|
|
|
}
|
1994-12-07 07:28:47 +03:00
|
|
|
} else
|
2002-07-29 01:46:34 +04:00
|
|
|
p = "tty??";
|
1994-12-07 07:28:47 +03:00
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)time(&now);
|
1994-12-07 07:28:47 +03:00
|
|
|
pw = getpwuid(getuid());
|
2002-07-29 01:46:34 +04:00
|
|
|
if (show_labels)
|
|
|
|
output_labels();
|
|
|
|
print(pw ? pw->pw_name : "?", p, now, "");
|
1994-12-07 07:28:47 +03:00
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
static void
|
|
|
|
process(const char *fname, int show_labels)
|
|
|
|
{
|
2002-08-02 03:23:07 +04:00
|
|
|
struct utmpentry *ehead, *ep;
|
|
|
|
(void)getutentries(fname, &ehead);
|
2002-07-29 01:46:34 +04:00
|
|
|
if (show_labels)
|
|
|
|
output_labels();
|
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next)
|
2002-08-02 03:23:07 +04:00
|
|
|
print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host);
|
2002-07-29 01:46:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print(const char *name, const char *line, time_t t, const char *host)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-12-07 07:28:47 +03:00
|
|
|
struct stat sb;
|
|
|
|
char state;
|
|
|
|
static time_t now = 0;
|
|
|
|
time_t idle;
|
|
|
|
|
1997-07-24 00:08:03 +04:00
|
|
|
state = '?';
|
|
|
|
idle = 0;
|
|
|
|
|
1994-12-07 07:28:47 +03:00
|
|
|
if (show_term || show_idle) {
|
|
|
|
if (now == 0)
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
if (stat(line, &sb) == 0) {
|
|
|
|
state = (sb.st_mode & 020) ? '+' : '-';
|
|
|
|
idle = now - sb.st_atime;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)printf("%-*.*s ", maxname, maxname, name);
|
1994-12-07 07:28:47 +03:00
|
|
|
|
|
|
|
if (show_term) {
|
|
|
|
(void)printf("%c ", state);
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)printf("%-*.*s ", maxline, maxline, line);
|
|
|
|
(void)printf("%.12s ", ctime(&t) + 4);
|
1994-12-07 07:28:47 +03:00
|
|
|
|
|
|
|
if (show_idle) {
|
|
|
|
if (idle < 60)
|
|
|
|
(void)printf(" . ");
|
|
|
|
else if (idle < (24 * 60 * 60))
|
1997-07-24 00:08:03 +04:00
|
|
|
(void)printf("%02ld:%02ld ",
|
|
|
|
(long)(idle / (60 * 60)),
|
|
|
|
(long)(idle % (60 * 60)) / 60);
|
1994-12-07 07:28:47 +03:00
|
|
|
else
|
|
|
|
(void)printf(" old ");
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
if (*host)
|
|
|
|
printf("\t(%.*s)", maxhost, host);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)putchar('\n');
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
static void
|
1997-07-24 00:08:03 +04:00
|
|
|
output_labels()
|
|
|
|
{
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)printf("%-*.*s ", maxname, maxname, "USER");
|
1997-07-24 00:08:03 +04:00
|
|
|
|
|
|
|
if (show_term)
|
|
|
|
(void)printf("S ");
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)printf("%-*.*s ", maxline, maxline, "LINE");
|
1997-07-24 00:08:03 +04:00
|
|
|
(void)printf("WHEN ");
|
|
|
|
|
|
|
|
if (show_idle)
|
|
|
|
(void)printf("IDLE ");
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)printf("\t%.*s", maxhost, "FROM");
|
1997-07-24 00:08:03 +04:00
|
|
|
|
|
|
|
(void)putchar('\n');
|
|
|
|
}
|
|
|
|
|
2002-07-29 01:46:34 +04:00
|
|
|
static void
|
1994-12-07 07:28:47 +03:00
|
|
|
usage()
|
|
|
|
{
|
2002-07-29 01:46:34 +04:00
|
|
|
(void)fprintf(stderr, "Usage: %s [-mTuH] [ file ]\n %s am i\n",
|
|
|
|
getprogname(), getprogname());
|
1994-12-07 07:28:47 +03:00
|
|
|
exit(1);
|
|
|
|
}
|