blob: f955ce6d35d578d436fdb16dac138092c68e85e0 [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 */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020019/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
Eric Andersen00a6a752002-04-26 23:53:10 +000020
Tito Ragusa7926b982011-08-09 04:37:50 +020021//config:config WHO
22//config: bool "who"
23//config: default y
24//config: depends on FEATURE_UTMP
25//config: help
26//config: who is used to show who is logged on.
27
28//config:config USERS
29//config: bool "users"
30//config: default y
31//config: depends on FEATURE_UTMP
32//config: help
33//config: Print users currently logged on.
34
35//applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
36//applet:IF_WHO( APPLET( who, BB_DIR_USR_BIN, BB_SUID_DROP))
37
38//kbuild:lib-$(CONFIG_USERS) += who.o
39//kbuild:lib-$(CONFIG_WHO) += who.o
40
41//usage:#define users_trivial_usage
42//usage: ""
43//usage:#define users_full_usage "\n\n"
44//usage: "Print the users currently logged on"
45
Pere Orga34425382011-03-31 14:43:25 +020046//usage:#define who_trivial_usage
47//usage: "[-a]"
48//usage:#define who_full_usage "\n\n"
49//usage: "Show who is logged on\n"
Pere Orga34425382011-03-31 14:43:25 +020050//usage: "\n -a Show all"
Denys Vlasenkob110e1f2012-04-18 14:38:15 +020051//usage: "\n -H Print column headers"
Pere Orga34425382011-03-31 14:43:25 +020052
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000053#include "libbb.h"
Eric Andersen00a6a752002-04-26 23:53:10 +000054
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000055static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000056{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000057 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000058
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000059 /*if (t < 60) {
60 str6[0] = '.';
61 str6[1] = '\0';
62 return;
63 }*/
64 if (t >= 0 && t < (24 * 60 * 60)) {
65 sprintf(str6, "%02d:%02d",
66 (int) (t / (60 * 60)),
67 (int) ((t % (60 * 60)) / 60));
68 return;
Rob Landleye01d7462006-03-12 19:26:01 +000069 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000070 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +000071}
72
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000073int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000074int who_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000075{
Rob Landleye01d7462006-03-12 19:26:01 +000076 struct utmp *ut;
Denis Vlasenko01cd9572007-11-16 05:24:43 +000077 unsigned opt;
Tito Ragusa7926b982011-08-09 04:37:50 +020078 int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
79 const char *fmt = "%s";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000080
Denis Vlasenko01cd9572007-11-16 05:24:43 +000081 opt_complementary = "=0";
Tito Ragusa7926b982011-08-09 04:37:50 +020082 opt = getopt32(argv, do_users ? "" : "aH");
Denys Vlasenko4c721042010-04-04 23:45:09 +020083 if (opt & 2) // -H
84 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000085
Rob Landleye01d7462006-03-12 19:26:01 +000086 setutent();
Rob Landleye01d7462006-03-12 19:26:01 +000087 while ((ut = getutent()) != NULL) {
Denys Vlasenko4c721042010-04-04 23:45:09 +020088 if (ut->ut_user[0]
89 && ((opt & 1) || ut->ut_type == USER_PROCESS)
90 ) {
Tito Ragusa7926b982011-08-09 04:37:50 +020091 if (!do_users) {
92 char str6[6];
93 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
94 struct stat st;
95 time_t seconds;
Denys Vlasenko4c721042010-04-04 23:45:09 +020096
Tito Ragusa7926b982011-08-09 04:37:50 +020097 str6[0] = '?';
98 str6[1] = '\0';
99 strcpy(name, "/dev/");
100 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
101 ut->ut_line,
102 sizeof(ut->ut_line)+1
103 );
104 if (stat(name, &st) == 0)
105 idle_string(str6, st.st_atime);
106 /* manpages say ut_tv.tv_sec *is* time_t,
107 * but some systems have it wrong */
108 seconds = ut->ut_tv.tv_sec;
109 /* How wide time field can be?
110 * "Nov 10 19:33:20": 15 chars
111 * "2010-11-10 19:33": 16 chars
112 */
113 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
114 (int)sizeof(ut->ut_user), ut->ut_user,
115 (int)sizeof(ut->ut_line), ut->ut_line,
116 str6,
117 ctime(&seconds) + 4,
118 (int)sizeof(ut->ut_host), ut->ut_host
119 );
120 } else {
121 printf(fmt, ut->ut_user);
122 fmt = " %s";
123 }
Rob Landleye01d7462006-03-12 19:26:01 +0000124 }
125 }
Tito Ragusa7926b982011-08-09 04:37:50 +0200126 if (do_users)
127 bb_putchar('\n');
Denis Vlasenko41cca2b2007-03-07 00:07:42 +0000128 if (ENABLE_FEATURE_CLEAN_UP)
129 endutent();
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +0000130 return EXIT_SUCCESS;
Eric Andersen00a6a752002-04-26 23:53:10 +0000131}