xref: /aosp_15_r20/external/toybox/toys/posix/who.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* who.c - display who is on the system
2  *
3  * Copyright 2012 ProFUSION Embedded Systems
4  *
5  * by Luis Felipe Strano Moraes <[email protected]>
6  *
7  * See http://opengroup.org/onlinepubs/9699919799/utilities/who.html
8  *
9  * Posix says to support many options (-abdHlmpqrstTu) but this
10  * isn't aimed at minicomputers with modem pools.
11  *
12  * TODO: -a doesn't know how to format other entries
13 
14 USE_WHO(NEWTOY(who, "a", TOYFLAG_USR|TOYFLAG_BIN))
15 
16 config WHO
17   bool "who"
18   default y
19   help
20     usage: who
21 
22     Print information about logged in users.
23 */
24 
25 #define FOR_who
26 #include "toys.h"
27 
who_main(void)28 void who_main(void)
29 {
30   struct utmpx *entry;
31 
32   setutxent();
33   while ((entry = getutxent())) {
34     if (FLAG(a) || entry->ut_type == USER_PROCESS) {
35       time_t t = entry->ut_tv.tv_sec;
36       struct tm *tm = localtime(&t);
37 
38       strftime(toybuf, sizeof(toybuf), "%F %H:%M", tm);
39       printf("%s\t%s\t%s (%s)\n", entry->ut_user, entry->ut_line,
40         toybuf, entry->ut_host);
41     }
42   }
43   endutxent();
44 }
45