Index | Thread | Search

From:
joshua stein <jcs@jcs.org>
Subject:
dd commas
To:
tech@openbsd.org
Date:
Mon, 18 Nov 2024 13:36:55 -0600

Download raw body.

Thread
  • joshua stein:

    dd commas

Maybe someone else will find this useful, but when I ^T dd and see 
output like 1257557196800, I have to highlight three digits at a 
time in my terminal for my brain to understand if it's at hundreds 
of gigabytes or in terabytes.

Before:

    11994+0 records in
    11993+0 records out
    1257557196800 bytes transferred in 5017.398 secs (250659198 bytes/sec)

After:

    11994+0 records in
    11993+0 records out
    1,257,557,196,800 bytes transferred in 5017.398 secs (250,659,198 bytes/sec)


diff --git bin/dd/extern.h bin/dd/extern.h
index aff85daf021..2f2ce4629ad 100644
--- bin/dd/extern.h
+++ bin/dd/extern.h
@@ -38,6 +38,7 @@
 
 void block(void);
 void block_close(void);
+void commafy(unsigned long long, char *, size_t);
 void dd_out(int);
 void def(void);
 void def_close(void);
diff --git bin/dd/misc.c bin/dd/misc.c
index cd0ba9fa41d..0671c35ed42 100644
--- bin/dd/misc.c
+++ bin/dd/misc.c
@@ -49,6 +49,7 @@
 void
 sig_summary(int notused)
 {
+	char bstr[26], pstr[26];
 	int save_errno = errno;
 	struct timespec elapsed, now;
 	unsigned long long bps, msec;
@@ -85,10 +86,12 @@ sig_summary(int notused)
 		    st.trunc, (st.trunc == 1) ? "block" : "blocks");
 	}
 	if (!(ddflags & C_NOXFER)) {
+		commafy(st.bytes, bstr, sizeof(bstr));
+		commafy(bps, pstr, sizeof(pstr));
 		dprintf(STDERR_FILENO,
-		    "%lld bytes transferred in %lld.%03ld secs "
-		    "(%llu bytes/sec)\n", (long long)st.bytes,
-		    (long long)elapsed.tv_sec, elapsed.tv_nsec / 1000000, bps);
+		    "%s bytes transferred in %lld.%03ld secs "
+		    "(%s bytes/sec)\n", bstr, elapsed.tv_sec,
+		    elapsed.tv_nsec / 1000000, pstr);
 	}
 	errno = save_errno;
 }
@@ -107,3 +110,24 @@ exit_summary(void)
 {
 	sig_summary(0);
 }
+
+void
+commafy(unsigned long long count, char *dst, size_t size)
+{
+	char llstr[26];
+	int n, len, pos, cpos;
+
+	len = snprintf(llstr, sizeof(llstr), "%llu", count);
+	cpos = len + ((len - 1) / 3);
+
+	if (cpos > size)
+		return;
+
+	dst[cpos--] = '\0';
+
+	for (n = 0, pos = len - 1; n < len; n++) {
+		if (n && n % 3 == 0)
+			dst[cpos--] = ',';
+		dst[cpos--] = llstr[pos--];
+	}
+}