mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-27 13:11:46 +03:00
truncate: Allow size suffixes to be used with the -s
option
The base 2 size suffixes: 'K', 'M' and 'G' may now be used when specifying a file size with the `-s` option.
This commit is contained in:
parent
fb27702981
commit
8e79afebb9
Notes:
sideshowbarker
2024-07-18 00:54:03 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/8e79afebb9 Pull-request: https://github.com/SerenityOS/serenity/pull/20145
@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
@ -53,12 +54,34 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
break;
|
||||
}
|
||||
|
||||
auto suffix = resize[resize.length() - 1];
|
||||
i64 multiplier = 1;
|
||||
if (!AK::is_ascii_digit(suffix)) {
|
||||
switch (to_ascii_lowercase(suffix)) {
|
||||
case 'k':
|
||||
multiplier = KiB;
|
||||
resize = resize.substring_view(0, resize.length() - 1);
|
||||
break;
|
||||
case 'm':
|
||||
multiplier = MiB;
|
||||
resize = resize.substring_view(0, resize.length() - 1);
|
||||
break;
|
||||
case 'g':
|
||||
multiplier = GiB;
|
||||
resize = resize.substring_view(0, resize.length() - 1);
|
||||
break;
|
||||
default:
|
||||
args_parser.print_usage(stderr, arguments.strings[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
auto size_opt = resize.to_int<off_t>();
|
||||
if (!size_opt.has_value()) {
|
||||
if (!size_opt.has_value() || Checked<off_t>::multiplication_would_overflow(size_opt.value(), multiplier)) {
|
||||
args_parser.print_usage(stderr, arguments.strings[0]);
|
||||
return 1;
|
||||
}
|
||||
size = size_opt.value();
|
||||
size = size_opt.value() * multiplier;
|
||||
}
|
||||
|
||||
if (!reference.is_empty()) {
|
||||
|
Loading…
Reference in New Issue
Block a user