From 67473085a1b2b159ea569bc9525ce2535a240507 Mon Sep 17 00:00:00 2001 From: alexmajor <5017286+alexmajor@users.noreply.github.com> Date: Wed, 12 Jan 2022 23:42:43 -0500 Subject: [PATCH] Utilities: Port ifconfig to LibMain --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/ifconfig.cpp | 21 ++++++--------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index d50212c4e2..955261792f 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -110,6 +110,7 @@ target_link_libraries(hexdump LibMain) target_link_libraries(host LibMain) target_link_libraries(hostname LibMain) target_link_libraries(id LibMain) +target_link_libraries(ifconfig LibMain) target_link_libraries(ini LibMain) target_link_libraries(jp LibMain) target_link_libraries(js LibJS LibLine LibMain) diff --git a/Userland/Utilities/ifconfig.cpp b/Userland/Utilities/ifconfig.cpp index b81770dcce..64bfaad74e 100644 --- a/Userland/Utilities/ifconfig.cpp +++ b/Userland/Utilities/ifconfig.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, Alex Major * * SPDX-License-Identifier: BSD-2-Clause */ @@ -13,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +23,7 @@ #include #include -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { const char* value_ipv4 = nullptr; const char* value_adapter = nullptr; @@ -34,23 +36,12 @@ int main(int argc, char** argv) args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "adapter"); args_parser.add_option(value_gateway, "Set the default gateway of the selected network", "gateway", 'g', "gateway"); args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "mask"); - args_parser.parse(argc, argv); + args_parser.parse(arguments); if (!value_ipv4 && !value_adapter && !value_gateway && !value_mask) { + auto file = TRY(Core::File::open("/proc/net/adapters", Core::OpenMode::ReadOnly)); + auto json = TRY(JsonValue::from_string(file->read_all())); - auto file = Core::File::construct("/proc/net/adapters"); - if (!file->open(Core::OpenMode::ReadOnly)) { - outln("Failed to open {}: {}", file->name(), file->error_string()); - return 1; - } - - auto file_contents = file->read_all(); - auto json_or_error = JsonValue::from_string(file_contents); - if (json_or_error.is_error()) { - outln("Failed to decode JSON: {}", json_or_error.error()); - return 1; - } - auto json = json_or_error.release_value(); json.as_array().for_each([](auto& value) { auto& if_object = value.as_object();