aboutsummaryrefslogtreecommitdiff
path: root/coreutils/who.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/who.c')
-rw-r--r--coreutils/who.c90
1 files changed, 62 insertions, 28 deletions
diff --git a/coreutils/who.c b/coreutils/who.c
index 9f37f65fd..c6c925264 100644
--- a/coreutils/who.c
+++ b/coreutils/who.c
@@ -18,6 +18,31 @@
18 */ 18 */
19/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */ 19/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
20 20
21//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
21//usage:#define who_trivial_usage 46//usage:#define who_trivial_usage
22//usage: "[-a]" 47//usage: "[-a]"
23//usage:#define who_full_usage "\n\n" 48//usage:#define who_full_usage "\n\n"
@@ -49,9 +74,11 @@ int who_main(int argc UNUSED_PARAM, char **argv)
49{ 74{
50 struct utmp *ut; 75 struct utmp *ut;
51 unsigned opt; 76 unsigned opt;
77 int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
78 const char *fmt = "%s";
52 79
53 opt_complementary = "=0"; 80 opt_complementary = "=0";
54 opt = getopt32(argv, "aH"); 81 opt = getopt32(argv, do_users ? "" : "aH");
55 if (opt & 2) // -H 82 if (opt & 2) // -H
56 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n"); 83 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
57 84
@@ -60,36 +87,43 @@ int who_main(int argc UNUSED_PARAM, char **argv)
60 if (ut->ut_user[0] 87 if (ut->ut_user[0]
61 && ((opt & 1) || ut->ut_type == USER_PROCESS) 88 && ((opt & 1) || ut->ut_type == USER_PROCESS)
62 ) { 89 ) {
63 char str6[6]; 90 if (!do_users) {
64 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1]; 91 char str6[6];
65 struct stat st; 92 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
66 time_t seconds; 93 struct stat st;
94 time_t seconds;
67 95
68 str6[0] = '?'; 96 str6[0] = '?';
69 str6[1] = '\0'; 97 str6[1] = '\0';
70 strcpy(name, "/dev/"); 98 strcpy(name, "/dev/");
71 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1, 99 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
72 ut->ut_line, 100 ut->ut_line,
73 sizeof(ut->ut_line)+1 101 sizeof(ut->ut_line)+1
74 ); 102 );
75 if (stat(name, &st) == 0) 103 if (stat(name, &st) == 0)
76 idle_string(str6, st.st_atime); 104 idle_string(str6, st.st_atime);
77 /* manpages say ut_tv.tv_sec *is* time_t, 105 /* manpages say ut_tv.tv_sec *is* time_t,
78 * but some systems have it wrong */ 106 * but some systems have it wrong */
79 seconds = ut->ut_tv.tv_sec; 107 seconds = ut->ut_tv.tv_sec;
80 /* How wide time field can be? 108 /* How wide time field can be?
81 * "Nov 10 19:33:20": 15 chars 109 * "Nov 10 19:33:20": 15 chars
82 * "2010-11-10 19:33": 16 chars 110 * "2010-11-10 19:33": 16 chars
83 */ 111 */
84 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n", 112 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
85 (int)sizeof(ut->ut_user), ut->ut_user, 113 (int)sizeof(ut->ut_user), ut->ut_user,
86 (int)sizeof(ut->ut_line), ut->ut_line, 114 (int)sizeof(ut->ut_line), ut->ut_line,
87 str6, 115 str6,
88 ctime(&seconds) + 4, 116 ctime(&seconds) + 4,
89 (int)sizeof(ut->ut_host), ut->ut_host 117 (int)sizeof(ut->ut_host), ut->ut_host
90 ); 118 );
119 } else {
120 printf(fmt, ut->ut_user);
121 fmt = " %s";
122 }
91 } 123 }
92 } 124 }
125 if (do_users)
126 bb_putchar('\n');
93 if (ENABLE_FEATURE_CLEAN_UP) 127 if (ENABLE_FEATURE_CLEAN_UP)
94 endutent(); 128 endutent();
95 return EXIT_SUCCESS; 129 return EXIT_SUCCESS;