1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "libbb.h"
22#include <utmp.h>
23
24static void idle_string(char *str6, time_t t)
25{
26 t = time(NULL) - t;
27
28
29
30
31
32
33 if (t >= 0 && t < (24 * 60 * 60)) {
34 sprintf(str6, "%02d:%02d",
35 (int) (t / (60 * 60)),
36 (int) ((t % (60 * 60)) / 60));
37 return;
38 }
39 strcpy(str6, "old");
40}
41
42int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int who_main(int argc UNUSED_PARAM, char **argv)
44{
45 char str6[6];
46 struct utmp *ut;
47 struct stat st;
48 char *name;
49 unsigned opt;
50
51 opt_complementary = "=0";
52 opt = getopt32(argv, "a");
53
54 setutent();
55 printf("USER TTY IDLE TIME HOST\n");
56 while ((ut = getutent()) != NULL) {
57 if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
58 time_t tmp;
59
60 name = concat_path_file("/dev", ut->ut_line);
61 str6[0] = '?';
62 str6[1] = '\0';
63 if (stat(name, &st) == 0)
64 idle_string(str6, st.st_atime);
65
66
67 tmp = ut->ut_tv.tv_sec;
68
69 printf("%-10s %-8s %-9s %-15.15s %s\n",
70 ut->ut_user, ut->ut_line, str6,
71 ctime(&tmp) + 4, ut->ut_host);
72 if (ENABLE_FEATURE_CLEAN_UP)
73 free(name);
74 }
75 }
76 if (ENABLE_FEATURE_CLEAN_UP)
77 endutent();
78 return EXIT_SUCCESS;
79}
80