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