1 /* human.c -- print human readable file size
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by Paul Eggert and Larry McVoy. */
34 #define _(msgid) gettext (msgid)
41 # define SIZE_MAX ((size_t) -1)
44 # define UINTMAX_MAX ((uintmax_t) -1)
47 /* The maximum length of a suffix like "KiB". */
48 #define HUMAN_READABLE_SUFFIX_LENGTH_MAX 3
50 static const char power_letter[] =
53 'K', /* kibi ('k' for kilo is a special case) */
54 'M', /* mega or mebi */
55 'G', /* giga or gibi */
56 'T', /* tera or tebi */
57 'P', /* peta or pebi */
58 'E', /* exa or exbi */
59 'Z', /* zetta or 2**70 */
60 'Y' /* yotta or 2**80 */
64 /* If INEXACT_STYLE is not human_round_to_nearest, and if easily
65 possible, adjust VALUE according to the style. */
68 adjust_value (int inexact_style, long double value)
70 /* Do not use the floorl or ceill functions, as that would mean
71 checking for their presence and possibly linking with the
72 standard math library, which is a porting pain. So leave the
73 value alone if it is too large to easily round. */
74 if (inexact_style != human_round_to_nearest && value < UINTMAX_MAX)
77 value = u + (inexact_style == human_ceiling && u != value);
83 /* Group the digits of NUMBER according to the grouping rules of the
84 current locale. NUMBER contains NUMBERLEN digits. Modify the
85 bytes pointed to by NUMBER in place, subtracting 1 from NUMBER for
86 each byte inserted. Return the starting address of the modified
89 To group the digits, use GROUPING and THOUSANDS_SEP as in `struct
90 lconv' from <locale.h>. */
93 group_number (char *number, size_t numberlen,
94 char const *grouping, char const *thousands_sep)
97 size_t grouplen = SIZE_MAX;
98 size_t thousands_seplen = strlen (thousands_sep);
101 /* The maximum possible value for NUMBERLEN is the number of digits
102 in the square of the largest uintmax_t, so double the size of
103 uintmax_t before converting to a bound. 302 / 1000 is ceil
104 (log10 (2.0)). Add 1 for integer division truncation. */
105 char buf[2 * sizeof (uintmax_t) * CHAR_BIT * 302 / 1000 + 1];
107 memcpy (buf, number, numberlen);
108 d = number + numberlen;
112 unsigned char g = *grouping;
116 grouplen = g < CHAR_MAX ? g : i;
125 memcpy (d, buf + i, grouplen);
129 d -= thousands_seplen;
130 memcpy (d, thousands_sep, thousands_seplen);
134 /* Convert N to a human readable format in BUF, using the options OPTS.
136 N is expressed in units of FROM_BLOCK_SIZE. FROM_BLOCK_SIZE must
139 Use units of TO_BLOCK_SIZE in the output number. TO_BLOCK_SIZE
142 Use (OPTS & (human_round_to_nearest | human_floor | human_ceiling))
143 to determine whether to take the ceiling or floor of any result
144 that cannot be expressed exactly.
146 If (OPTS & human_group_digits), group the thousands digits
147 according to the locale, e.g., `1,000,000' in an American English
150 If (OPTS & human_autoscale), deduce the output block size
151 automatically; TO_BLOCK_SIZE must be 1 but it has no effect on the
152 output. Use powers of 1024 if (OPTS & human_base_1024), and powers
153 of 1000 otherwise. For example, assuming powers of 1024, 8500
154 would be converted to 8.3, 133456345 to 127, 56990456345 to 53, and
155 so on. Numbers smaller than the power aren't modified.
156 human_autoscale is normally used together with human_SI.
158 If (OPTS & human_space_before_unit), use a space to separate the
159 number from any suffix that is appended as described below.
161 If (OPTS & human_SI), append an SI prefix indicating which power is
162 being used. If in addition (OPTS & human_B), append "B" (if base
163 1000) or "iB" (if base 1024) to the SI prefix. When ((OPTS &
164 human_SI) && ! (OPTS & human_autoscale)), TO_BLOCK_SIZE must be a
165 power of 1024 or of 1000, depending on (OPTS &
169 human_readable (uintmax_t n, char *buf, int opts,
170 uintmax_t from_block_size, uintmax_t to_block_size)
173 opts & (human_round_to_nearest | human_floor | human_ceiling);
174 unsigned int base = opts & human_base_1024 ? 1024 : 1000;
178 int exponent_max = sizeof power_letter - 1;
181 char const *integerlim;
183 /* 0 means adjusted N == AMT.TENTHS;
184 1 means AMT.TENTHS < adjusted N < AMT.TENTHS + 0.05;
185 2 means adjusted N == AMT.TENTHS + 0.05;
186 3 means AMT.TENTHS + 0.05 < adjusted N < AMT.TENTHS + 0.1. */
189 char const *decimal_point = ".";
190 size_t decimal_pointlen = 1;
191 char const *grouping = "";
192 char const *thousands_sep = "";
193 struct lconv const *l = localeconv ();
194 size_t pointlen = strlen (l->decimal_point);
195 if (0 < pointlen && pointlen <= MB_LEN_MAX)
197 decimal_point = l->decimal_point;
198 decimal_pointlen = pointlen;
200 grouping = l->grouping;
201 if (strlen (l->thousands_sep) <= MB_LEN_MAX)
202 thousands_sep = l->thousands_sep;
204 psuffix = buf + LONGEST_HUMAN_READABLE - HUMAN_READABLE_SUFFIX_LENGTH_MAX;
207 /* Adjust AMT out of FROM_BLOCK_SIZE units and into TO_BLOCK_SIZE
208 units. If this can be done exactly with integer arithmetic, do
209 not use floating point operations. */
210 if (to_block_size <= from_block_size)
212 if (from_block_size % to_block_size == 0)
214 uintmax_t multiplier = from_block_size / to_block_size;
215 amt = n * multiplier;
216 if (amt / multiplier == n)
220 goto use_integer_arithmetic;
224 else if (from_block_size != 0 && to_block_size % from_block_size == 0)
226 uintmax_t divisor = to_block_size / from_block_size;
227 uintmax_t r10 = (n % divisor) * 10;
228 uintmax_t r2 = (r10 % divisor) * 2;
230 tenths = r10 / divisor;
231 rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2);
232 goto use_integer_arithmetic;
236 /* Either the result cannot be computed easily using uintmax_t,
237 or from_block_size is zero. Fall back on floating point.
238 FIXME: This can yield answers that are slightly off. */
240 long double dto_block_size = to_block_size;
241 long double damt = n * (from_block_size / dto_block_size);
243 size_t nonintegerlen;
245 if (! (opts & human_autoscale))
247 sprintf (buf, "%.0Lf", adjust_value (inexact_style, damt));
248 buflen = strlen (buf);
261 while (e * base <= damt && exponent < exponent_max);
265 sprintf (buf, "%.1Lf", adjust_value (inexact_style, damt));
266 buflen = strlen (buf);
267 nonintegerlen = decimal_pointlen + 1;
269 if (1 + nonintegerlen + ! (opts & human_base_1024) < buflen
270 || ((opts & human_suppress_point_zero)
271 && buf[buflen - 1] == '0'))
273 sprintf (buf, "%.0Lf",
274 adjust_value (inexact_style, damt * 10) / 10);
275 buflen = strlen (buf);
280 p = psuffix - buflen;
281 memmove (p, buf, buflen);
282 integerlim = p + buflen - nonintegerlen;
286 use_integer_arithmetic:
288 /* The computation can be done exactly, with integer arithmetic.
290 Use power of BASE notation if requested and if adjusted AMT is
293 if (opts & human_autoscale)
301 unsigned int r10 = (amt % base) * 10 + tenths;
302 unsigned int r2 = (r10 % base) * 2 + (rounding >> 1);
305 rounding = (r2 < base
306 ? (r2 + rounding) != 0
307 : 2 + (base < r2 + rounding));
310 while (base <= amt && exponent < exponent_max);
314 if (inexact_style == human_round_to_nearest
315 ? 2 < rounding + (tenths & 1)
316 : inexact_style == human_ceiling && 0 < rounding)
329 && (tenths || ! (opts & human_suppress_point_zero)))
332 p -= decimal_pointlen;
333 memcpy (p, decimal_point, decimal_pointlen);
334 tenths = rounding = 0;
340 if (inexact_style == human_round_to_nearest
341 ? 5 < tenths + (0 < rounding + (amt & 1))
342 : inexact_style == human_ceiling && 0 < tenths + rounding)
346 if ((opts & human_autoscale)
347 && amt == base && exponent < exponent_max)
350 if (! (opts & human_suppress_point_zero))
353 p -= decimal_pointlen;
354 memcpy (p, decimal_point, decimal_pointlen);
364 int digit = amt % 10;
367 while ((amt /= 10) != 0);
371 if (opts & human_group_digits)
372 p = group_number (p, integerlim - p, grouping, thousands_sep);
380 for (power = 1; power < to_block_size; power *= base)
381 if (++exponent == exponent_max)
385 if ((exponent | (opts & human_B)) && (opts & human_space_before_unit))
389 *psuffix++ = (! (opts & human_base_1024) && exponent == 1
391 : power_letter[exponent]);
395 if ((opts & human_base_1024) && exponent)
407 /* The default block size used for output. This number may change in
408 the future as disks get larger. */
409 #ifndef DEFAULT_BLOCK_SIZE
410 # define DEFAULT_BLOCK_SIZE 1024
413 static char const *const block_size_args[] = { "human-readable", "si", 0 };
414 static int const block_size_opts[] =
416 human_autoscale + human_SI + human_base_1024,
417 human_autoscale + human_SI
421 default_block_size (void)
423 return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE;
427 humblock (char const *spec, uintmax_t *block_size, int *options)
433 && ! (spec = getenv ("BLOCK_SIZE"))
434 && ! (spec = getenv ("BLOCKSIZE")))
435 *block_size = default_block_size ();
440 opts |= human_group_digits;
444 if (0 <= (i = ARGMATCH (spec, block_size_args, block_size_opts)))
446 opts |= block_size_opts[i];
452 strtol_error e = xstrtoumax (spec, &ptr, 0, block_size,
453 "eEgGkKmMpPtTyYzZ0");
456 for (; ! ('0' <= *spec && *spec <= '9'); spec++)
462 if (ptr[-1] != 'B' || ptr[-2] == 'i')
463 opts |= human_base_1024;
474 human_options (char const *spec, bool report_errors, uintmax_t *block_size)
477 strtol_error e = humblock (spec, block_size, &opts);
478 if (*block_size == 0)
480 *block_size = default_block_size ();
483 if (e != LONGINT_OK && report_errors)
484 STRTOL_FATAL_ERROR (spec, _("block size"), e);