2 * Copyright (c) 2002 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
50 static void heading(void);
51 static void process_utmp(void);
52 static void quick(void);
53 static void row(const struct utmpx *);
54 static int ttywidth(void);
55 static void usage(void);
56 static void whoami(void);
58 static int Hflag; /* Write column headings */
59 static int aflag; /* Print all entries */
60 static int bflag; /* Show date of the last reboot */
61 static int mflag; /* Show info about current terminal */
62 static int qflag; /* "Quick" mode */
63 static int sflag; /* Show name, line, time */
64 static int Tflag; /* Show terminal state */
65 static int uflag; /* Show idle time */
68 main(int argc, char *argv[])
72 setlocale(LC_TIME, "");
74 while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
76 case 'H': /* Write column headings */
79 case 'T': /* Show terminal state */
82 case 'a': /* Same as -bdlprtTu */
83 aflag = bflag = Tflag = uflag = 1;
85 case 'b': /* Show date of the last reboot */
88 case 'm': /* Show info about current terminal */
91 case 'q': /* "Quick" mode */
94 case 's': /* Show name, line, time */
97 case 'u': /* Show idle time */
108 if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
109 (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
110 /* "who am i" or "who am I", equivalent to -m */
119 if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
145 fprintf(stderr, "usage: who [-abHmqsTu] [am I] [file]\n");
153 printf("%-16s ", "NAME");
156 printf("%-12s %-12s ", "LINE", "TIME");
159 printf("%-16s\n", "FROM");
163 row(const struct utmpx *ut)
165 char buf[80], tty[PATH_MAX];
168 static int d_first = -1;
173 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
177 if (Tflag || uflag) {
178 snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
179 if (stat(tty, &sb) == 0) {
180 state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
182 idle = time(NULL) - sb.st_mtime;
186 printf("%-16s ", ut->ut_user);
188 printf("%c ", state);
189 if (ut->ut_type == BOOT_TIME)
190 printf("%-12s ", "system boot");
192 printf("%-12s ", ut->ut_line);
193 t = ut->ut_tv.tv_sec;
195 strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
196 printf("%-*s ", 12, buf);
200 else if (idle < 24 * 60 * 60)
201 printf("%02d:%02d ", (int)(idle / 60 / 60),
202 (int)(idle / 60 % 60));
206 if (*ut->ut_host != '\0')
207 printf("(%s)", ut->ut_host);
215 char ttybuf[MAXPATHLEN];
217 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
218 if (stat(ttybuf, &sb) == 0) {
229 while ((utx = getutxent()) != NULL) {
230 if (((aflag || !bflag) && utx->ut_type == USER_PROCESS) ||
231 (bflag && utx->ut_type == BOOT_TIME))
232 if (ttystat(utx->ut_line) == 0)
245 while ((utx = getutxent()) != NULL) {
246 if (utx->ut_type != USER_PROCESS)
248 printf("%-16s", utx->ut_user);
249 if (++col < ncols / (16 + 1))
260 printf("# users = %d\n", num);
266 struct utmpx ut, *utx;
268 const char *name, *tty;
270 if ((tty = ttyname(STDIN_FILENO)) == NULL)
272 else if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
273 tty += sizeof _PATH_DEV - 1;
274 strlcpy(ut.ut_line, tty, sizeof ut.ut_line);
276 /* Search utmp for our tty, dump first matching record. */
277 if ((utx = getutxline(&ut)) != NULL && utx->ut_type == USER_PROCESS) {
282 /* Not found; fill the utmp structure with the information we have. */
283 memset(&ut, 0, sizeof(ut));
284 if ((pwd = getpwuid(getuid())) != NULL)
288 strlcpy(ut.ut_user, name, sizeof ut.ut_user);
289 gettimeofday(&ut.ut_tv, NULL);
300 if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
302 width = strtol(cols, &ep, 10);
303 if (errno || width <= 0 || width > INT_MAX || ep == cols ||
305 warnx("invalid COLUMNS environment variable ignored");
309 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)