2 Copyright (C) 1992-2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by jla; revised by djm; revised again by mstone */
21 name [state] line time [activity] [pid] [comment] [exit]
23 name, line, time: not -q
31 #include <sys/types.h>
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "who"
41 #define AUTHORS N_ ("Joseph Arceneaux, David MacKenzie, and Michael Stone")
43 #ifndef MAXHOSTNAMELEN
44 # define MAXHOSTNAMELEN 64
52 # define USER_PROCESS INT_MAX
56 # define RUN_LVL INT_MAX
60 # define INIT_PROCESS INT_MAX
64 # define LOGIN_PROCESS INT_MAX
68 # define DEAD_PROCESS INT_MAX
72 # define NEW_TIME INT_MAX
81 /* The name this program was run with. */
84 /* If nonzero, attempt to canonicalize hostnames via a DNS lookup. */
87 /* If nonzero, display only a list of usernames and count of
89 Ignored for `who am i'. */
90 static int short_list;
92 /* If nonzero, display only name, line, and time fields */
93 static int short_output;
95 /* If nonzero, display the hours:minutes since each user has touched
96 the keyboard, or "." if within the last minute, or "old" if
97 not within the last day. */
98 static int include_idle;
100 /* If nonzero, display a line at the top describing each field. */
101 static int include_heading;
103 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
104 or a `?' if their tty cannot be statted. */
105 static int include_mesg;
107 /* If nonzero, display process termination & exit status */
108 static int include_exit;
110 /* If nonzero, display the last boot time */
111 static int need_boottime;
113 /* If nonzero, display dead processes */
114 static int need_deadprocs;
116 /* If nonzero, display processes waiting for user login */
117 static int need_login;
119 /* If nonzero, display processes started by init */
120 static int need_initspawn;
122 /* If nonzero, display the last clock change */
123 static int need_clockchange;
125 /* If nonzero, display the current runlevel */
126 static int need_runlevel;
128 /* If nonzero, display user processes */
129 static int need_users;
131 /* If nonzero, display info only for the controlling tty */
132 static int my_line_only;
134 /* for long options with no corresponding short option, use enum */
137 LOOKUP_OPTION = CHAR_MAX + 1,
141 static struct option const longopts[] = {
142 {"all", no_argument, NULL, 'a'},
143 {"boot", no_argument, NULL, 'b'},
144 {"count", no_argument, NULL, 'q'},
145 {"dead", no_argument, NULL, 'd'},
146 {"heading", no_argument, NULL, 'H'},
147 {"idle", no_argument, NULL, 'i'},
148 {"login", no_argument, NULL, LOGIN_OPTION},
149 {"lookup", no_argument, NULL, LOOKUP_OPTION},
150 {"message", no_argument, NULL, 'T'},
151 {"mesg", no_argument, NULL, 'T'},
152 {"process", no_argument, NULL, 'p'},
153 {"runlevel", no_argument, NULL, 'r'},
154 {"short", no_argument, NULL, 's'},
155 {"time", no_argument, NULL, 't'},
156 {"users", no_argument, NULL, 'u'},
157 {"writable", no_argument, NULL, 'T'},
158 {GETOPT_HELP_OPTION_DECL},
159 {GETOPT_VERSION_OPTION_DECL},
163 /* Return a string representing the time between WHEN and the time
164 that this function is first run.
167 idle_string (time_t when)
169 static time_t now = 0;
170 static char idle_hhmm[IDLESTR_LEN];
176 seconds_idle = now - when;
177 if (seconds_idle < 60) /* One minute. */
179 if (seconds_idle < (24 * 60 * 60)) /* One day. */
181 sprintf (idle_hhmm, "%02d:%02d",
182 (int) (seconds_idle / (60 * 60)),
183 (int) ((seconds_idle % (60 * 60)) / 60));
184 return (const char *) idle_hhmm;
189 /* Return a standard time string, "mon dd hh:mm"
190 FIXME: handle localization */
192 time_string (const STRUCT_UTMP *utmp_ent)
194 /* Don't take the address of UT_TIME_MEMBER directly.
195 Ulrich Drepper wrote:
196 ``... GNU libc (and perhaps other libcs as well) have extended
197 utmp file formats which do not use a simple time_t ut_time field.
198 In glibc, ut_time is a macro which selects for backward compatibility
199 the tv_sec member of a struct timeval value.'' */
200 time_t tm = UT_TIME_MEMBER (utmp_ent);
202 char *ptr = ctime (&tm) + 4;
207 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
208 will need tweaking if any of the localization stuff is done, or for 64 bit
211 print_line (const char *user, const char state, const char *line,
212 const char *time_str, const char *idle, const char *pid,
213 const char *comment, const char *exitstr)
215 printf ("%-8s", user ? user : " .");
217 printf (" %c", state);
218 printf (" %-12s", line);
219 printf (" %-12s", time_str);
220 if (include_idle && !short_output)
221 printf (" %-6s", idle);
223 printf (" %10s", pid);
224 /* FIXME: it's not really clear whether the following should be in short_output.
225 a strict reading of SUSv2 would suggest not, but I haven't seen any
226 implementations that actually work that way... */
227 printf (" %-8s", comment);
228 if (include_exit && exitstr && *exitstr)
229 printf (" %-12s", exitstr);
233 #if HAVE_STRUCT_XTMP_UT_PID
234 # define PIDSTR_DECL_AND_INIT(Var) \
235 char Var[INT_STRLEN_BOUND (utmp_ent->ut_pid) + 1]; \
236 sprintf (Var, "%d", utmp_ent->ut_pid)
238 # define PIDSTR_DECL_AND_INIT(Var) \
242 /* Send properly parsed USER_PROCESS info to print_line */
244 print_user (const STRUCT_UTMP *utmp_ent)
249 char idlestr[IDLESTR_LEN];
250 static char *hoststr;
253 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
254 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
256 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
257 PIDSTR_DECL_AND_INIT (pidstr);
259 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
260 already an absolute pathname. Some system may put the full,
261 absolute pathname in ut_line. */
262 if (utmp_ent->ut_line[0] == '/')
264 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
265 line[sizeof (utmp_ent->ut_line)] = '\0';
269 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
270 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
271 sizeof (utmp_ent->ut_line));
272 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
275 if (stat (line, &stats) == 0)
277 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
278 last_change = stats.st_atime;
287 sprintf (idlestr, "%.6s", idle_string (last_change));
289 sprintf (idlestr, " ?");
292 if (utmp_ent->ut_host[0])
294 char ut_host[sizeof (utmp_ent->ut_host) + 1];
295 char *host = 0, *display = 0;
297 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
298 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
299 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
301 /* Look for an X display. */
302 display = strrchr (ut_host, ':');
306 if (*ut_host && do_lookup)
308 /* See if we can canonicalize it. */
309 host = canon_host (ut_host);
317 if (hostlen < strlen (host) + strlen (display) + 4)
319 hostlen = strlen (host) + strlen (display) + 4;
320 hoststr = (char *) realloc (hoststr, hostlen);
322 sprintf (hoststr, "(%s:%s)", host, display);
326 if (hostlen < strlen (host) + 3)
328 hostlen = strlen (host) + 3;
329 hoststr = (char *) realloc (hoststr, hostlen);
331 sprintf (hoststr, "(%s)", host);
339 hoststr = (char *) realloc (hoststr, hostlen);
341 stpcpy (hoststr, "");
345 print_line (UT_USER (utmp_ent), mesg, utmp_ent->ut_line,
346 time_string (utmp_ent), idlestr, pidstr,
347 hoststr ? hoststr : "", "");
351 print_boottime (const STRUCT_UTMP *utmp_ent)
353 print_line ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
357 print_deadprocs (const STRUCT_UTMP *utmp_ent)
359 static char *comment, *exitstr;
360 PIDSTR_DECL_AND_INIT (pidstr);
364 (char *) malloc (sizeof (_("id=")) + sizeof (utmp_ent->ut_id) + 1);
365 sprintf (comment, "%s%.*s", _("id="), sizeof utmp_ent->ut_id,
369 exitstr = (char *) malloc (sizeof (_("term="))
370 + INT_STRLEN_BOUND (utmp_ent->ut_exit.e_termination) + 1
371 + sizeof (_("exit="))
372 + INT_STRLEN_BOUND (utmp_ent->ut_exit.e_exit)
374 sprintf (exitstr, "%s%d %s%d", _("term="), utmp_ent->ut_exit.e_termination,
375 _("exit="), utmp_ent->ut_exit.e_exit);
377 /* FIXME: add idle time? */
379 print_line ("", ' ', utmp_ent->ut_line,
380 time_string (utmp_ent), "", pidstr, comment, exitstr);
384 print_login (const STRUCT_UTMP *utmp_ent)
386 static char *comment;
387 PIDSTR_DECL_AND_INIT (pidstr);
391 (char *) malloc (sizeof (_("id=")) + sizeof (utmp_ent->ut_id) + 1);
392 sprintf (comment, "%s%s", _("id="), utmp_ent->ut_id);
394 /* FIXME: add idle time? */
396 print_line ("LOGIN", ' ', utmp_ent->ut_line,
397 time_string (utmp_ent), "", pidstr, comment, "");
401 print_initspawn (const STRUCT_UTMP *utmp_ent)
403 static char *comment;
404 PIDSTR_DECL_AND_INIT (pidstr);
408 (char *) malloc (sizeof (_("id=")) + sizeof (utmp_ent->ut_id) + 1);
409 sprintf (comment, "%s%s", _("id="), utmp_ent->ut_id);
411 print_line ("", ' ', utmp_ent->ut_line,
412 time_string (utmp_ent), "", pidstr, comment, "");
416 print_clockchange (const STRUCT_UTMP *utmp_ent)
418 /* FIXME: handle NEW_TIME & OLD_TIME both */
419 print_line ("", ' ', _("clock change"),
420 time_string (utmp_ent), "", "", "", "");
424 print_runlevel (const STRUCT_UTMP *utmp_ent)
426 static char *runlevline, *comment;
428 /* FIXME: The following is correct for linux, may need help
429 on other platforms */
430 #if 1 || HAVE_STRUCT_XTMP_UT_PID
431 int last = utmp_ent->ut_pid / 256;
432 int curr = utmp_ent->ut_pid % 256;
436 runlevline = (char *) malloc (sizeof (_("run-level")) + 3);
437 sprintf (runlevline, "%s %c", _("run-level"), curr);
440 comment = (char *) malloc (sizeof (_("last=")) + 2);
441 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
443 print_line ("", ' ', runlevline, time_string (utmp_ent),
444 "", "", comment, "");
449 /* Print the username of each valid entry and the number of valid entries
450 in UTMP_BUF, which should have N elements. */
452 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
458 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
462 trimmed_name = extract_trimmed_name (utmp_buf);
464 printf ("%s ", trimmed_name);
470 printf (_("\n# users=%u\n"), entries);
476 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
477 _("COMMENT"), _("EXIT"));
480 /* Display UTMP_BUF, which should have N entries. */
482 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
484 char *ttyname_b IF_LINT ( = NULL);
491 ttyname_b = ttyname (0);
494 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
495 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
501 strncmp (ttyname_b, utmp_buf->ut_line,
502 sizeof (utmp_buf->ut_line)) == 0)
504 if (need_users && UT_USER (utmp_buf)[0]
505 && UT_TYPE (utmp_buf) == USER_PROCESS)
506 print_user (utmp_buf);
507 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
508 print_runlevel (utmp_buf);
509 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
510 print_boottime (utmp_buf);
511 /* I've never seen one of these, so I don't know what it should
513 FIXME: handle OLD_TIME also, perhaps show the delta? */
514 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
515 print_clockchange (utmp_buf);
516 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
517 print_initspawn (utmp_buf);
518 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
519 print_login (utmp_buf);
520 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
521 print_deadprocs (utmp_buf);
528 /* Display a list of who is on the system, according to utmp file filename. */
530 who (const char *filename)
533 STRUCT_UTMP *utmp_buf;
534 int fail = read_utmp (filename, &n_users, &utmp_buf);
537 error (1, errno, "%s", filename);
540 list_entries_who (n_users, utmp_buf);
542 scan_entries (n_users, utmp_buf);
549 fprintf (stderr, _("Try `%s --help' for more information.\n"),
553 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
556 -a, --all same as -b -d --login -p -r -t -T -u\n\
557 -b, --boot time of last system boot\n\
558 -d, --dead print dead processes\n\
559 -H, --heading print line of column headings\n\
562 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
563 (deprecated, use -u)\n\
564 --login print system login processes\n\
565 (equivalent to SUS -l)\n\
568 -l, --lookup attempt to canonicalize hostnames via DNS\n\
569 (-l is deprecated, use --lookup)\n\
570 -m only hostname and user associated with stdin\n\
571 -p, --process print active processes spawned by init\n\
574 -q, --count all login names and number of users logged on\n\
575 -r, --runlevel print current runlevel\n\
576 -s, --short print only name, line, and time (default)\n\
577 -t, --time print last system clock change\n\
580 -T, -w, --mesg add user's message status as +, - or ?\n\
581 -u, --users lists users logged in\n\
582 --message same as -T\n\
583 --writable same as -T\n\
585 fputs (HELP_OPTION_DESCRIPTION, stdout);
586 fputs (VERSION_OPTION_DESCRIPTION, stdout);
589 If FILE is not specified, use %s. %s as FILE is common.\n\
590 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
591 "), UTMP_FILE, WTMP_FILE);
592 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
598 main (int argc, char **argv)
603 program_name = argv[0];
604 setlocale (LC_ALL, "");
605 bindtextdomain (PACKAGE, LOCALEDIR);
606 textdomain (PACKAGE);
608 atexit (close_stdout);
610 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts,
624 need_clockchange = 1;
648 /* FIXME: This should be -l in a future version */
679 need_clockchange = 1;
690 _("Warning: -i will be removed in a future release; \
701 _("Warning: the meaning of '-l' will change in a future\
702 release to conform to POSIX"));
707 case_GETOPT_HELP_CHAR;
709 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
727 switch (argc - optind)
733 case 1: /* who <utmp file> */
737 case 2: /* who <blurf> <glop> */
743 error (0, 0, _("too many arguments"));