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