dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0426-Factor-out-human-size-printing.patch

f96e0b
From d58051e416c525401a2cd86a0fe7750a7f9c1a62 Mon Sep 17 00:00:00 2001
f96e0b
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
f96e0b
Date: Sun, 5 May 2013 11:31:53 +0200
f96e0b
Subject: [PATCH 426/482] 	Factor-out human-size printing.
f96e0b
f96e0b
---
f96e0b
 ChangeLog               |  4 ++++
f96e0b
 grub-core/commands/ls.c | 31 +------------------------------
f96e0b
 grub-core/normal/misc.c | 34 ++++++++++++++++++++++++++++++++++
f96e0b
 include/grub/normal.h   |  3 +++
f96e0b
 4 files changed, 42 insertions(+), 30 deletions(-)
f96e0b
f96e0b
diff --git a/ChangeLog b/ChangeLog
f96e0b
index 57bce50..c9e6f06 100644
f96e0b
--- a/ChangeLog
f96e0b
+++ b/ChangeLog
f96e0b
@@ -1,3 +1,7 @@
f96e0b
+2013-05-05  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
+
f96e0b
+	Factor-out human-size printing.
f96e0b
+
f96e0b
 2013-05-04  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
 
f96e0b
 	Agglomerate more mallocs to speed-up gfxterm.
f96e0b
diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
f96e0b
index 83930aa..6c608f6 100644
f96e0b
--- a/grub-core/commands/ls.c
f96e0b
+++ b/grub-core/commands/ls.c
f96e0b
@@ -43,8 +43,6 @@ static const struct grub_arg_option options[] =
f96e0b
     {0, 0, 0, 0, 0, 0}
f96e0b
   };
f96e0b
 
f96e0b
-static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
f96e0b
-
f96e0b
 /* Helper for grub_ls_list_devices.  */
f96e0b
 static int
f96e0b
 grub_ls_print_devices (const char *name, void *data)
f96e0b
@@ -143,34 +141,7 @@ print_files_long (const char *filename, const struct grub_dirhook_info *info,
f96e0b
       if (! ctx->human)
f96e0b
 	grub_printf ("%-12llu", (unsigned long long) file->size);
f96e0b
       else
f96e0b
-	{
f96e0b
-	  grub_uint64_t fsize = file->size * 100ULL;
f96e0b
-	  grub_uint64_t fsz = file->size;
f96e0b
-	  int units = 0;
f96e0b
-	  char buf[20];
f96e0b
-
f96e0b
-	  while (fsz / 1024)
f96e0b
-	    {
f96e0b
-	      fsize = (fsize + 512) / 1024;
f96e0b
-	      fsz /= 1024;
f96e0b
-	      units++;
f96e0b
-	    }
f96e0b
-
f96e0b
-	  if (units)
f96e0b
-	    {
f96e0b
-	      grub_uint64_t whole, fraction;
f96e0b
-
f96e0b
-	      whole = grub_divmod64 (fsize, 100, &fraction);
f96e0b
-	      grub_snprintf (buf, sizeof (buf),
f96e0b
-			     "%" PRIuGRUB_UINT64_T
f96e0b
-			     ".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
f96e0b
-			     grub_human_sizes[units]);
f96e0b
-	      grub_printf ("%-12s", buf);
f96e0b
-	    }
f96e0b
-	  else
f96e0b
-	    grub_printf ("%-12llu", (unsigned long long) file->size);
f96e0b
-
f96e0b
-	}
f96e0b
+	grub_printf ("%-12s", grub_get_human_size (file->size, 1));
f96e0b
       grub_file_close (file);
f96e0b
       grub_free (pathname);
f96e0b
     }
f96e0b
diff --git a/grub-core/normal/misc.c b/grub-core/normal/misc.c
f96e0b
index d23de62..1a86e0f 100644
f96e0b
--- a/grub-core/normal/misc.c
f96e0b
+++ b/grub-core/normal/misc.c
f96e0b
@@ -28,6 +28,40 @@
f96e0b
 #include <grub/i18n.h>
f96e0b
 #include <grub/partition.h>
f96e0b
 
f96e0b
+static const char *grub_human_sizes[] = {N_("B"), N_("KiB"), N_("MiB"), N_("GiB"), N_("TiB")};
f96e0b
+static const char *grub_human_short_sizes[] = {"", "K", "M", "G", "T"};
f96e0b
+
f96e0b
+const char *
f96e0b
+grub_get_human_size (grub_uint64_t size, int sh)
f96e0b
+{
f96e0b
+  grub_uint64_t fsize = size * 100ULL;
f96e0b
+  grub_uint64_t fsz = size;
f96e0b
+  int units = 0;
f96e0b
+  static char buf[20];
f96e0b
+
f96e0b
+  while (fsz / 1024)
f96e0b
+    {
f96e0b
+      fsize = (fsize + 512) / 1024;
f96e0b
+      fsz /= 1024;
f96e0b
+      units++;
f96e0b
+    }
f96e0b
+
f96e0b
+  if (units)
f96e0b
+    {
f96e0b
+      grub_uint64_t whole, fraction;
f96e0b
+
f96e0b
+      whole = grub_divmod64 (fsize, 100, &fraction);
f96e0b
+      grub_snprintf (buf, sizeof (buf),
f96e0b
+		     "%" PRIuGRUB_UINT64_T
f96e0b
+		     ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
f96e0b
+		     sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
f96e0b
+    }
f96e0b
+  else
f96e0b
+    grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
f96e0b
+		   sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
f96e0b
+  return buf;
f96e0b
+}
f96e0b
+
f96e0b
 /* Print the information on the device NAME.  */
f96e0b
 grub_err_t
f96e0b
 grub_normal_print_device_info (const char *name)
f96e0b
diff --git a/include/grub/normal.h b/include/grub/normal.h
f96e0b
index 4fcc3da..930b3b9 100644
f96e0b
--- a/include/grub/normal.h
f96e0b
+++ b/include/grub/normal.h
f96e0b
@@ -150,4 +150,7 @@ grub_dyncmd_get_cmd (grub_command_t cmd);
f96e0b
 void
f96e0b
 grub_gettext_reread_prefix (const char *val);
f96e0b
 
f96e0b
+const char *
f96e0b
+grub_get_human_size (grub_uint64_t size, int sh);
f96e0b
+
f96e0b
 #endif /* ! GRUB_NORMAL_HEADER */
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b