summaryrefslogtreecommitdiff
path: root/support.cc
diff options
context:
space:
mode:
authorRod Smith <rodsmith@rodsbooks.com>2022-01-29 10:51:02 -0500
committerRod Smith <rodsmith@rodsbooks.com>2022-01-29 10:51:02 -0500
commitfd60f743628e16180daf3b1719974fa4dadf8f90 (patch)
treedcad6ad1cc1d304a2c4c3d1bd1d4315e4dad62e6 /support.cc
parent43b3df969cbbf3da0c043afdc9939da97bbd8d68 (diff)
downloadsgdisk-fd60f743628e16180daf3b1719974fa4dadf8f90.tar.gz
Add end-alignment feature.
Diffstat (limited to 'support.cc')
-rw-r--r--support.cc40
1 files changed, 9 insertions, 31 deletions
diff --git a/support.cc b/support.cc
index 99fdffe..0d3bd6f 100644
--- a/support.cc
+++ b/support.cc
@@ -3,7 +3,7 @@
// Primarily by Rod Smith, February 2009, but with a few functions
// copied from other sources (see attributions below).
-/* This program is copyright (c) 2009-2018 by Roderick W. Smith. It is distributed
+/* This program is copyright (c) 2009-2022 by Roderick W. Smith. It is distributed
under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
#define __STDC_LIMIT_MACROS
@@ -113,34 +113,11 @@ char GetYN(void) {
return response;
} // GetYN(void)
-// Obtains a sector number, between low and high, from the
-// user, accepting values prefixed by "+" to add sectors to low,
-// or the same with "K", "M", "G", "T", or "P" as suffixes to add
-// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
-// respectively. If a "-" prefix is used, use the high value minus
-// the user-specified number of sectors (or KiB, MiB, etc.). Use the
-// def value as the default if the user just hits Enter. The sSize is
-// the sector size of the device.
-uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
- const string & prompt) {
- uint64_t response;
- char line[255];
-
- do {
- cout << prompt;
- cin.getline(line, 255);
- if (!cin.good())
- exit(5);
- response = IeeeToInt(line, sSize, low, high, def);
- } while ((response < low) || (response > high));
- return response;
-} // GetSectorNum()
-
// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
// number of sectors. If no units are appended, interprets as the number
// of sectors; otherwise, interprets as number of specified units and
// converts to sectors. For instance, with 512-byte sectors, "1K" converts
-// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
+// to 2. If value includes a "+", adds low and subtracts 1; if inValue
// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
// Returns final sector value. In case inValue is invalid, returns 0 (a
// sector value that's always in use on GPT and therefore invalid); and if
@@ -153,7 +130,7 @@ uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
// 0 values. The result is that IeeeToInt() returns UINT64_MAX when
// compiled with GCC (and so the value is rejected), whereas when VC++
// is used, the default value is returned.
-uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
+uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint32_t sectorAlignment, uint64_t def) {
uint64_t response = def, bytesPerUnit, mult = 1, divide = 1;
size_t foundAt = 0;
char suffix = ' ', plusFlag = ' ';
@@ -208,11 +185,12 @@ uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high,
} // if/elseif
if (plusFlag == '+') {
- // Recompute response based on low part of range (if default == high
- // value, which should be the case when prompting for the end of a
- // range) or the defaut value (if default != high, which should be
- // the case for the first sector of a partition).
- if (def == high) {
+ // Recompute response based on low part of range (if default is within
+ // sectorAlignment sectors of high, which should be the case when
+ // prompting for the end of a range) or the defaut value (if default is
+ // further away from the high value, which should be the case for the
+ // first sector of a partition).
+ if ((high - def) < sectorAlignment) {
if (response > 0)
response--;
if (response > (UINT64_MAX - low))