fdisk: calculate +size{K,M,G} in 2^N

fdisk(8) does not calculate partition size (+sizeM or +sizeG)
in MiB or GiB correctly. It uses 10^N instead 2^N.

This patch cleanups +sizeX to:

  +sizeK    -- KiB  (2^10)
  +sizeKB   -- KB   (10^3)
  +sizeM    -- MiB  (2^20)
  +sizeMB   -- MB   (10^6)
  +sizeG    -- GB   (10^9)
  +sizeGB   -- GiB  (2^30)

This patch also fixes the "Last cylinder..." hint message. The "+number"
without any suffix is not a size at all. It's number of cylinders/sectors.
Note, the 10^N suffixes are not proposed to end-uses in the hint message.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2007-12-13 01:06:44 +01:00
parent 3f59f818b4
commit b1edb5102d
1 changed files with 49 additions and 23 deletions

View File

@ -1171,36 +1171,61 @@ read_int(unsigned int low, unsigned int dflt, unsigned int high,
if (*line_ptr == '+' || *line_ptr == '-') {
int minus = (*line_ptr == '-');
int absolute = 0;
int suflen;
i = atoi(line_ptr+1);
while (isdigit(*++line_ptr))
while (isdigit(*++line_ptr))
use_default = 0;
switch (*line_ptr) {
case 'c':
case 'C':
if (!display_in_cyl_units)
i *= heads * sectors;
break;
case 'K':
absolute = 1024;
break;
case 'k':
suflen = strlen(line_ptr) - 1;
while(isspace(*(line_ptr + suflen)))
*(line_ptr + suflen--) = '\0';
if ((*line_ptr == 'C' || *line_ptr == 'c') &&
*(line_ptr + 1) == '\0') {
/*
* Cylinders
*/
if (!display_in_cyl_units)
i *= heads * sectors;
} else if (*(line_ptr + 1) == 'B' &&
*(line_ptr + 2) == '\0') {
/*
* 10^N
*/
if (*line_ptr == 'K')
absolute = 1000;
break;
case 'm':
case 'M':
else if (*line_ptr == 'M')
absolute = 1000000;
break;
case 'g':
case 'G':
else if (*line_ptr == 'G')
absolute = 1000000000;
break;
default:
break;
else
absolute = -1;
} else if (*(line_ptr + 1) == '\0') {
/*
* 2^N
*/
if (*line_ptr == 'K')
absolute = 1 << 10;
else if (*line_ptr == 'M')
absolute = 1 << 20;
else if (*line_ptr == 'G')
absolute = 1 << 30;
else
absolute = -1;
} else if (*line_ptr != '\0')
absolute = -1;
if (absolute == -1) {
printf(_("Unsupported suffix: '%s'.\n"), line_ptr);
printf(_("Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)\n"
" 2^N: K (KibiByte), M (MebiByte), G (GibiByte)\n"));
continue;
}
if (absolute) {
if (absolute && i) {
unsigned long long bytes;
unsigned long unit;
@ -2061,8 +2086,9 @@ add_partition(int n, int sys) {
stop = limit;
} else {
snprintf(mesg, sizeof(mesg),
_("Last %s or +size or +sizeM or +sizeK"),
str_units(SINGULAR));
_("Last %1$s, +%2$s or +size{K,M,G}"),
str_units(SINGULAR), str_units(PLURAL));
stop = read_int(cround(start), cround(limit), cround(limit),
cround(start), mesg);
if (display_in_cyl_units) {