From 87de32df590a96a3bcf71a8bae2d144e69454559 Mon Sep 17 00:00:00 2001 From: bleusakura <59930937+bleusakura@users.noreply.github.com> Date: Sat, 8 May 2021 05:03:31 -0400 Subject: [PATCH] dd: Add ability to use k, M, & G suffixes for block sizes --- Userland/Utilities/dd.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Userland/Utilities/dd.cpp b/Userland/Utilities/dd.cpp index 18b1736cd7..720c0b598b 100644 --- a/Userland/Utilities/dd.cpp +++ b/Userland/Utilities/dd.cpp @@ -22,7 +22,7 @@ const char* usage = "usage:\n" "options:\n" "\tif=\tinput file (default: stdin)\n" "\tof=\toutput file (default: stdout)\n" - "\tbs=\tblocks size (default: 512)\n" + "\tbs=\tblocks size may be followed by multiplicate suffixes: k=1024, M=1024*1024, G=1024*1024*1024 (default: 512)\n" "\tcount=\t blocks to copy (default: 0 (until end-of-file))\n" "\tseek=\tskip blocks at start of output (default: 0)\n" "\tskip=\tskip blocks at start of input (default: 0)\n" @@ -74,13 +74,29 @@ static int handle_size_arguments(size_t& numeric_value, const char* argument) return -1; } + unsigned suffix_multiplier = 1; + switch (value.to_lowercase()[value.length() - 1]) { + case 'k': + suffix_multiplier = KiB; + value = value.substring(0, value.length() - 1); + break; + case 'm': + suffix_multiplier = MiB; + value = value.substring(0, value.length() - 1); + break; + case 'g': + suffix_multiplier = GiB; + value = value.substring(0, value.length() - 1); + break; + } + Optional numeric_optional = value.to_uint(); if (!numeric_optional.has_value()) { fprintf(stderr, "Invalid size-value: %s\n", value.characters()); return -1; } - numeric_value = numeric_optional.value(); + numeric_value = numeric_optional.value() * suffix_multiplier; if (numeric_value < 1) { fprintf(stderr, "Invalid size-value: %lu\n", numeric_value); return -1;