[illumos-Developer] Formatting human readable numbers

Jason King jason.brian.king at gmail.com
Tue Jan 11 21:22:04 PST 2011


Currently, in swap(1m), du(1), df(1), and ls(1) they all support a -h
flag for printing values in human readable formats (i.e. 1.5k, 25M,
etc.).  du and df also support a -H flag to use powers of 10 instead
of 2, but the same concept.  The source for all of these all use a
function number_to_scaled_string to generate the output.  It looks
very much like it was almost copy/pasted verbatim between all of them.
 It seems silly to have 4 implementations of the same function
floating around.
I think this is a fairly good (and simple) candidate to put into a
central library.  The questions are: which library would be most
appropriate, should this be classified as (private, etc.), and should
the prototype change any.  I'd like some guidance on these.  I'm not
aware if there are equivalent functions in one of the BSDs or Linux,
though if anyone is aware, that might be a good place to start.

The current definition is
#define NUMBER_WIDTH 64
typedef char numbuf_t[NUMBER_WIDTH];

char *number_to_scaled_string(
	numbuf_t buf,			/* put the result here */
	unsigned long long number,	/* convert this number */
	unsigned long long unit_from,	/* number of bytes per input unit */

	unsigned long long scale)	/* 1024 (-h)  or 1000 (-H) */



More information about the Developer mailing list