2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Tim J. Robbins.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
52 static void heading(void);
53 static void process_utmp(void);
54 static void quick(void);
55 static void row(const struct utmpx *);
56 static int ttywidth(void);
57 static void usage(void);
58 static void whoami(void);
60 static int Hflag; /* Write column headings */
61 static int aflag; /* Print all entries */
62 static int bflag; /* Show date of the last reboot */
63 static int mflag; /* Show info about current terminal */
64 static int qflag; /* "Quick" mode */
65 static int sflag; /* Show name, line, time */
66 static int Tflag; /* Show terminal state */
67 static int uflag; /* Show idle time */
70 main(int argc, char *argv[])
74 setlocale(LC_TIME, "");
76 while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
78 case 'H': /* Write column headings */
81 case 'T': /* Show terminal state */
84 case 'a': /* Same as -bdlprtTu */
85 aflag = bflag = Tflag = uflag = 1;
87 case 'b': /* Show date of the last reboot */
90 case 'm': /* Show info about current terminal */
93 case 'q': /* "Quick" mode */
96 case 's': /* Show name, line, time */
99 case 'u': /* Show idle time */
110 if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
111 (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
112 /* "who am i" or "who am I", equivalent to -m */
121 if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
147 fprintf(stderr, "usage: who [-abHmqsTu] [am I] [file]\n");
155 printf("%-16s ", "NAME");
158 printf("%-12s %-12s ", "LINE", "TIME");
161 printf("%-16s\n", "FROM");
165 row(const struct utmpx *ut)
167 char buf[80], tty[PATH_MAX];
170 static int d_first = -1;
175 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
179 if (Tflag || uflag) {
180 snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
181 if (stat(tty, &sb) == 0) {
182 state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
184 idle = time(NULL) - sb.st_mtime;
188 printf("%-16s ", ut->ut_user);
190 printf("%c ", state);
191 if (ut->ut_type == BOOT_TIME)
192 printf("%-12s ", "system boot");
194 printf("%-12s ", ut->ut_line);
195 t = ut->ut_tv.tv_sec;
197 strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
198 printf("%-*s ", 12, buf);
202 else if (idle < 24 * 60 * 60)
203 printf("%02d:%02d ", (int)(idle / 60 / 60),
204 (int)(idle / 60 % 60));
208 if (*ut->ut_host != '\0')
209 printf("(%s)", ut->ut_host);
217 char ttybuf[MAXPATHLEN];
219 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
220 if (stat(ttybuf, &sb) == 0) {
231 while ((utx = getutxent()) != NULL) {
232 if (((aflag || !bflag) && utx->ut_type == USER_PROCESS) ||
233 (bflag && utx->ut_type == BOOT_TIME))
234 if (ttystat(utx->ut_line) == 0)
247 while ((utx = getutxent()) != NULL) {
248 if (utx->ut_type != USER_PROCESS)
250 printf("%-16s", utx->ut_user);
251 if (++col < ncols / (16 + 1))
262 printf("# users = %d\n", num);
268 struct utmpx ut, *utx;
270 const char *name, *tty;
272 if ((tty = ttyname(STDIN_FILENO)) == NULL)
274 else if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
275 tty += sizeof _PATH_DEV - 1;
276 strlcpy(ut.ut_line, tty, sizeof ut.ut_line);
278 /* Search utmp for our tty, dump first matching record. */
279 if ((utx = getutxline(&ut)) != NULL && utx->ut_type == USER_PROCESS) {
284 /* Not found; fill the utmp structure with the information we have. */
285 memset(&ut, 0, sizeof(ut));
286 if ((pwd = getpwuid(getuid())) != NULL)
290 strlcpy(ut.ut_user, name, sizeof ut.ut_user);
291 gettimeofday(&ut.ut_tv, NULL);
302 if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
304 width = strtol(cols, &ep, 10);
305 if (errno || width <= 0 || width > INT_MAX || ep == cols ||
307 warnx("invalid COLUMNS environment variable ignored");
311 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)