w: new applet, alias to "who -H"
[oweals/busybox.git] / coreutils / who.c
1 /* vi: set sw=4 ts=4: */
2 /*----------------------------------------------------------------------
3  * Mini who is used to display user name, login time,
4  * idle time and host name.
5  *
6  * Author: Da Chen  <dchen@ayrnetworks.com>
7  *
8  * This is a free document; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation:
11  *    http://www.gnu.org/copyleft/gpl.html
12  *
13  * Copyright (c) 2002 AYR Networks, Inc.
14  *
15  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
16  *
17  *----------------------------------------------------------------------
18  */
19 //config:config WHO
20 //config:      bool "who"
21 //config:      default y
22 //config:      depends on FEATURE_UTMP
23 //config:      help
24 //config:        who is used to show who is logged on.
25 //config:
26 // procps-ng has this variation of "who":
27 //config:config W
28 //config:      bool "w"
29 //config:      default y
30 //config:      depends on FEATURE_UTMP
31 //config:      help
32 //config:        w is used to show who is logged on.
33 //config:
34 //config:config USERS
35 //config:      bool "users"
36 //config:      default y
37 //config:      depends on FEATURE_UTMP
38 //config:      help
39 //config:        Print users currently logged on.
40
41 //                APPLET_ODDNAME:name   main location        suid_type     help
42 //applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
43 //applet:IF_USERS(APPLET_ODDNAME(w,     who, BB_DIR_USR_BIN, BB_SUID_DROP, w))
44 //applet:IF_WHO(  APPLET(        who,        BB_DIR_USR_BIN, BB_SUID_DROP))
45
46 //kbuild:lib-$(CONFIG_USERS) += who.o
47 //kbuild:lib-$(CONFIG_WHO) += who.o
48
49 /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'.  */
50
51 //usage:#define users_trivial_usage
52 //usage:       ""
53 //usage:#define users_full_usage "\n\n"
54 //usage:       "Print the users currently logged on"
55
56 //usage:#define w_trivial_usage
57 //usage:       ""
58 //usage:#define w_full_usage "\n\n"
59 //usage:       "Show who is logged on"
60 //
61 // procps-ng 3.3.10:
62 //           "\n        -h, --no-header"
63 //           "\n        -u, --no-current"
64 //      Ignores the username while figuring out the current process
65 //      and cpu times.  To demonstrate this, do a "su" and do a "w" and a "w -u".
66 //           "\n        -s, --short"
67 //      Short format.  Don't print the login time, JCPU or PCPU times.
68 //           "\n        -f, --from"
69 //      Toggle printing the from (remote hostname) field.
70 //      The default is for the from field to not be printed
71 //           "\n        -i, --ip-addr"
72 //      Display IP address instead of hostname for from field.
73 //           "\n        -o, --old-style"
74 //      Old style output. Prints blank space for idle times less than one minute.
75 // Example output:
76 //  17:28:00 up 4 days, 22:41,  4 users,  load average: 0.84, 0.97, 0.90
77 // USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
78 // root     tty1      Thu18    4days  4:33m  0.07s /bin/sh /etc/xdg/xfce4/xinitrc -- vt
79 // root     pts/1     Mon13    3:24m  1:01   0.01s w
80
81 //usage:#define who_trivial_usage
82 //usage:       "[-a]"
83 //usage:#define who_full_usage "\n\n"
84 //usage:       "Show who is logged on\n"
85 //usage:     "\n        -a      Show all"
86 //usage:     "\n        -H      Print column headers"
87
88 #include "libbb.h"
89
90 static void idle_string(char *str6, time_t t)
91 {
92         t = time(NULL) - t;
93
94         /*if (t < 60) {
95                 str6[0] = '.';
96                 str6[1] = '\0';
97                 return;
98         }*/
99         if (t >= 0 && t < (24 * 60 * 60)) {
100                 sprintf(str6, "%02d:%02d",
101                                 (int) (t / (60 * 60)),
102                                 (int) ((t % (60 * 60)) / 60));
103                 return;
104         }
105         strcpy(str6, "old");
106 }
107
108 int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
109 int who_main(int argc UNUSED_PARAM, char **argv)
110 {
111 #define CNT_APPLET (ENABLE_USERS + ENABLE_W + ENABLE_WHO)
112         int do_users = (ENABLE_USERS && (CNT_APPLET == 1 || applet_name[0] == 'u'));
113         int do_w     = (ENABLE_W     && (CNT_APPLET == 1 || applet_name[1] == '\0'));
114         int do_who   = (ENABLE_WHO   && (CNT_APPLET == 1 || applet_name[1] == 'h'));
115         struct utmpx *ut;
116         unsigned opt;
117         const char *fmt = "%s";
118
119         opt_complementary = "=0";
120         opt = getopt32(argv, do_who ? "aH" : "");
121         if ((opt & 2) || do_w) /* -H or we are w */
122                 puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
123
124         setutxent();
125         while ((ut = getutxent()) != NULL) {
126                 if (ut->ut_user[0]
127                  && ((opt & 1) || ut->ut_type == USER_PROCESS)
128                 ) {
129                         if (!do_users) {
130                                 char str6[6];
131                                 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
132                                 struct stat st;
133                                 time_t seconds;
134
135                                 str6[0] = '?';
136                                 str6[1] = '\0';
137                                 strcpy(name, "/dev/");
138                                 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
139                                         ut->ut_line,
140                                         sizeof(ut->ut_line)+1
141                                 );
142                                 if (stat(name, &st) == 0)
143                                         idle_string(str6, st.st_atime);
144                                 /* manpages say ut_tv.tv_sec *is* time_t,
145                                  * but some systems have it wrong */
146                                 seconds = ut->ut_tv.tv_sec;
147                                 /* How wide time field can be?
148                                  * "Nov 10 19:33:20": 15 chars
149                                  * "2010-11-10 19:33": 16 chars
150                                  */
151                                 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
152                                                 (int)sizeof(ut->ut_user), ut->ut_user,
153                                                 (int)sizeof(ut->ut_line), ut->ut_line,
154                                                 str6,
155 // TODO: with LANG=en_US.UTF-8, who from coreutils 8.25 shows
156 // TIME col as "2017-04-06 18:47" (the default format is "Apr  6 18:47").
157 // The former format looks saner to me. Switch to it unconditionally?
158                                                 ctime(&seconds) + 4,
159                                                 (int)sizeof(ut->ut_host), ut->ut_host
160                                 );
161                         } else {
162                                 printf(fmt, ut->ut_user);
163                                 fmt = " %s";
164                         }
165                 }
166         }
167         if (do_users)
168                 bb_putchar('\n');
169         if (ENABLE_FEATURE_CLEAN_UP)
170                 endutxent();
171         return EXIT_SUCCESS;
172 }