public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: cygcheck: add human readable package size output
Date: Sun, 29 Jan 2023 16:52:52 +0000 (GMT)	[thread overview]
Message-ID: <20230129165252.499F43858D32@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c67e6570eef5b745e23d8c9449062b0f9f6c8cc4

commit c67e6570eef5b745e23d8c9449062b0f9f6c8cc4
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Sun Jan 29 17:21:28 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Sun Jan 29 17:21:28 2023 +0100

    Cygwin: cygcheck: add human readable package size output
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/utils/mingw/cygcheck.cc | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/winsup/utils/mingw/cygcheck.cc b/winsup/utils/mingw/cygcheck.cc
index ed1d8f85a2d2..17a7669197df 100644
--- a/winsup/utils/mingw/cygcheck.cc
+++ b/winsup/utils/mingw/cygcheck.cc
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <math.h>
 #include <io.h>
 #include <windows.h>
 #include <wininet.h>
@@ -2343,6 +2344,27 @@ error:
   return NULL;
 }
 
+static const char *
+human_readable (char *buf, size_t bytes)
+{
+  const char *siz[] = { "B", "K", "M", "G", NULL };
+  double db = bytes;
+  int idx = 0;
+  int prec;
+
+  while (bytes > 1023 && siz[idx + 1])
+    {
+      bytes >>= 10;
+      db /= 1024.0;
+      ++idx;
+    }
+  prec = log10 (db) + 1;
+  if (prec < 2)
+    prec = 2;
+  sprintf (buf, "%.*g %s", prec, db, siz[idx]);
+  return buf;
+}
+
 static void
 package_info_print (ini_package_info *pi, vers_info *vers)
 {
@@ -2364,7 +2386,9 @@ package_info_print (ini_package_info *pi, vers_info *vers)
   if (vers->install)
     {
       char *arch = strcpy (buf, vers->install);
-      char *size = NULL;
+      char *sizep;
+      size_t size = 0;
+
       char *cp;
 
       cp = strchr (arch, '/');
@@ -2374,10 +2398,13 @@ package_info_print (ini_package_info *pi, vers_info *vers)
 	  cp = strchr (cp, ' ');
 	  if (cp)
 	    {
-	      size = ++cp;
+	      sizep = ++cp;
 	      cp = strchr (cp, ' ');
 	      if (cp)
-		*cp = '\0';
+		{
+		  *cp = '\0';
+		  size = strtoull (sizep, NULL, 10);
+		}
 	    }
 	}
       if (cp)
@@ -2385,7 +2412,9 @@ package_info_print (ini_package_info *pi, vers_info *vers)
 	  printf ("Architecture: %s\n", arch);
 	  if (vers->install_date)
 	    printf ("Install Date: %s", ctime (&vers->install_date));
-	  printf ("Size        : %s\n", size); /* FIXME: human-readable */
+
+	  printf ("Size        : %llu (%s)\n", size,
+					       human_readable (buf, size));
 	}
     }
   if (vers->source)

                 reply	other threads:[~2023-01-29 16:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230129165252.499F43858D32@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).