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