diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-22 19:40:27 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-22 19:40:27 +0100 |
commit | 561e50108d0cecac47f9cd118319baa7a7fbd4a7 (patch) | |
tree | d25d44e7c2acc5b78e49be5d21f711e87919ca0a /Userland/Utilities | |
parent | e388782f6013cf9efcccade28f9fc7b1772d4d96 (diff) | |
download | serenity-561e50108d0cecac47f9cd118319baa7a7fbd4a7.zip |
w: Port to LibMain :^)
This unlocked a bunch of TRY() opportunities. Not just system calls, but
also Core::File::open() and JsonValue::from_string().
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Utilities/w.cpp | 52 |
2 files changed, 15 insertions, 38 deletions
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 <LibCore/DateTime.h> #include <LibCore/File.h> #include <LibCore/ProcessStatisticsReader.h> +#include <LibMain/Main.h> +#include <LibSystem/Wrappers.h> #include <pwd.h> -#include <stdio.h> #include <sys/stat.h> #include <time.h> -int main() +ErrorOr<int> serenity_main(Main::Arguments) { - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } - - 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()) { + 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)); + + 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(); |