mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:28:11 +00:00
chmod+mkdir: Use convert_to_uint_from_octal
This commit is contained in:
parent
9e97823ff8
commit
26bb3e1acf
2 changed files with 16 additions and 5 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <AK/StringUtils.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
@ -50,6 +51,12 @@ public:
|
||||||
|
|
||||||
mode_t& get_removal_mask() { return removal_mask; }
|
mode_t& get_removal_mask() { return removal_mask; }
|
||||||
mode_t& get_applying_mask() { return applying_mask; }
|
mode_t& get_applying_mask() { return applying_mask; }
|
||||||
|
|
||||||
|
void set_mode(mode_t mode)
|
||||||
|
{
|
||||||
|
this->applying_mask = mode;
|
||||||
|
this->removal_mask = ~mode;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Optional<Mask> string_to_mode(char access_scope, StringView access_string);
|
Optional<Mask> string_to_mode(char access_scope, StringView access_string);
|
||||||
|
@ -69,12 +76,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
/* compute a mask */
|
/* compute a mask */
|
||||||
|
|
||||||
if (arguments.strings[1][0] >= '0' && arguments.strings[1][0] <= '7') {
|
auto mode_string = arguments.strings[1];
|
||||||
if (sscanf(arguments.strings[1].to_string().characters(), "%ho", &mask.get_applying_mask()) != 1) {
|
if (mode_string[0] >= '0' && mode_string[0] <= '7') {
|
||||||
perror("sscanf");
|
mode_t mode = AK::StringUtils::convert_to_uint_from_octal<u16>(mode_string).value_or(01000);
|
||||||
|
if (mode > 0777) {
|
||||||
|
warnln("chmod: invalid mode: {}", mode_string);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
mask.get_removal_mask() = ~mask.get_applying_mask();
|
mask.set_mode(mode);
|
||||||
} else {
|
} else {
|
||||||
auto access_strings = arguments.strings[1].split_view(',');
|
auto access_strings = arguments.strings[1].split_view(',');
|
||||||
for (auto access_string : access_strings) {
|
for (auto access_string : access_strings) {
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <AK/StringUtils.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
@ -32,7 +33,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
mode_t mode = default_mode;
|
mode_t mode = default_mode;
|
||||||
|
|
||||||
if (!mode_string.is_empty()) {
|
if (!mode_string.is_empty()) {
|
||||||
if (sscanf(mode_string.characters(), "%ho", &mode) != 1) {
|
mode = AK::StringUtils::convert_to_uint_from_octal<u16>(mode_string).value_or(01000);
|
||||||
|
if (mode > 0777) {
|
||||||
warnln("mkdir: invalid mode: {}", mode_string);
|
warnln("mkdir: invalid mode: {}", mode_string);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue