diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 3c23ac8c34..612063e393 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -102,5 +102,6 @@ target_link_libraries(zip LibArchive LibCompress LibCrypto) target_link_libraries(cpp-lexer LibCpp) target_link_libraries(cpp-parser LibCpp LibGUI) target_link_libraries(cpp-preprocessor LibCpp LibGUI) +target_link_libraries(w LibMain) target_link_libraries(wasm LibWasm LibLine) target_link_libraries(wsctl LibGUI) diff --git a/Userland/Utilities/w.cpp b/Userland/Utilities/w.cpp index ad30396efd..4dd68aa98a 100644 --- a/Userland/Utilities/w.cpp +++ b/Userland/Utilities/w.cpp @@ -9,48 +9,24 @@ #include #include #include +#include +#include #include -#include #include #include -int main() +ErrorOr serenity_main(Main::Arguments) { - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(System::pledge("stdio rpath", nullptr)); + TRY(System::unveil("/dev", "r")); + TRY(System::unveil("/etc/passwd", "r")); + TRY(System::unveil("/var/run/utmp", "r")); + TRY(System::unveil("/proc", "r")); + TRY(System::unveil(nullptr, nullptr)); - if (unveil("/dev", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/etc/passwd", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/var/run/utmp", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/proc", "r") < 0) { - perror("unveil"); - return 1; - } - - unveil(nullptr, nullptr); - - auto file_or_error = Core::File::open("/var/run/utmp", Core::OpenMode::ReadOnly); - if (file_or_error.is_error()) { - warnln("Error: {}", file_or_error.error()); - return 1; - } - auto& file = *file_or_error.value(); - auto json = JsonValue::from_string(file.read_all()); - if (json.is_error() || !json.value().is_object()) { + auto file = TRY(Core::File::open("/var/run/utmp", Core::OpenMode::ReadOnly)); + auto json = TRY(JsonValue::from_string(file->read_all())); + if (!json.is_object()) { warnln("Error: Could not parse /var/run/utmp"); return 1; } @@ -64,7 +40,7 @@ int main() auto now = time(nullptr); outln("\033[1m{:10} {:12} {:16} {:6} {}\033[0m", "USER", "TTY", "LOGIN@", "IDLE", "WHAT"); - json.value().as_object().for_each_member([&](auto& tty, auto& value) { + json.as_object().for_each_member([&](auto& tty, auto& value) { const JsonObject& entry = value.as_object(); auto uid = entry.get("uid").to_u32(); [[maybe_unused]] auto pid = entry.get("pid").to_i32();