2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-11-27 17:55:40 +03:00
|
|
|
* Copyright (c) 2021, Kenneth Myhra <kennethmyhra@gmail.com>
|
2021-12-25 13:02:56 +03:00
|
|
|
* Copyright (c) 2021, Xavier Defrang <xavier.defrang@gmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
#include <AK/Format.h>
|
2019-07-13 20:36:02 +03:00
|
|
|
#include <AK/Optional.h>
|
2021-11-27 17:55:40 +03:00
|
|
|
#include <AK/String.h>
|
2021-12-20 23:19:18 +03:00
|
|
|
#include <AK/StringUtils.h>
|
2021-11-27 17:55:40 +03:00
|
|
|
#include <AK/Vector.h>
|
2021-12-25 13:02:56 +03:00
|
|
|
#include <LibCore/FilePermissionsMask.h>
|
2021-11-27 17:55:40 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-01-29 06:55:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <sys/stat.h>
|
2019-01-29 06:55:08 +03:00
|
|
|
|
2021-11-27 17:55:40 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-01-29 06:55:08 +03:00
|
|
|
{
|
2021-12-25 13:02:56 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath fattr"));
|
2020-01-12 15:25:02 +03:00
|
|
|
|
2021-11-27 17:55:40 +03:00
|
|
|
if (arguments.strings.size() < 3) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("usage: chmod <octal-mode> <path...>");
|
|
|
|
warnln(" chmod [[ugoa][+-=][rwx...],...] <path...>");
|
2019-01-29 06:55:08 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 13:02:56 +03:00
|
|
|
auto mask = TRY(Core::FilePermissionsMask::parse(arguments.strings[1]));
|
2019-07-13 20:36:02 +03:00
|
|
|
|
2021-12-25 13:02:56 +03:00
|
|
|
for (size_t i = 2; i < arguments.strings.size(); ++i) {
|
2021-11-27 17:55:40 +03:00
|
|
|
auto current_access = TRY(Core::System::stat(arguments.strings[i]));
|
2021-12-25 13:02:56 +03:00
|
|
|
TRY(Core::System::chmod(arguments.strings[i], mask.apply(current_access.st_mode)));
|
2019-01-29 06:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|