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.
6 * Author: Da Chen <dchen@ayrnetworks.com>
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
13 * Copyright (c) 2002 AYR Networks, Inc.
15 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
17 *----------------------------------------------------------------------
19 /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
24 //config: depends on FEATURE_UTMP
26 //config: who is used to show who is logged on.
29 //config: bool "users"
31 //config: depends on FEATURE_UTMP
33 //config: Print users currently logged on.
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))
38 //kbuild:lib-$(CONFIG_USERS) += who.o
39 //kbuild:lib-$(CONFIG_WHO) += who.o
41 //usage:#define users_trivial_usage
43 //usage:#define users_full_usage "\n\n"
44 //usage: "Print the users currently logged on"
46 //usage:#define who_trivial_usage
48 //usage:#define who_full_usage "\n\n"
49 //usage: "Show who is logged on\n"
50 //usage: "\n -a Show all"
51 //usage: "\n -H Print column headers"
55 static void idle_string(char *str6
, time_t t
)
64 if (t
>= 0 && t
< (24 * 60 * 60)) {
65 sprintf(str6
, "%02d:%02d",
66 (int) (t
/ (60 * 60)),
67 (int) ((t
% (60 * 60)) / 60));
73 int who_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
74 int who_main(int argc UNUSED_PARAM
, char **argv
)
78 int do_users
= (ENABLE_USERS
&& (!ENABLE_WHO
|| applet_name
[0] == 'u'));
79 const char *fmt
= "%s";
81 opt_complementary
= "=0";
82 opt
= getopt32(argv
, do_users
? "" : "aH");
84 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
87 while ((ut
= getutent()) != NULL
) {
89 && ((opt
& 1) || ut
->ut_type
== USER_PROCESS
)
93 char name
[sizeof("/dev/") + sizeof(ut
->ut_line
) + 1];
99 strcpy(name
, "/dev/");
100 safe_strncpy(ut
->ut_line
[0] == '/' ? name
: name
+ sizeof("/dev/")-1,
102 sizeof(ut
->ut_line
)+1
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
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
,
118 (int)sizeof(ut
->ut_host
), ut
->ut_host
121 printf(fmt
, ut
->ut_user
);
128 if (ENABLE_FEATURE_CLEAN_UP
)