]> err.no Git - util-linux/commitdiff
fdisk: calculate +size{K,M,G} in 2^N
authorKarel Zak <kzak@redhat.com>
Thu, 13 Dec 2007 00:06:44 +0000 (01:06 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 14 Jan 2008 11:10:29 +0000 (12:10 +0100)
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>
fdisk/fdisk.c

index c5e3f06c754f40a1f980922dd4ded50301696937..cc71c5c4928a0095ed5694628f4c585fcfc06702 100644 (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) {