mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +00: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
|
* 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 <AK/Vector.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/FilePermissionsMask.h>
|
#include <LibCore/FilePermissionsMask.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio rpath fattr"));
|
TRY(Core::System::pledge("stdio rpath fattr"));
|
||||||
|
|
||||||
if (arguments.strings.size() < 3) {
|
StringView mode;
|
||||||
warnln("usage: chmod <octal-mode> <path...>");
|
Vector<StringView> paths;
|
||||||
warnln(" chmod [[ugoa][+-=][rwx...],...] <path...>");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 mask = TRY(Core::FilePermissionsMask::parse(mode));
|
||||||
auto current_access = TRY(Core::System::stat(arguments.strings[i]));
|
|
||||||
TRY(Core::System::chmod(arguments.strings[i], mask.apply(current_access.st_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;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue