fdisk: +size{k,K,m,M,g,G} chaos (v2)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Dec 13, 2007 at 10:35:13AM +0000, Pádraig Brady wrote:
> If you were changing the meaning, would it be better
> to sync with coreutils? There it is:
> 
> <size> is a number which may be optionally followed
> by the following multiplicative suffixes:
>   b              512 ("blocks")
>   KB            1000 (KiloBytes)
>   K             1024 (KibiBytes)
>   MB       1000*1000 (MegaBytes)
>   M        1024*1024 (MebiBytes)
> and so on for G, T, P, E, Z, Y

 Sounds good. It makes sense to use same suffixes everywhere. (Well,
 except 'b' -- I think we needn't this multiplier.)

 The updated patch is below.

    Karel

 
>From 3cce2b41d9b4b8ca60d04beb4f226af3ef57ff48 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@xxxxxxxxxx>
Date: Thu, 13 Dec 2007 01:06:44 +0100
Subject: [PATCH] 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.

  Old version:
     Last cylinder or +size or +sizeM or +sizeK

  Fixed version:
     Last cylinder, +cylinders, +sizeK, +sizeM or +sizeG

Note, the 10^N suffixes are not proposed to end-uses in the hint
message.

Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 fdisk/fdisk.c |   67 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index c5e3f06..6a1af32 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -1174,33 +1174,54 @@ read_int(unsigned int low, unsigned int dflt, unsigned int high,
 
 			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':
+			if ((*line_ptr == 'C' || *line_ptr == 'c') &&
+			    *(line_ptr + 1) == '\n') {
+				/*
+				 * Cylinders
+				 */
+				if (!display_in_cyl_units)
+					i *= heads * sectors;
+			} else if (*(line_ptr + 1) == 'B' &&
+				   *(line_ptr + 2) == '\n') {
+				/*
+				 * 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) == '\n') {
+				/*
+				 * 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 != '\n')
+				absolute = -1;
+
+			if (absolute == -1)  {
+				int len = strlen(line_ptr) - 1;
+				*(line_ptr+len) = '\0';
+				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 +2082,8 @@ 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 %s, +%s, +sizeK, +sizeM or +sizeG"),
+			 str_units(SINGULAR), str_units(PLURAL));
 		stop = read_int(cround(start), cround(limit), cround(limit),
 				cround(start), mesg);
 		if (display_in_cyl_units) {
-- 
1.5.3.1


-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux