mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:57:45 +00:00
Userland: Show idle times in "w" output
The idle time is based on the mtime of the session's TTY :^)
This commit is contained in:
parent
4527d9852a
commit
abce7e7ca2
1 changed files with 25 additions and 4 deletions
|
@ -3,6 +3,8 @@
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -11,6 +13,11 @@ int main()
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (unveil("/dev", "r") < 0) {
|
||||||
|
perror("unveil");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (unveil("/etc/passwd", "r") < 0) {
|
if (unveil("/etc/passwd", "r") < 0) {
|
||||||
perror("unveil");
|
perror("unveil");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -35,8 +42,10 @@ int main()
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\033[1m%-10s %-12s %-16s %-16s\033[0m\n",
|
auto now = time(nullptr);
|
||||||
"USER", "TTY", "FROM", "LOGIN@");
|
|
||||||
|
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) {
|
json.value().as_object().for_each_member([&](auto& tty, auto& value) {
|
||||||
const JsonObject& entry = value.as_object();
|
const JsonObject& entry = value.as_object();
|
||||||
auto uid = entry.get("uid").to_u32();
|
auto uid = entry.get("uid").to_u32();
|
||||||
|
@ -52,11 +61,23 @@ int main()
|
||||||
else
|
else
|
||||||
username = String::number(uid);
|
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(),
|
username.characters(),
|
||||||
tty.characters(),
|
tty.characters(),
|
||||||
from.characters(),
|
from.characters(),
|
||||||
login_at.characters());
|
login_at.characters(),
|
||||||
|
idle_string.characters());
|
||||||
});
|
});
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue