2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
49 static void heading(void);
50 static void process_utmp(void);
51 static void quick(void);
52 static void row(const struct utmpx *);
53 static int ttywidth(void);
54 static void usage(void);
55 static void whoami(void);
57 static int Hflag; /* Write column headings */
58 static int aflag; /* Print all entries */
59 static int bflag; /* Show date of the last reboot */
60 static int mflag; /* Show info about current terminal */
61 static int qflag; /* "Quick" mode */
62 static int sflag; /* Show name, line, time */
63 static int Tflag; /* Show terminal state */
64 static int uflag; /* Show idle time */
67 main(int argc, char *argv[])
71 setlocale(LC_TIME, "");
73 while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
75 case 'H': /* Write column headings */
78 case 'T': /* Show terminal state */
81 case 'a': /* Same as -bdlprtTu */
82 aflag = bflag = Tflag = uflag = 1;
84 case 'b': /* Show date of the last reboot */
87 case 'm': /* Show info about current terminal */
90 case 'q': /* "Quick" mode */
93 case 's': /* Show name, line, time */
96 case 'u': /* Show idle time */
107 if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
108 (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
109 /* "who am i" or "who am I", equivalent to -m */
118 if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
144 fprintf(stderr, "usage: who [-abHmqsTu] [am I] [file]\n");
152 printf("%-16s ", "NAME");
155 printf("%-12s %-12s ", "LINE", "TIME");
158 printf("%-16s\n", "FROM");
162 row(const struct utmpx *ut)
164 char buf[80], tty[PATH_MAX];
167 static int d_first = -1;
172 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
176 if (Tflag || uflag) {
177 snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
178 if (stat(tty, &sb) == 0) {
179 state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
181 idle = time(NULL) - sb.st_mtime;
185 printf("%-16s ", ut->ut_user);
187 printf("%c ", state);
188 if (ut->ut_type == BOOT_TIME)
189 printf("%-12s ", "system boot");
191 printf("%-12s ", ut->ut_line);
192 t = ut->ut_tv.tv_sec;
194 strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
195 printf("%-*s ", 12, buf);
199 else if (idle < 24 * 60 * 60)
200 printf("%02d:%02d ", (int)(idle / 60 / 60),
201 (int)(idle / 60 % 60));
205 if (*ut->ut_host != '\0')
206 printf("(%s)", ut->ut_host);
214 char ttybuf[MAXPATHLEN];
218 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
219 if (stat(ttybuf, &sb) == 0) {
230 while ((utx = getutxent()) != NULL) {
231 if ((aflag || !bflag) && utx->ut_type == USER_PROCESS) {
232 if (ttystat(utx->ut_line) == 0)
234 } else if (bflag && utx->ut_type == BOOT_TIME)
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)