2002-05-09 23:04:40 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2002-05-09 23:04:40 +00:00
|
|
|
* Copyright (c) 2002 Tim J. Robbins.
|
|
|
|
* All rights reserved.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2002-05-09 23:04:40 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1994-05-27 12:33:43 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2002-05-09 23:04:40 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 22:29:53 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-02-21 13:01:00 +00:00
|
|
|
#include <sys/param.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/types.h>
|
2002-05-09 23:04:40 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2001-12-11 22:29:53 +00:00
|
|
|
|
1997-08-26 11:14:57 +00:00
|
|
|
#include <err.h>
|
2002-06-04 09:52:30 +00:00
|
|
|
#include <errno.h>
|
2001-03-21 19:16:41 +00:00
|
|
|
#include <langinfo.h>
|
2002-06-04 09:52:30 +00:00
|
|
|
#include <limits.h>
|
1997-08-26 11:14:57 +00:00
|
|
|
#include <locale.h>
|
2002-05-09 23:04:40 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2001-06-24 19:50:42 +00:00
|
|
|
#include <stdlib.h>
|
1997-08-26 11:14:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <timeconv.h>
|
1997-08-26 11:14:57 +00:00
|
|
|
#include <unistd.h>
|
2010-01-13 18:09:54 +00:00
|
|
|
#include <utmpx.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-09 23:04:40 +00:00
|
|
|
static void heading(void);
|
2009-12-05 20:44:19 +00:00
|
|
|
static void process_utmp(void);
|
|
|
|
static void quick(void);
|
2011-10-28 12:47:37 +00:00
|
|
|
static void row(const struct utmpx *);
|
2002-05-09 23:04:40 +00:00
|
|
|
static int ttywidth(void);
|
|
|
|
static void usage(void);
|
2009-12-05 20:44:19 +00:00
|
|
|
static void whoami(void);
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
static int Hflag; /* Write column headings */
|
2012-02-11 21:49:23 +00:00
|
|
|
static int aflag; /* Print all entries */
|
2011-10-28 12:47:37 +00:00
|
|
|
static int bflag; /* Show date of the last reboot */
|
2002-05-09 23:04:40 +00:00
|
|
|
static int mflag; /* Show info about current terminal */
|
|
|
|
static int qflag; /* "Quick" mode */
|
|
|
|
static int sflag; /* Show name, line, time */
|
|
|
|
static int Tflag; /* Show terminal state */
|
|
|
|
static int uflag; /* Show idle time */
|
1997-08-26 11:14:57 +00:00
|
|
|
|
|
|
|
int
|
2002-05-09 23:04:40 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-09 23:04:40 +00:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
setlocale(LC_TIME, "");
|
|
|
|
|
2012-02-11 21:49:23 +00:00
|
|
|
while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
|
2002-05-09 23:04:40 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'H': /* Write column headings */
|
|
|
|
Hflag = 1;
|
|
|
|
break;
|
|
|
|
case 'T': /* Show terminal state */
|
|
|
|
Tflag = 1;
|
|
|
|
break;
|
2012-02-11 21:49:23 +00:00
|
|
|
case 'a': /* Same as -bdlprtTu */
|
|
|
|
aflag = bflag = Tflag = uflag = 1;
|
|
|
|
break;
|
2011-10-28 12:47:37 +00:00
|
|
|
case 'b': /* Show date of the last reboot */
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
2002-05-09 23:04:40 +00:00
|
|
|
case 'm': /* Show info about current terminal */
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
|
|
|
case 'q': /* "Quick" mode */
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
|
|
|
case 's': /* Show name, line, time */
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u': /* Show idle time */
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
|
|
|
|
(strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
|
|
|
|
/* "who am i" or "who am I", equivalent to -m */
|
|
|
|
mflag = 1;
|
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
|
|
|
}
|
|
|
|
if (argc > 1)
|
1997-08-26 11:14:57 +00:00
|
|
|
usage();
|
2002-05-09 23:04:40 +00:00
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
if (*argv != NULL) {
|
2010-01-13 18:09:54 +00:00
|
|
|
if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
|
2009-12-05 20:44:19 +00:00
|
|
|
err(1, "%s", *argv);
|
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
if (qflag)
|
2009-12-05 20:44:19 +00:00
|
|
|
quick();
|
2002-05-09 23:04:40 +00:00
|
|
|
else {
|
|
|
|
if (sflag)
|
|
|
|
Tflag = uflag = 0;
|
|
|
|
if (Hflag)
|
|
|
|
heading();
|
|
|
|
if (mflag)
|
2009-12-05 20:44:19 +00:00
|
|
|
whoami();
|
2002-05-09 23:04:40 +00:00
|
|
|
else
|
2009-12-05 20:44:19 +00:00
|
|
|
process_utmp();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
endutxent();
|
2002-05-09 23:04:40 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2002-05-09 23:04:40 +00:00
|
|
|
usage(void)
|
1997-08-26 11:14:57 +00:00
|
|
|
{
|
2002-05-09 23:04:40 +00:00
|
|
|
|
2012-02-11 21:50:44 +00:00
|
|
|
fprintf(stderr, "usage: who [-abHmqsTu] [am I] [file]\n");
|
1997-08-26 11:14:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2002-05-09 23:04:40 +00:00
|
|
|
heading(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-09 23:04:40 +00:00
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
printf("%-16s ", "NAME");
|
2002-05-09 23:04:40 +00:00
|
|
|
if (Tflag)
|
|
|
|
printf("S ");
|
2011-10-28 12:47:37 +00:00
|
|
|
printf("%-12s %-12s ", "LINE", "TIME");
|
2002-05-09 23:04:40 +00:00
|
|
|
if (uflag)
|
|
|
|
printf("IDLE ");
|
2009-12-05 20:44:19 +00:00
|
|
|
printf("%-16s\n", "FROM");
|
2002-05-09 23:04:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2011-10-28 12:47:37 +00:00
|
|
|
row(const struct utmpx *ut)
|
2002-05-09 23:04:40 +00:00
|
|
|
{
|
2009-12-05 20:44:19 +00:00
|
|
|
char buf[80], tty[PATH_MAX];
|
2002-05-09 23:04:40 +00:00
|
|
|
struct stat sb;
|
2002-11-15 22:42:00 +00:00
|
|
|
time_t idle, t;
|
2001-03-21 19:16:41 +00:00
|
|
|
static int d_first = -1;
|
2002-05-09 23:04:40 +00:00
|
|
|
struct tm *tm;
|
|
|
|
char state;
|
2001-03-21 19:16:41 +00:00
|
|
|
|
|
|
|
if (d_first < 0)
|
|
|
|
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-05-29 15:52:48 +00:00
|
|
|
state = '?';
|
|
|
|
idle = 0;
|
2002-05-09 23:04:40 +00:00
|
|
|
if (Tflag || uflag) {
|
2009-12-05 20:44:19 +00:00
|
|
|
snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
|
2002-05-09 23:04:40 +00:00
|
|
|
if (stat(tty, &sb) == 0) {
|
|
|
|
state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
|
|
|
|
'+' : '-';
|
|
|
|
idle = time(NULL) - sb.st_mtime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
printf("%-16s ", ut->ut_user);
|
2002-05-09 23:04:40 +00:00
|
|
|
if (Tflag)
|
|
|
|
printf("%c ", state);
|
2011-10-28 12:47:37 +00:00
|
|
|
if (ut->ut_type == BOOT_TIME)
|
|
|
|
printf("%-12s ", "system boot");
|
|
|
|
else
|
|
|
|
printf("%-12s ", ut->ut_line);
|
2009-12-05 20:44:19 +00:00
|
|
|
t = ut->ut_tv.tv_sec;
|
2002-11-15 22:42:00 +00:00
|
|
|
tm = localtime(&t);
|
2002-05-09 23:04:40 +00:00
|
|
|
strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
|
|
|
|
printf("%-*s ", 12, buf);
|
|
|
|
if (uflag) {
|
|
|
|
if (idle < 60)
|
|
|
|
printf(" . ");
|
|
|
|
else if (idle < 24 * 60 * 60)
|
|
|
|
printf("%02d:%02d ", (int)(idle / 60 / 60),
|
|
|
|
(int)(idle / 60 % 60));
|
|
|
|
else
|
|
|
|
printf(" old ");
|
|
|
|
}
|
|
|
|
if (*ut->ut_host != '\0')
|
2009-12-05 20:44:19 +00:00
|
|
|
printf("(%s)", ut->ut_host);
|
2002-05-09 23:04:40 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2006-02-21 13:01:00 +00:00
|
|
|
static int
|
2009-12-05 20:44:19 +00:00
|
|
|
ttystat(char *line)
|
2006-02-21 13:01:00 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
char ttybuf[MAXPATHLEN];
|
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
|
2006-02-21 13:01:00 +00:00
|
|
|
if (stat(ttybuf, &sb) == 0) {
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2009-12-05 20:44:19 +00:00
|
|
|
process_utmp(void)
|
2002-05-09 23:04:40 +00:00
|
|
|
{
|
2009-12-05 20:44:19 +00:00
|
|
|
struct utmpx *utx;
|
2002-05-09 23:04:40 +00:00
|
|
|
|
2009-12-05 20:44:19 +00:00
|
|
|
while ((utx = getutxent()) != NULL) {
|
2012-02-11 21:49:23 +00:00
|
|
|
if (((aflag || !bflag) && utx->ut_type == USER_PROCESS) ||
|
|
|
|
(bflag && utx->ut_type == BOOT_TIME))
|
|
|
|
if (ttystat(utx->ut_line) == 0)
|
|
|
|
row(utx);
|
2006-02-21 13:01:00 +00:00
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2009-12-05 20:44:19 +00:00
|
|
|
quick(void)
|
2002-05-09 23:04:40 +00:00
|
|
|
{
|
2009-12-05 20:44:19 +00:00
|
|
|
struct utmpx *utx;
|
2002-05-09 23:04:40 +00:00
|
|
|
int col, ncols, num;
|
|
|
|
|
|
|
|
ncols = ttywidth();
|
|
|
|
col = num = 0;
|
2009-12-05 20:44:19 +00:00
|
|
|
while ((utx = getutxent()) != NULL) {
|
|
|
|
if (utx->ut_type != USER_PROCESS)
|
2002-05-09 23:04:40 +00:00
|
|
|
continue;
|
2009-12-05 20:44:19 +00:00
|
|
|
printf("%-16s", utx->ut_user);
|
|
|
|
if (++col < ncols / (16 + 1))
|
2002-05-09 23:04:40 +00:00
|
|
|
putchar(' ');
|
|
|
|
else {
|
|
|
|
col = 0;
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
if (col != 0)
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
printf("# users = %d\n", num);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2009-12-05 20:44:19 +00:00
|
|
|
whoami(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2009-12-05 20:44:19 +00:00
|
|
|
struct utmpx ut, *utx;
|
2002-05-09 23:04:40 +00:00
|
|
|
struct passwd *pwd;
|
2009-12-05 20:44:19 +00:00
|
|
|
const char *name, *tty;
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
if ((tty = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
tty = "tty??";
|
2009-12-05 20:44:19 +00:00
|
|
|
else if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
|
|
|
|
tty += sizeof _PATH_DEV - 1;
|
|
|
|
strlcpy(ut.ut_line, tty, sizeof ut.ut_line);
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
/* Search utmp for our tty, dump first matching record. */
|
2009-12-05 20:44:19 +00:00
|
|
|
if ((utx = getutxline(&ut)) != NULL && utx->ut_type == USER_PROCESS) {
|
|
|
|
row(utx);
|
|
|
|
return;
|
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
/* Not found; fill the utmp structure with the information we have. */
|
|
|
|
memset(&ut, 0, sizeof(ut));
|
|
|
|
if ((pwd = getpwuid(getuid())) != NULL)
|
|
|
|
name = pwd->pw_name;
|
|
|
|
else
|
|
|
|
name = "?";
|
2009-12-05 20:44:19 +00:00
|
|
|
strlcpy(ut.ut_user, name, sizeof ut.ut_user);
|
|
|
|
gettimeofday(&ut.ut_tv, NULL);
|
2002-05-09 23:04:40 +00:00
|
|
|
row(&ut);
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static int
|
2002-05-09 23:04:40 +00:00
|
|
|
ttywidth(void)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
2002-06-04 09:52:30 +00:00
|
|
|
long width;
|
|
|
|
char *cols, *ep;
|
|
|
|
|
|
|
|
if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
|
|
|
|
errno = 0;
|
|
|
|
width = strtol(cols, &ep, 10);
|
|
|
|
if (errno || width <= 0 || width > INT_MAX || ep == cols ||
|
|
|
|
*ep != '\0')
|
|
|
|
warnx("invalid COLUMNS environment variable ignored");
|
|
|
|
else
|
2003-10-26 05:05:48 +00:00
|
|
|
return (width);
|
2002-06-04 09:52:30 +00:00
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
|
2002-06-04 09:52:30 +00:00
|
|
|
return (ws.ws_col);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-06-04 09:52:30 +00:00
|
|
|
return (80);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|