From: Karel Zak Date: Thu, 13 Dec 2007 00:06:44 +0000 (+0100) Subject: fdisk: calculate +size{K,M,G} in 2^N X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1edb5102d51d0f916aecb579d694d740d5a6c5b;p=util-linux 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 --- diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c index c5e3f06c..cc71c5c4 100644 --- a/fdisk/fdisk.c +++ b/fdisk/fdisk.c @@ -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) {