From abce7e7ca2248043fb0862eda7dc4aa8894f67fa Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Sep 2020 18:58:58 +0200 Subject: [PATCH] Userland: Show idle times in "w" output The idle time is based on the mtime of the session's TTY :^) --- Userland/w.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Userland/w.cpp b/Userland/w.cpp index 764ed6ca57a..bf0a2afe491 100644 --- a/Userland/w.cpp +++ b/Userland/w.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include int main() { @@ -11,6 +13,11 @@ int main() return 1; } + if (unveil("/dev", "r") < 0) { + perror("unveil"); + return 1; + } + if (unveil("/etc/passwd", "r") < 0) { perror("unveil"); return 1; @@ -35,8 +42,10 @@ int main() return 1; } - printf("\033[1m%-10s %-12s %-16s %-16s\033[0m\n", - "USER", "TTY", "FROM", "LOGIN@"); + auto now = time(nullptr); + + printf("\033[1m%-10s %-12s %-16s %-20s %-6s\033[0m\n", + "USER", "TTY", "FROM", "LOGIN@", "IDLE"); json.value().as_object().for_each_member([&](auto& tty, auto& value) { const JsonObject& entry = value.as_object(); auto uid = entry.get("uid").to_u32(); @@ -52,11 +61,23 @@ int main() else username = String::number(uid); - printf("%-10s %-12s %-16s %-16s\n", + StringBuilder builder; + String idle_string = "n/a"; + struct stat st; + if (stat(tty.characters(), &st) == 0) { + auto idle_time = now - st.st_mtime; + if (idle_time >= 0) { + builder.appendf("%d sec", idle_time); + idle_string = builder.to_string(); + } + } + + printf("%-10s %-12s %-16s %-20s %-6s\n", username.characters(), tty.characters(), from.characters(), - login_at.characters()); + login_at.characters(), + idle_string.characters()); }); return 0; }