From 7232789211b4eda77b4fe70a4fdc0c2c7782380b Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 7 Feb 2019 21:04:50 +0100 Subject: [PATCH] Extract parse_size function from ramdisk command. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If7dd36321e1ed2feb20b4c76ddaf303bc997d8b7 Reviewed-on: https://review.haiku-os.org/c/1018 Reviewed-by: Jérôme Duval Reviewed-by: Axel Dörfler --- headers/private/shared/StringForSize.h | 2 ++ src/bin/ramdisk.cpp | 25 ++-------------- src/kits/shared/StringForSize.cpp | 40 +++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/headers/private/shared/StringForSize.h b/headers/private/shared/StringForSize.h index ab9a512095..00adb7af52 100644 --- a/headers/private/shared/StringForSize.h +++ b/headers/private/shared/StringForSize.h @@ -12,12 +12,14 @@ namespace BPrivate { const char* string_for_size(double size, char* string, size_t stringSize); +int64 parse_size(const char* sizeString); } // namespace BPrivate using BPrivate::string_for_size; +using BPrivate::parse_size; #endif // STRING_FOR_SIZE_H diff --git a/src/bin/ramdisk.cpp b/src/bin/ramdisk.cpp index b6070ea9f7..e82f0fc01f 100644 --- a/src/bin/ramdisk.cpp +++ b/src/bin/ramdisk.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -140,29 +141,7 @@ command_register(int argc, const char* const* argv) case 's': { const char* sizeString = optarg; - char* end; - deviceSize = strtoll(sizeString, &end, 0); - if (end != sizeString && deviceSize > 0) { - int64 originalDeviceSize = deviceSize; - switch (*end) { - case 'g': - deviceSize *= 1024; - case 'm': - deviceSize *= 1024; - case 'k': - deviceSize *= 1024; - end++; - break; - case '\0': - break; - default: - deviceSize = -1; - break; - } - - if (deviceSize > 0 && originalDeviceSize > deviceSize) - deviceSize = -1; - } + deviceSize = parse_size(sizeString); if (deviceSize <= 0) { fprintf(stderr, "Error: Invalid size argument: \"%s\"\n", diff --git a/src/kits/shared/StringForSize.cpp b/src/kits/shared/StringForSize.cpp index 9862ff8f29..6166e7831f 100644 --- a/src/kits/shared/StringForSize.cpp +++ b/src/kits/shared/StringForSize.cpp @@ -1,11 +1,14 @@ /* - * Copyright 2010 Haiku Inc. All rights reserved. + * Copyright 2010-2019 Haiku Inc. All rights reserved. + * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ #include "StringForSize.h" +#include #include +#include #include #include @@ -64,5 +67,40 @@ string_for_size(double size, char* string, size_t stringSize) } +int64 +parse_size(const char* sizeString) +{ + int64 parsedSize = -1; + char* end; + parsedSize = strtoll(sizeString, &end, 0); + if (end != sizeString && parsedSize > 0) { + int64 rawSize = parsedSize; + switch (tolower(*end)) { + case 't': + parsedSize *= 1024; + case 'g': + parsedSize *= 1024; + case 'm': + parsedSize *= 1024; + case 'k': + parsedSize *= 1024; + end++; + break; + case '\0': + break; + default: + parsedSize = -1; + break; + } + + // Check for overflow + if (parsedSize > 0 && rawSize > parsedSize) + parsedSize = -1; + } + + return parsedSize; +} + + } // namespace BPrivate