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(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 mflag; /* Show info about current terminal */
60 static int qflag; /* "Quick" mode */
61 static int sflag; /* Show name, line, time */
62 static int Tflag; /* Show terminal state */
63 static int uflag; /* Show idle time */
66 main(int argc, char *argv[])
70 setlocale(LC_TIME, "");
72 while ((ch = getopt(argc, argv, "HTmqsu")) != -1) {
74 case 'H': /* Write column headings */
77 case 'T': /* Show terminal state */
80 case 'm': /* Show info about current terminal */
83 case 'q': /* "Quick" mode */
86 case 's': /* Show name, line, time */
89 case 'u': /* Show idle time */
100 if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
101 (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
102 /* "who am i" or "who am I", equivalent to -m */
111 if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
137 fprintf(stderr, "usage: who [-HmqsTu] [am I] [file]\n");
145 printf("%-16s ", "NAME");
148 printf("%-8s %-12s ", "LINE", "TIME");
151 printf("%-16s\n", "FROM");
155 row(struct utmpx *ut)
157 char buf[80], tty[PATH_MAX];
160 static int d_first = -1;
165 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
169 if (Tflag || uflag) {
170 snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
171 if (stat(tty, &sb) == 0) {
172 state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
174 idle = time(NULL) - sb.st_mtime;
178 printf("%-16s ", ut->ut_user);
180 printf("%c ", state);
181 printf("%-8s ", ut->ut_line);
182 t = ut->ut_tv.tv_sec;
184 strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
185 printf("%-*s ", 12, buf);
189 else if (idle < 24 * 60 * 60)
190 printf("%02d:%02d ", (int)(idle / 60 / 60),
191 (int)(idle / 60 % 60));
195 if (*ut->ut_host != '\0')
196 printf("(%s)", ut->ut_host);
204 char ttybuf[MAXPATHLEN];
206 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
207 if (stat(ttybuf, &sb) == 0) {
218 while ((utx = getutxent()) != NULL) {
219 if (utx->ut_type != USER_PROCESS)
221 if (ttystat(utx->ut_line) != 0)
235 while ((utx = getutxent()) != NULL) {
236 if (utx->ut_type != USER_PROCESS)
238 printf("%-16s", utx->ut_user);
239 if (++col < ncols / (16 + 1))
250 printf("# users = %d\n", num);
256 struct utmpx ut, *utx;
258 const char *name, *tty;
260 if ((tty = ttyname(STDIN_FILENO)) == NULL)
262 else if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
263 tty += sizeof _PATH_DEV - 1;
264 strlcpy(ut.ut_line, tty, sizeof ut.ut_line);
266 /* Search utmp for our tty, dump first matching record. */
267 if ((utx = getutxline(&ut)) != NULL && utx->ut_type == USER_PROCESS) {
272 /* Not found; fill the utmp structure with the information we have. */
273 memset(&ut, 0, sizeof(ut));
274 if ((pwd = getpwuid(getuid())) != NULL)
278 strlcpy(ut.ut_user, name, sizeof ut.ut_user);
279 gettimeofday(&ut.ut_tv, NULL);
290 if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
292 width = strtol(cols, &ep, 10);
293 if (errno || width <= 0 || width > INT_MAX || ep == cols ||
295 warnx("invalid COLUMNS environment variable ignored");
299 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)