blob: e6179bb00df791b8c3f38556515a5ad38b6b319b [file] [log] [blame]
Eric Andersen00a6a752002-04-26 23:53:10 +00001/* vi: set sw=4 ts=4: */
2/*----------------------------------------------------------------------
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Mini who is used to display user name, login time,
Eric Andersen00a6a752002-04-26 23:53:10 +00004 * 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 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013 * Copyright (c) 2002 AYR Networks, Inc.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000014 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020015 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000016 *
Eric Andersen00a6a752002-04-26 23:53:10 +000017 *----------------------------------------------------------------------
18 */
Tito Ragusa7926b982011-08-09 04:37:50 +020019//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.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010025//config:
Tito Ragusa7926b982011-08-09 04:37:50 +020026//config:config USERS
27//config: bool "users"
28//config: default y
29//config: depends on FEATURE_UTMP
30//config: help
31//config: Print users currently logged on.
32
Denys Vlasenko205d48e2017-01-29 14:57:33 +010033// APPLET_ODDNAME:name main location suid_type help
Tito Ragusa7926b982011-08-09 04:37:50 +020034//applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010035//applet:IF_WHO(APPLET(who, BB_DIR_USR_BIN, BB_SUID_DROP))
Tito Ragusa7926b982011-08-09 04:37:50 +020036
37//kbuild:lib-$(CONFIG_USERS) += who.o
38//kbuild:lib-$(CONFIG_WHO) += who.o
39
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010040/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
41
Tito Ragusa7926b982011-08-09 04:37:50 +020042//usage:#define users_trivial_usage
43//usage: ""
44//usage:#define users_full_usage "\n\n"
45//usage: "Print the users currently logged on"
46
Pere Orga34425382011-03-31 14:43:25 +020047//usage:#define who_trivial_usage
48//usage: "[-a]"
49//usage:#define who_full_usage "\n\n"
50//usage: "Show who is logged on\n"
Pere Orga34425382011-03-31 14:43:25 +020051//usage: "\n -a Show all"
Denys Vlasenkob110e1f2012-04-18 14:38:15 +020052//usage: "\n -H Print column headers"
Pere Orga34425382011-03-31 14:43:25 +020053
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000054#include "libbb.h"
Eric Andersen00a6a752002-04-26 23:53:10 +000055
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000056static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000057{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000058 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000059
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000060 /*if (t < 60) {
61 str6[0] = '.';
62 str6[1] = '\0';
63 return;
64 }*/
65 if (t >= 0 && t < (24 * 60 * 60)) {
66 sprintf(str6, "%02d:%02d",
67 (int) (t / (60 * 60)),
68 (int) ((t % (60 * 60)) / 60));
69 return;
Rob Landleye01d7462006-03-12 19:26:01 +000070 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000071 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +000072}
73
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000074int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000075int who_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000076{
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020077 struct utmpx *ut;
Denis Vlasenko01cd9572007-11-16 05:24:43 +000078 unsigned opt;
Tito Ragusa7926b982011-08-09 04:37:50 +020079 int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
80 const char *fmt = "%s";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000081
Denis Vlasenko01cd9572007-11-16 05:24:43 +000082 opt_complementary = "=0";
Tito Ragusa7926b982011-08-09 04:37:50 +020083 opt = getopt32(argv, do_users ? "" : "aH");
Denys Vlasenko4c721042010-04-04 23:45:09 +020084 if (opt & 2) // -H
Denys Vlasenkod60752f2015-10-07 22:42:45 +020085 puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000086
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020087 setutxent();
88 while ((ut = getutxent()) != NULL) {
Denys Vlasenko4c721042010-04-04 23:45:09 +020089 if (ut->ut_user[0]
90 && ((opt & 1) || ut->ut_type == USER_PROCESS)
91 ) {
Tito Ragusa7926b982011-08-09 04:37:50 +020092 if (!do_users) {
93 char str6[6];
94 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
95 struct stat st;
96 time_t seconds;
Denys Vlasenko4c721042010-04-04 23:45:09 +020097
Tito Ragusa7926b982011-08-09 04:37:50 +020098 str6[0] = '?';
99 str6[1] = '\0';
100 strcpy(name, "/dev/");
101 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
102 ut->ut_line,
103 sizeof(ut->ut_line)+1
104 );
105 if (stat(name, &st) == 0)
106 idle_string(str6, st.st_atime);
107 /* manpages say ut_tv.tv_sec *is* time_t,
108 * but some systems have it wrong */
109 seconds = ut->ut_tv.tv_sec;
110 /* How wide time field can be?
111 * "Nov 10 19:33:20": 15 chars
112 * "2010-11-10 19:33": 16 chars
113 */
114 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
115 (int)sizeof(ut->ut_user), ut->ut_user,
116 (int)sizeof(ut->ut_line), ut->ut_line,
117 str6,
118 ctime(&seconds) + 4,
119 (int)sizeof(ut->ut_host), ut->ut_host
120 );
121 } else {
122 printf(fmt, ut->ut_user);
123 fmt = " %s";
124 }
Rob Landleye01d7462006-03-12 19:26:01 +0000125 }
126 }
Tito Ragusa7926b982011-08-09 04:37:50 +0200127 if (do_users)
128 bb_putchar('\n');
Denis Vlasenko41cca2b2007-03-07 00:07:42 +0000129 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200130 endutxent();
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +0000131 return EXIT_SUCCESS;
Eric Andersen00a6a752002-04-26 23:53:10 +0000132}