chmod: Port to ArgsParser

This commit is contained in:
Tim Schumacher 2022-07-23 21:31:49 +02:00 committed by Andreas Kling
parent 29e2b1c386
commit 0329a644f8

View file

@ -6,33 +6,29 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringUtils.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/FilePermissionsMask.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath fattr"));
if (arguments.strings.size() < 3) {
warnln("usage: chmod <octal-mode> <path...>");
warnln(" chmod [[ugoa][+-=][rwx...],...] <path...>");
return 1;
}
StringView mode;
Vector<StringView> 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;