mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
chmod: Port to ArgsParser
This commit is contained in:
parent
29e2b1c386
commit
0329a644f8
1 changed files with 12 additions and 16 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue