From 0329a644f888b96f27cd5a6e5bc19a114ffd095d Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 23 Jul 2022 21:31:49 +0200 Subject: [PATCH] chmod: Port to ArgsParser --- Userland/Utilities/chmod.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Userland/Utilities/chmod.cpp b/Userland/Utilities/chmod.cpp index 5929d931e0..95019b49e9 100644 --- a/Userland/Utilities/chmod.cpp +++ b/Userland/Utilities/chmod.cpp @@ -6,33 +6,29 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include -#include -#include -#include #include +#include #include #include #include -#include -#include -#include ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath fattr")); - if (arguments.strings.size() < 3) { - warnln("usage: chmod "); - warnln(" chmod [[ugoa][+-=][rwx...],...] "); - return 1; - } + StringView mode; + Vector paths; - auto mask = TRY(Core::FilePermissionsMask::parse(arguments.strings[1])); + Core::ArgsParser args_parser; + args_parser.add_positional_argument(mode, "File mode in octal or symbolic notation", "mode"); + args_parser.add_positional_argument(paths, "Paths to file", "paths"); + args_parser.parse(arguments); - for (size_t i = 2; i < arguments.strings.size(); ++i) { - auto current_access = TRY(Core::System::stat(arguments.strings[i])); - TRY(Core::System::chmod(arguments.strings[i], mask.apply(current_access.st_mode))); + auto mask = TRY(Core::FilePermissionsMask::parse(mode)); + + for (auto const& path : paths) { + auto current_access = TRY(Core::System::stat(path)); + TRY(Core::System::chmod(path, mask.apply(current_access.st_mode))); } return 0;