diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-22 16:03:06 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-22 19:28:31 +0100 |
commit | 843262497a8661945f01287abb9713b481e20d52 (patch) | |
tree | dc2151bcd0067d09fc5577fc4f5d7aff5dc4e66f | |
parent | 4e530135d554adf4e3edfafc580585b07b3b07ce (diff) | |
download | serenity-843262497a8661945f01287abb9713b481e20d52.zip |
id: Port to LibMain :^)
This is a first port of a simple program to LibMain. A bunch of code is
immediately simplified thanks to the LibSystem wrappers and ability to
use TRY(). This is pretty cool!
-rw-r--r-- | Userland/Utilities/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Utilities/id.cpp | 48 |
2 files changed, 13 insertions, 36 deletions
diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 81f5ec16b4..3c23ac8c34 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -69,6 +69,7 @@ target_link_libraries(gml-format LibGUI) target_link_libraries(grep LibRegex) target_link_libraries(gunzip LibCompress) target_link_libraries(gzip LibCompress) +target_link_libraries(id LibMain) target_link_libraries(js LibJS LibLine) target_link_libraries(keymap LibKeyboard) target_link_libraries(lspci LibPCIDB) diff --git a/Userland/Utilities/id.cpp b/Userland/Utilities/id.cpp index c7a5a17568..ae721fec3b 100644 --- a/Userland/Utilities/id.cpp +++ b/Userland/Utilities/id.cpp @@ -7,6 +7,8 @@ #include <AK/StringUtils.h> #include <LibCore/Account.h> #include <LibCore/ArgsParser.h> +#include <LibMain/Main.h> +#include <LibSystem/Wrappers.h> #include <alloca.h> #include <grp.h> #include <pwd.h> @@ -21,27 +23,12 @@ static bool flag_print_name = false; static bool flag_print_gid_all = false; static String user_str; -int main(int argc, char** argv) +ErrorOr<int> serenity_main(Main::Arguments arguments) { - if (unveil("/etc/passwd", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil("/etc/group", "r") < 0) { - perror("unveil"); - return 1; - } - - if (unveil(nullptr, nullptr) < 0) { - perror("unveil"); - return 1; - } - - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(System::unveil("/etc/passwd", "r")); + TRY(System::unveil("/etc/group", "r")); + TRY(System::unveil(nullptr, nullptr)); + TRY(System::pledge("stdio rpath", nullptr)); Core::ArgsParser args_parser; args_parser.add_option(flag_print_uid, "Print UID", nullptr, 'u'); @@ -49,7 +36,7 @@ int main(int argc, char** argv) args_parser.add_option(flag_print_gid_all, "Print all GIDs", nullptr, 'G'); args_parser.add_option(flag_print_name, "Print name", nullptr, 'n'); args_parser.add_positional_argument(user_str, "User name/UID to query", "USER", Core::ArgsParser::Required::No); - args_parser.parse(argc, argv); + args_parser.parse(arguments.argc, arguments.argv); if (flag_print_name && !(flag_print_uid || flag_print_gid || flag_print_gid_all)) { warnln("cannot print only names or real IDs in default format"); @@ -63,21 +50,10 @@ int main(int argc, char** argv) Optional<Core::Account> account; if (!user_str.is_empty()) { - if (auto user_id = user_str.to_uint(); user_id.has_value()) { - auto result = Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly); - if (result.is_error()) { - warnln("Couldn't retrieve user id '{}': {}", user_str, result.error()); - return 1; - } - account = result.release_value(); - } else { - auto result = Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly); - if (result.is_error()) { - warnln("Couldn't retrieve user name '{}': {}", user_str, result.error()); - return 1; - } - account = result.release_value(); - } + if (auto user_id = user_str.to_uint(); user_id.has_value()) + account = TRY(Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly)); + else + account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly)); } else { account = Core::Account::self(Core::Account::Read::PasswdOnly); } |