dd: clarify meaning of multiplication factors; put xM in order
[platform/upstream/coreutils.git] / src / who.c
1 /* GNU's who.
2    Copyright (C) 1992-2008 Free Software Foundation, Inc.
3
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 3 of the License, or
7    (at your option) any later version.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by jla; revised by djm; revised again by mstone */
18
19 /* Output format:
20    name [state] line time [activity] [pid] [comment] [exit]
21    state: -T
22    name, line, time: not -q
23    idle: -u
24 */
25
26 #include <config.h>
27 #include <getopt.h>
28 #include <stdio.h>
29
30 #include <sys/types.h>
31 #include "system.h"
32
33 #include "c-ctype.h"
34 #include "canon-host.h"
35 #include "readutmp.h"
36 #include "error.h"
37 #include "hard-locale.h"
38 #include "quote.h"
39
40 /* The official name of this program (e.g., no `g' prefix).  */
41 #define PROGRAM_NAME "who"
42
43 #define AUTHORS \
44   proper_name ("Joseph Arceneaux"), \
45   proper_name ("David MacKenzie"), \
46   proper_name ("Michael Stone")
47
48 #ifndef MAXHOSTNAMELEN
49 # define MAXHOSTNAMELEN 64
50 #endif
51
52 #ifdef RUN_LVL
53 # define UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
54 #else
55 # define UT_TYPE_RUN_LVL(U) false
56 #endif
57
58 #ifdef INIT_PROCESS
59 # define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
60 #else
61 # define UT_TYPE_INIT_PROCESS(U) false
62 #endif
63
64 #ifdef LOGIN_PROCESS
65 # define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
66 #else
67 # define UT_TYPE_LOGIN_PROCESS(U) false
68 #endif
69
70 #ifdef DEAD_PROCESS
71 # define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
72 #else
73 # define UT_TYPE_DEAD_PROCESS(U) false
74 #endif
75
76 #ifdef NEW_TIME
77 # define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
78 #else
79 # define UT_TYPE_NEW_TIME(U) false
80 #endif
81
82 #define IDLESTR_LEN 6
83
84 #if HAVE_STRUCT_XTMP_UT_PID
85 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
86   char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
87   sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
88 #else
89 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
90   const char *Var = ""
91 #endif
92
93 #if HAVE_STRUCT_XTMP_UT_ID
94 # define UT_ID(U) ((U)->ut_id)
95 #else
96 # define UT_ID(U) "??"
97 #endif
98
99 char *ttyname ();
100
101 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
102 static bool do_lookup;
103
104 /* If true, display only a list of usernames and count of
105    the users logged on.
106    Ignored for `who am i'.  */
107 static bool short_list;
108
109 /* If true, display only name, line, and time fields.  */
110 static bool short_output;
111
112 /* If true, display the hours:minutes since each user has touched
113    the keyboard, or "." if within the last minute, or "old" if
114    not within the last day.  */
115 static bool include_idle;
116
117 /* If true, display a line at the top describing each field.  */
118 static bool include_heading;
119
120 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
121    or a `?' if their tty cannot be statted. */
122 static bool include_mesg;
123
124 /* If true, display process termination & exit status.  */
125 static bool include_exit;
126
127 /* If true, display the last boot time.  */
128 static bool need_boottime;
129
130 /* If true, display dead processes.  */
131 static bool need_deadprocs;
132
133 /* If true, display processes waiting for user login.  */
134 static bool need_login;
135
136 /* If true, display processes started by init.  */
137 static bool need_initspawn;
138
139 /* If true, display the last clock change.  */
140 static bool need_clockchange;
141
142 /* If true, display the current runlevel.  */
143 static bool need_runlevel;
144
145 /* If true, display user processes.  */
146 static bool need_users;
147
148 /* If true, display info only for the controlling tty.  */
149 static bool my_line_only;
150
151 /* The strftime format to use for login times, and its expected
152    output width.  */
153 static char const *time_format;
154 static int time_format_width;
155
156 /* for long options with no corresponding short option, use enum */
157 enum
158 {
159   LOOKUP_OPTION = CHAR_MAX + 1
160 };
161
162 static struct option const longopts[] =
163 {
164   {"all", no_argument, NULL, 'a'},
165   {"boot", no_argument, NULL, 'b'},
166   {"count", no_argument, NULL, 'q'},
167   {"dead", no_argument, NULL, 'd'},
168   {"heading", no_argument, NULL, 'H'},
169   {"login", no_argument, NULL, 'l'},
170   {"lookup", no_argument, NULL, LOOKUP_OPTION},
171   {"message", no_argument, NULL, 'T'},
172   {"mesg", no_argument, NULL, 'T'},
173   {"process", no_argument, NULL, 'p'},
174   {"runlevel", no_argument, NULL, 'r'},
175   {"short", no_argument, NULL, 's'},
176   {"time", no_argument, NULL, 't'},
177   {"users", no_argument, NULL, 'u'},
178   {"writable", no_argument, NULL, 'T'},
179   {GETOPT_HELP_OPTION_DECL},
180   {GETOPT_VERSION_OPTION_DECL},
181   {NULL, 0, NULL, 0}
182 };
183
184 /* Return a string representing the time between WHEN and now.
185    BOOTTIME is the time of last reboot.
186    FIXME: locale? */
187 static const char *
188 idle_string (time_t when, time_t boottime)
189 {
190   static time_t now = TYPE_MINIMUM (time_t);
191
192   if (now == TYPE_MINIMUM (time_t))
193     time (&now);
194
195   if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
196     {
197       int seconds_idle = now - when;
198       if (seconds_idle < 60)
199         return "  .  ";
200       else
201         {
202           static char idle_hhmm[IDLESTR_LEN];
203           sprintf (idle_hhmm, "%02d:%02d",
204                    seconds_idle / (60 * 60),
205                    (seconds_idle % (60 * 60)) / 60);
206           return idle_hhmm;
207         }
208     }
209
210   return _(" old ");
211 }
212
213 /* Return a time string.  */
214 static const char *
215 time_string (const STRUCT_UTMP *utmp_ent)
216 {
217   static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
218
219   /* Don't take the address of UT_TIME_MEMBER directly.
220      Ulrich Drepper wrote:
221      ``... GNU libc (and perhaps other libcs as well) have extended
222      utmp file formats which do not use a simple time_t ut_time field.
223      In glibc, ut_time is a macro which selects for backward compatibility
224      the tv_sec member of a struct timeval value.''  */
225   time_t t = UT_TIME_MEMBER (utmp_ent);
226   struct tm *tmp = localtime (&t);
227
228   if (tmp)
229     {
230       strftime (buf, sizeof buf, time_format, tmp);
231       return buf;
232     }
233   else
234     return timetostr (t, buf);
235 }
236
237 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
238    will need tweaking if any of the localization stuff is done, or for 64 bit
239    pids, etc. */
240 static void
241 print_line (int userlen, const char *user, const char state,
242             int linelen, const char *line,
243             const char *time_str, const char *idle, const char *pid,
244             const char *comment, const char *exitstr)
245 {
246   static char mesg[3] = { ' ', 'x', '\0' };
247   char *buf;
248   char x_idle[1 + IDLESTR_LEN + 1];
249   char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
250   char *x_exitstr;
251   int err;
252
253   mesg[1] = state;
254
255   if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
256     sprintf (x_idle, " %-6s", idle);
257   else
258     *x_idle = '\0';
259
260   if (!short_output && strlen (pid) < sizeof x_pid - 1)
261     sprintf (x_pid, " %10s", pid);
262   else
263     *x_pid = '\0';
264
265   x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
266   if (include_exit)
267     sprintf (x_exitstr, " %-12s", exitstr);
268   else
269     *x_exitstr = '\0';
270
271   err = asprintf (&buf,
272                   "%-8.*s"
273                   "%s"
274                   " %-12.*s"
275                   " %-*s"
276                   "%s"
277                   "%s"
278                   " %-8s"
279                   "%s"
280                   ,
281                   userlen, user ? user : "   .",
282                   include_mesg ? mesg : "",
283                   linelen, line,
284                   time_format_width,
285                   time_str,
286                   x_idle,
287                   x_pid,
288                   /* FIXME: it's not really clear whether the following
289                      field should be in the short_output.  A strict reading
290                      of SUSv2 would suggest not, but I haven't seen any
291                      implementations that actually work that way... */
292                   comment,
293                   x_exitstr
294                   );
295   if (err == -1)
296     xalloc_die ();
297
298   {
299     /* Remove any trailing spaces.  */
300     char *p = buf + strlen (buf);
301     while (*--p == ' ')
302       /* empty */;
303     *(p + 1) = '\0';
304   }
305
306   puts (buf);
307   free (buf);
308   free (x_exitstr);
309 }
310
311 /* Send properly parsed USER_PROCESS info to print_line.  The most
312    recent boot time is BOOTTIME. */
313 static void
314 print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
315 {
316   struct stat stats;
317   time_t last_change;
318   char mesg;
319   char idlestr[IDLESTR_LEN + 1];
320   static char *hoststr;
321 #if HAVE_UT_HOST
322   static size_t hostlen;
323 #endif
324
325 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
326 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
327
328   char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
329   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
330
331   /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
332      already an absolute file name.  Some systems may put the full,
333      absolute file name in ut_line.  */
334   if (utmp_ent->ut_line[0] == '/')
335     {
336       strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
337       line[sizeof (utmp_ent->ut_line)] = '\0';
338     }
339   else
340     {
341       strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
342       strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
343                sizeof (utmp_ent->ut_line));
344       line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
345     }
346
347   if (stat (line, &stats) == 0)
348     {
349       mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
350       last_change = stats.st_atime;
351     }
352   else
353     {
354       mesg = '?';
355       last_change = 0;
356     }
357
358   if (last_change)
359     sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
360   else
361     sprintf (idlestr, "  ?");
362
363 #if HAVE_UT_HOST
364   if (utmp_ent->ut_host[0])
365     {
366       char ut_host[sizeof (utmp_ent->ut_host) + 1];
367       char *host = NULL;
368       char *display = NULL;
369
370       /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
371       strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
372       ut_host[sizeof (utmp_ent->ut_host)] = '\0';
373
374       /* Look for an X display.  */
375       display = strchr (ut_host, ':');
376       if (display)
377         *display++ = '\0';
378
379       if (*ut_host && do_lookup)
380         {
381           /* See if we can canonicalize it.  */
382           host = canon_host (ut_host);
383         }
384
385       if (! host)
386         host = ut_host;
387
388       if (display)
389         {
390           if (hostlen < strlen (host) + strlen (display) + 4)
391             {
392               hostlen = strlen (host) + strlen (display) + 4;
393               hoststr = xrealloc (hoststr, hostlen);
394             }
395           sprintf (hoststr, "(%s:%s)", host, display);
396         }
397       else
398         {
399           if (hostlen < strlen (host) + 3)
400             {
401               hostlen = strlen (host) + 3;
402               hoststr = xrealloc (hoststr, hostlen);
403             }
404           sprintf (hoststr, "(%s)", host);
405         }
406
407       if (host != ut_host)
408         free (host);
409     }
410   else
411     {
412       if (hostlen < 1)
413         {
414           hostlen = 1;
415           hoststr = xrealloc (hoststr, hostlen);
416         }
417       *hoststr = '\0';
418     }
419 #endif
420
421   print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg,
422               sizeof utmp_ent->ut_line, utmp_ent->ut_line,
423               time_string (utmp_ent), idlestr, pidstr,
424               hoststr ? hoststr : "", "");
425 }
426
427 static void
428 print_boottime (const STRUCT_UTMP *utmp_ent)
429 {
430   print_line (-1, "", ' ', -1, _("system boot"),
431               time_string (utmp_ent), "", "", "", "");
432 }
433
434 static char *
435 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
436 {
437   char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
438
439   strcpy (comment, _("id="));
440   strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
441   return comment;
442 }
443
444 static void
445 print_deadprocs (const STRUCT_UTMP *utmp_ent)
446 {
447   static char *exitstr;
448   char *comment = make_id_equals_comment (utmp_ent);
449   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
450
451   if (!exitstr)
452     exitstr = xmalloc (strlen (_("term="))
453                        + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
454                        + strlen (_("exit="))
455                        + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
456                        + 1);
457   sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
458            _("exit="), UT_EXIT_E_EXIT (utmp_ent));
459
460   /* FIXME: add idle time? */
461
462   print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
463               time_string (utmp_ent), "", pidstr, comment, exitstr);
464   free (comment);
465 }
466
467 static void
468 print_login (const STRUCT_UTMP *utmp_ent)
469 {
470   char *comment = make_id_equals_comment (utmp_ent);
471   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
472
473   /* FIXME: add idle time? */
474
475   print_line (-1, _("LOGIN"), ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
476               time_string (utmp_ent), "", pidstr, comment, "");
477   free (comment);
478 }
479
480 static void
481 print_initspawn (const STRUCT_UTMP *utmp_ent)
482 {
483   char *comment = make_id_equals_comment (utmp_ent);
484   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
485
486   print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
487               time_string (utmp_ent), "", pidstr, comment, "");
488   free (comment);
489 }
490
491 static void
492 print_clockchange (const STRUCT_UTMP *utmp_ent)
493 {
494   /* FIXME: handle NEW_TIME & OLD_TIME both */
495   print_line (-1, "", ' ', -1, _("clock change"),
496               time_string (utmp_ent), "", "", "", "");
497 }
498
499 static void
500 print_runlevel (const STRUCT_UTMP *utmp_ent)
501 {
502   static char *runlevline, *comment;
503   unsigned char last = UT_PID (utmp_ent) / 256;
504   unsigned char curr = UT_PID (utmp_ent) % 256;
505
506   if (!runlevline)
507     runlevline = xmalloc (strlen (_("run-level")) + 3);
508   sprintf (runlevline, "%s %c", _("run-level"), curr);
509
510   if (!comment)
511     comment = xmalloc (strlen (_("last=")) + 2);
512   sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
513
514   print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent),
515               "", "", c_isprint (last) ? comment : "", "");
516
517   return;
518 }
519
520 /* Print the username of each valid entry and the number of valid entries
521    in UTMP_BUF, which should have N elements. */
522 static void
523 list_entries_who (size_t n, const STRUCT_UTMP *utmp_buf)
524 {
525   unsigned long int entries = 0;
526   char const *separator = "";
527
528   while (n--)
529     {
530       if (IS_USER_PROCESS (utmp_buf))
531         {
532           char *trimmed_name;
533
534           trimmed_name = extract_trimmed_name (utmp_buf);
535
536           printf ("%s%s", separator, trimmed_name);
537           free (trimmed_name);
538           separator = " ";
539           entries++;
540         }
541       utmp_buf++;
542     }
543   printf (_("\n# users=%lu\n"), entries);
544 }
545
546 static void
547 print_heading (void)
548 {
549   print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
550               _("PID"), _("COMMENT"), _("EXIT"));
551 }
552
553 /* Display UTMP_BUF, which should have N entries. */
554 static void
555 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
556 {
557   char *ttyname_b IF_LINT ( = NULL);
558   time_t boottime = TYPE_MINIMUM (time_t);
559
560   if (include_heading)
561     print_heading ();
562
563   if (my_line_only)
564     {
565       ttyname_b = ttyname (STDIN_FILENO);
566       if (!ttyname_b)
567         return;
568       if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
569         ttyname_b += DEV_DIR_LEN;       /* Discard /dev/ prefix.  */
570     }
571
572   while (n--)
573     {
574       if (!my_line_only ||
575           strncmp (ttyname_b, utmp_buf->ut_line,
576                    sizeof (utmp_buf->ut_line)) == 0)
577         {
578           if (need_users && IS_USER_PROCESS (utmp_buf))
579             print_user (utmp_buf, boottime);
580           else if (need_runlevel && UT_TYPE_RUN_LVL (utmp_buf))
581             print_runlevel (utmp_buf);
582           else if (need_boottime && UT_TYPE_BOOT_TIME (utmp_buf))
583             print_boottime (utmp_buf);
584           /* I've never seen one of these, so I don't know what it should
585              look like :^)
586              FIXME: handle OLD_TIME also, perhaps show the delta? */
587           else if (need_clockchange && UT_TYPE_NEW_TIME (utmp_buf))
588             print_clockchange (utmp_buf);
589           else if (need_initspawn && UT_TYPE_INIT_PROCESS (utmp_buf))
590             print_initspawn (utmp_buf);
591           else if (need_login && UT_TYPE_LOGIN_PROCESS (utmp_buf))
592             print_login (utmp_buf);
593           else if (need_deadprocs && UT_TYPE_DEAD_PROCESS (utmp_buf))
594             print_deadprocs (utmp_buf);
595         }
596
597       if (UT_TYPE_BOOT_TIME (utmp_buf))
598         boottime = UT_TIME_MEMBER (utmp_buf);
599
600       utmp_buf++;
601     }
602 }
603
604 /* Display a list of who is on the system, according to utmp file FILENAME.
605    Use read_utmp OPTIONS to read the file.  */
606 static void
607 who (const char *filename, int options)
608 {
609   size_t n_users;
610   STRUCT_UTMP *utmp_buf;
611
612   if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
613     error (EXIT_FAILURE, errno, "%s", filename);
614
615   if (short_list)
616     list_entries_who (n_users, utmp_buf);
617   else
618     scan_entries (n_users, utmp_buf);
619
620   free (utmp_buf);
621 }
622
623 void
624 usage (int status)
625 {
626   if (status != EXIT_SUCCESS)
627     fprintf (stderr, _("Try `%s --help' for more information.\n"),
628              program_name);
629   else
630     {
631       printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
632       fputs (_("\
633 \n\
634   -a, --all         same as -b -d --login -p -r -t -T -u\n\
635   -b, --boot        time of last system boot\n\
636   -d, --dead        print dead processes\n\
637   -H, --heading     print line of column headings\n\
638 "), stdout);
639       fputs (_("\
640   -l, --login       print system login processes\n\
641 "), stdout);
642       fputs (_("\
643       --lookup      attempt to canonicalize hostnames via DNS\n\
644   -m                only hostname and user associated with stdin\n\
645   -p, --process     print active processes spawned by init\n\
646 "), stdout);
647       fputs (_("\
648   -q, --count       all login names and number of users logged on\n\
649   -r, --runlevel    print current runlevel\n\
650   -s, --short       print only name, line, and time (default)\n\
651   -t, --time        print last system clock change\n\
652 "), stdout);
653       fputs (_("\
654   -T, -w, --mesg    add user's message status as +, - or ?\n\
655   -u, --users       list users logged in\n\
656       --message     same as -T\n\
657       --writable    same as -T\n\
658 "), stdout);
659       fputs (HELP_OPTION_DESCRIPTION, stdout);
660       fputs (VERSION_OPTION_DESCRIPTION, stdout);
661       printf (_("\
662 \n\
663 If FILE is not specified, use %s.  %s as FILE is common.\n\
664 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
665 "), UTMP_FILE, WTMP_FILE);
666       emit_bug_reporting_address ();
667     }
668   exit (status);
669 }
670
671 int
672 main (int argc, char **argv)
673 {
674   int optc;
675   bool assumptions = true;
676
677   initialize_main (&argc, &argv);
678   set_program_name (argv[0]);
679   setlocale (LC_ALL, "");
680   bindtextdomain (PACKAGE, LOCALEDIR);
681   textdomain (PACKAGE);
682
683   atexit (close_stdout);
684
685   while ((optc = getopt_long (argc, argv, "abdlmpqrstuwHT", longopts, NULL))
686          != -1)
687     {
688       switch (optc)
689         {
690         case 'a':
691           need_boottime = true;
692           need_deadprocs = true;
693           need_login = true;
694           need_initspawn = true;
695           need_runlevel = true;
696           need_clockchange = true;
697           need_users = true;
698           include_mesg = true;
699           include_idle = true;
700           include_exit = true;
701           assumptions = false;
702           break;
703
704         case 'b':
705           need_boottime = true;
706           assumptions = false;
707           break;
708
709         case 'd':
710           need_deadprocs = true;
711           include_idle = true;
712           include_exit = true;
713           assumptions = false;
714           break;
715
716         case 'H':
717           include_heading = true;
718           break;
719
720         case 'l':
721           need_login = true;
722           include_idle = true;
723           assumptions = false;
724           break;
725
726         case 'm':
727           my_line_only = true;
728           break;
729
730         case 'p':
731           need_initspawn = true;
732           assumptions = false;
733           break;
734
735         case 'q':
736           short_list = true;
737           break;
738
739         case 'r':
740           need_runlevel = true;
741           include_idle = true;
742           assumptions = false;
743           break;
744
745         case 's':
746           short_output = true;
747           break;
748
749         case 't':
750           need_clockchange = true;
751           assumptions = false;
752           break;
753
754         case 'T':
755         case 'w':
756           include_mesg = true;
757           break;
758
759         case 'u':
760           need_users = true;
761           include_idle = true;
762           assumptions = false;
763           break;
764
765         case LOOKUP_OPTION:
766           do_lookup = true;
767           break;
768
769           case_GETOPT_HELP_CHAR;
770
771           case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
772
773         default:
774           usage (EXIT_FAILURE);
775         }
776     }
777
778   if (assumptions)
779     {
780       need_users = true;
781       short_output = true;
782     }
783
784   if (include_exit)
785     {
786       short_output = false;
787     }
788
789   if (hard_locale (LC_TIME))
790     {
791       time_format = "%Y-%m-%d %H:%M";
792       time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
793     }
794   else
795     {
796       time_format = "%b %e %H:%M";
797       time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
798     }
799
800   switch (argc - optind)
801     {
802     case 2:                     /* who <blurf> <glop> */
803       my_line_only = true;
804       /* Fall through.  */
805     case -1:
806     case 0:                     /* who */
807       who (UTMP_FILE, READ_UTMP_CHECK_PIDS);
808       break;
809
810     case 1:                     /* who <utmp file> */
811       who (argv[optind], 0);
812       break;
813
814     default:                    /* lose */
815       error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
816       usage (EXIT_FAILURE);
817     }
818
819   exit (EXIT_SUCCESS);
820 }