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$");
|
|
|
|
|
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>
|
2002-11-15 22:42:00 +00:00
|
|
|
#include <timeconv.h>
|
1997-08-26 11:14:57 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-09 23:04:40 +00:00
|
|
|
static void heading(void);
|
|
|
|
static void process_utmp(FILE *);
|
|
|
|
static void quick(FILE *);
|
|
|
|
static void row(struct utmp *);
|
|
|
|
static int ttywidth(void);
|
|
|
|
static void usage(void);
|
|
|
|
static void whoami(FILE *);
|
|
|
|
|
|
|
|
static int Hflag; /* Write column headings */
|
|
|
|
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;
|
|
|
|
const char *file;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
setlocale(LC_TIME, "");
|
|
|
|
|
2002-12-21 00:38:14 +00:00
|
|
|
while ((ch = getopt(argc, argv, "HTmqsu")) != -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;
|
|
|
|
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
|
|
|
|
|
|
|
if (*argv != NULL)
|
|
|
|
file = *argv;
|
|
|
|
else
|
|
|
|
file = _PATH_UTMP;
|
|
|
|
if ((fp = fopen(file, "r")) == NULL)
|
|
|
|
err(1, "%s", file);
|
|
|
|
|
|
|
|
if (qflag)
|
|
|
|
quick(fp);
|
|
|
|
else {
|
|
|
|
if (sflag)
|
|
|
|
Tflag = uflag = 0;
|
|
|
|
if (Hflag)
|
|
|
|
heading();
|
|
|
|
if (mflag)
|
|
|
|
whoami(fp);
|
|
|
|
else
|
|
|
|
process_utmp(fp);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-05-09 23:04:40 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
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
|
|
|
|
|
|
|
fprintf(stderr, "usage: who [-HmqsTu] [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
|
|
|
|
|
|
|
printf("%-*s ", UT_NAMESIZE, "NAME");
|
|
|
|
if (Tflag)
|
|
|
|
printf("S ");
|
|
|
|
printf("%-*s ", UT_LINESIZE, "LINE");
|
|
|
|
printf("%-*s ", 12, "TIME");
|
|
|
|
if (uflag)
|
|
|
|
printf("IDLE ");
|
2002-05-28 06:36:46 +00:00
|
|
|
printf("%-*s", UT_HOSTSIZE, "FROM");
|
2002-05-09 23:04:40 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2002-05-09 23:04:40 +00:00
|
|
|
row(struct utmp *ut)
|
|
|
|
{
|
|
|
|
char buf[80], tty[sizeof(_PATH_DEV) + UT_LINESIZE];
|
|
|
|
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
|
|
|
|
2002-05-09 23:04:40 +00:00
|
|
|
if (Tflag || uflag) {
|
|
|
|
snprintf(tty, sizeof(tty), "%s%.*s", _PATH_DEV,
|
|
|
|
UT_LINESIZE, ut->ut_line);
|
|
|
|
state = '?';
|
|
|
|
idle = 0;
|
|
|
|
if (stat(tty, &sb) == 0) {
|
|
|
|
state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
|
|
|
|
'+' : '-';
|
|
|
|
idle = time(NULL) - sb.st_mtime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, ut->ut_name);
|
|
|
|
if (Tflag)
|
|
|
|
printf("%c ", state);
|
|
|
|
printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, ut->ut_line);
|
2002-11-15 22:42:00 +00:00
|
|
|
t = _time32_to_time(ut->ut_time);
|
|
|
|
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')
|
|
|
|
printf("(%.*s)", UT_HOSTSIZE, ut->ut_host);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2002-05-09 23:04:40 +00:00
|
|
|
process_utmp(FILE *fp)
|
|
|
|
{
|
|
|
|
struct utmp ut;
|
|
|
|
|
|
|
|
while (fread(&ut, sizeof(ut), 1, fp) == 1)
|
|
|
|
if (*ut.ut_name != '\0')
|
|
|
|
row(&ut);
|
|
|
|
}
|
|
|
|
|
2002-06-20 07:36:22 +00:00
|
|
|
static void
|
2002-05-09 23:04:40 +00:00
|
|
|
quick(FILE *fp)
|
|
|
|
{
|
|
|
|
struct utmp ut;
|
|
|
|
int col, ncols, num;
|
|
|
|
|
|
|
|
ncols = ttywidth();
|
|
|
|
col = num = 0;
|
|
|
|
while (fread(&ut, sizeof(ut), 1, fp) == 1) {
|
|
|
|
if (*ut.ut_name == '\0')
|
|
|
|
continue;
|
|
|
|
printf("%-*.*s", UT_NAMESIZE, UT_NAMESIZE, ut.ut_name);
|
|
|
|
if (++col < ncols / (UT_NAMESIZE + 1))
|
|
|
|
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
|
2002-05-09 23:04:40 +00:00
|
|
|
whoami(FILE *fp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-09 23:04:40 +00:00
|
|
|
struct utmp ut;
|
|
|
|
struct passwd *pwd;
|
|
|
|
const char *name, *p, *tty;
|
|
|
|
|
|
|
|
if ((tty = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
tty = "tty??";
|
|
|
|
else if ((p = strrchr(tty, '/')) != NULL)
|
|
|
|
tty = p + 1;
|
|
|
|
|
|
|
|
/* Search utmp for our tty, dump first matching record. */
|
|
|
|
while (fread(&ut, sizeof(ut), 1, fp) == 1)
|
|
|
|
if (*ut.ut_name != '\0' && strncmp(ut.ut_line, tty,
|
|
|
|
UT_LINESIZE) == 0) {
|
|
|
|
row(&ut);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 = "?";
|
|
|
|
strncpy(ut.ut_name, name, UT_NAMESIZE);
|
|
|
|
strncpy(ut.ut_line, tty, UT_LINESIZE);
|
2002-11-15 22:42:00 +00:00
|
|
|
ut.ut_time = _time_to_time32(time(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
|
|
|
}
|