4
0

human.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* human.c -- print human readable file size
  2. Copyright (C) 1996-2007, 2009-2015 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert and Larry McVoy. */
  14. #include <config.h>
  15. #include "human.h"
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <argmatch.h>
  21. #include <error.h>
  22. #include <intprops.h>
  23. /* The maximum length of a suffix like "KiB". */
  24. #define HUMAN_READABLE_SUFFIX_LENGTH_MAX 3
  25. static const char power_letter[] =
  26. {
  27. 0, /* not used */
  28. 'K', /* kibi ('k' for kilo is a special case) */
  29. 'M', /* mega or mebi */
  30. 'G', /* giga or gibi */
  31. 'T', /* tera or tebi */
  32. 'P', /* peta or pebi */
  33. 'E', /* exa or exbi */
  34. 'Z', /* zetta or 2**70 */
  35. 'Y' /* yotta or 2**80 */
  36. };
  37. /* If INEXACT_STYLE is not human_round_to_nearest, and if easily
  38. possible, adjust VALUE according to the style. */
  39. static long double
  40. adjust_value (int inexact_style, long double value)
  41. {
  42. /* Do not use the floorl or ceill functions, as that would mean
  43. checking for their presence and possibly linking with the
  44. standard math library, which is a porting pain. So leave the
  45. value alone if it is too large to easily round. */
  46. if (inexact_style != human_round_to_nearest && value < UINTMAX_MAX)
  47. {
  48. uintmax_t u = value;
  49. value = u + (inexact_style == human_ceiling && u != value);
  50. }
  51. return value;
  52. }
  53. /* Group the digits of NUMBER according to the grouping rules of the
  54. current locale. NUMBER contains NUMBERLEN digits. Modify the
  55. bytes pointed to by NUMBER in place, subtracting 1 from NUMBER for
  56. each byte inserted. Return the starting address of the modified
  57. number.
  58. To group the digits, use GROUPING and THOUSANDS_SEP as in 'struct
  59. lconv' from <locale.h>. */
  60. static char *
  61. group_number (char *number, size_t numberlen,
  62. char const *grouping, char const *thousands_sep)
  63. {
  64. register char *d;
  65. size_t grouplen = SIZE_MAX;
  66. size_t thousands_seplen = strlen (thousands_sep);
  67. size_t i = numberlen;
  68. /* The maximum possible value for NUMBERLEN is the number of digits
  69. in the square of the largest uintmax_t, so double the size needed. */
  70. char buf[2 * INT_STRLEN_BOUND (uintmax_t) + 1];
  71. memcpy (buf, number, numberlen);
  72. d = number + numberlen;
  73. for (;;)
  74. {
  75. unsigned char g = *grouping;
  76. if (g)
  77. {
  78. grouplen = g < CHAR_MAX ? g : i;
  79. grouping++;
  80. }
  81. if (i < grouplen)
  82. grouplen = i;
  83. d -= grouplen;
  84. i -= grouplen;
  85. memcpy (d, buf + i, grouplen);
  86. if (i == 0)
  87. return d;
  88. d -= thousands_seplen;
  89. memcpy (d, thousands_sep, thousands_seplen);
  90. }
  91. }
  92. /* Convert N to a human readable format in BUF, using the options OPTS.
  93. N is expressed in units of FROM_BLOCK_SIZE. FROM_BLOCK_SIZE must
  94. be nonnegative.
  95. Use units of TO_BLOCK_SIZE in the output number. TO_BLOCK_SIZE
  96. must be positive.
  97. Use (OPTS & (human_round_to_nearest | human_floor | human_ceiling))
  98. to determine whether to take the ceiling or floor of any result
  99. that cannot be expressed exactly.
  100. If (OPTS & human_group_digits), group the thousands digits
  101. according to the locale, e.g., "1,000,000" in an American English
  102. locale.
  103. If (OPTS & human_autoscale), deduce the output block size
  104. automatically; TO_BLOCK_SIZE must be 1 but it has no effect on the
  105. output. Use powers of 1024 if (OPTS & human_base_1024), and powers
  106. of 1000 otherwise. For example, assuming powers of 1024, 8500
  107. would be converted to 8.3, 133456345 to 127, 56990456345 to 53, and
  108. so on. Numbers smaller than the power aren't modified.
  109. human_autoscale is normally used together with human_SI.
  110. If (OPTS & human_space_before_unit), use a space to separate the
  111. number from any suffix that is appended as described below.
  112. If (OPTS & human_SI), append an SI prefix indicating which power is
  113. being used. If in addition (OPTS & human_B), append "B" (if base
  114. 1000) or "iB" (if base 1024) to the SI prefix. When ((OPTS &
  115. human_SI) && ! (OPTS & human_autoscale)), TO_BLOCK_SIZE must be a
  116. power of 1024 or of 1000, depending on (OPTS &
  117. human_base_1024). */
  118. char *
  119. human_readable (uintmax_t n, char *buf, int opts,
  120. uintmax_t from_block_size, uintmax_t to_block_size)
  121. {
  122. int inexact_style =
  123. opts & (human_round_to_nearest | human_floor | human_ceiling);
  124. unsigned int base = opts & human_base_1024 ? 1024 : 1000;
  125. uintmax_t amt;
  126. int tenths;
  127. int exponent = -1;
  128. int exponent_max = sizeof power_letter - 1;
  129. char *p;
  130. char *psuffix;
  131. char const *integerlim;
  132. /* 0 means adjusted N == AMT.TENTHS;
  133. 1 means AMT.TENTHS < adjusted N < AMT.TENTHS + 0.05;
  134. 2 means adjusted N == AMT.TENTHS + 0.05;
  135. 3 means AMT.TENTHS + 0.05 < adjusted N < AMT.TENTHS + 0.1. */
  136. int rounding;
  137. char const *decimal_point = ".";
  138. size_t decimal_pointlen = 1;
  139. char const *grouping = "";
  140. char const *thousands_sep = "";
  141. struct lconv const *l = localeconv ();
  142. size_t pointlen = strlen (l->decimal_point);
  143. if (0 < pointlen && pointlen <= MB_LEN_MAX)
  144. {
  145. decimal_point = l->decimal_point;
  146. decimal_pointlen = pointlen;
  147. }
  148. grouping = l->grouping;
  149. if (strlen (l->thousands_sep) <= MB_LEN_MAX)
  150. thousands_sep = l->thousands_sep;
  151. psuffix = buf + LONGEST_HUMAN_READABLE - HUMAN_READABLE_SUFFIX_LENGTH_MAX;
  152. p = psuffix;
  153. /* Adjust AMT out of FROM_BLOCK_SIZE units and into TO_BLOCK_SIZE
  154. units. If this can be done exactly with integer arithmetic, do
  155. not use floating point operations. */
  156. if (to_block_size <= from_block_size)
  157. {
  158. if (from_block_size % to_block_size == 0)
  159. {
  160. uintmax_t multiplier = from_block_size / to_block_size;
  161. amt = n * multiplier;
  162. if (amt / multiplier == n)
  163. {
  164. tenths = 0;
  165. rounding = 0;
  166. goto use_integer_arithmetic;
  167. }
  168. }
  169. }
  170. else if (from_block_size != 0 && to_block_size % from_block_size == 0)
  171. {
  172. uintmax_t divisor = to_block_size / from_block_size;
  173. uintmax_t r10 = (n % divisor) * 10;
  174. uintmax_t r2 = (r10 % divisor) * 2;
  175. amt = n / divisor;
  176. tenths = r10 / divisor;
  177. rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2);
  178. goto use_integer_arithmetic;
  179. }
  180. {
  181. /* Either the result cannot be computed easily using uintmax_t,
  182. or from_block_size is zero. Fall back on floating point.
  183. FIXME: This can yield answers that are slightly off. */
  184. long double dto_block_size = to_block_size;
  185. long double damt = n * (from_block_size / dto_block_size);
  186. size_t buflen;
  187. size_t nonintegerlen;
  188. if (! (opts & human_autoscale))
  189. {
  190. sprintf (buf, "%.0Lf", adjust_value (inexact_style, damt));
  191. buflen = strlen (buf);
  192. nonintegerlen = 0;
  193. }
  194. else
  195. {
  196. long double e = 1;
  197. exponent = 0;
  198. do
  199. {
  200. e *= base;
  201. exponent++;
  202. }
  203. while (e * base <= damt && exponent < exponent_max);
  204. damt /= e;
  205. sprintf (buf, "%.1Lf", adjust_value (inexact_style, damt));
  206. buflen = strlen (buf);
  207. nonintegerlen = decimal_pointlen + 1;
  208. if (1 + nonintegerlen + ! (opts & human_base_1024) < buflen
  209. || ((opts & human_suppress_point_zero)
  210. && buf[buflen - 1] == '0'))
  211. {
  212. sprintf (buf, "%.0Lf",
  213. adjust_value (inexact_style, damt * 10) / 10);
  214. buflen = strlen (buf);
  215. nonintegerlen = 0;
  216. }
  217. }
  218. p = psuffix - buflen;
  219. memmove (p, buf, buflen);
  220. integerlim = p + buflen - nonintegerlen;
  221. }
  222. goto do_grouping;
  223. use_integer_arithmetic:
  224. {
  225. /* The computation can be done exactly, with integer arithmetic.
  226. Use power of BASE notation if requested and if adjusted AMT is
  227. large enough. */
  228. if (opts & human_autoscale)
  229. {
  230. exponent = 0;
  231. if (base <= amt)
  232. {
  233. do
  234. {
  235. unsigned int r10 = (amt % base) * 10 + tenths;
  236. unsigned int r2 = (r10 % base) * 2 + (rounding >> 1);
  237. amt /= base;
  238. tenths = r10 / base;
  239. rounding = (r2 < base
  240. ? (r2 + rounding) != 0
  241. : 2 + (base < r2 + rounding));
  242. exponent++;
  243. }
  244. while (base <= amt && exponent < exponent_max);
  245. if (amt < 10)
  246. {
  247. if (inexact_style == human_round_to_nearest
  248. ? 2 < rounding + (tenths & 1)
  249. : inexact_style == human_ceiling && 0 < rounding)
  250. {
  251. tenths++;
  252. rounding = 0;
  253. if (tenths == 10)
  254. {
  255. amt++;
  256. tenths = 0;
  257. }
  258. }
  259. if (amt < 10
  260. && (tenths || ! (opts & human_suppress_point_zero)))
  261. {
  262. *--p = '0' + tenths;
  263. p -= decimal_pointlen;
  264. memcpy (p, decimal_point, decimal_pointlen);
  265. tenths = rounding = 0;
  266. }
  267. }
  268. }
  269. }
  270. if (inexact_style == human_round_to_nearest
  271. ? 5 < tenths + (0 < rounding + (amt & 1))
  272. : inexact_style == human_ceiling && 0 < tenths + rounding)
  273. {
  274. amt++;
  275. if ((opts & human_autoscale)
  276. && amt == base && exponent < exponent_max)
  277. {
  278. exponent++;
  279. if (! (opts & human_suppress_point_zero))
  280. {
  281. *--p = '0';
  282. p -= decimal_pointlen;
  283. memcpy (p, decimal_point, decimal_pointlen);
  284. }
  285. amt = 1;
  286. }
  287. }
  288. integerlim = p;
  289. do
  290. {
  291. int digit = amt % 10;
  292. *--p = digit + '0';
  293. }
  294. while ((amt /= 10) != 0);
  295. }
  296. do_grouping:
  297. if (opts & human_group_digits)
  298. p = group_number (p, integerlim - p, grouping, thousands_sep);
  299. if (opts & human_SI)
  300. {
  301. if (exponent < 0)
  302. {
  303. uintmax_t power;
  304. exponent = 0;
  305. for (power = 1; power < to_block_size; power *= base)
  306. if (++exponent == exponent_max)
  307. break;
  308. }
  309. if ((exponent | (opts & human_B)) && (opts & human_space_before_unit))
  310. *psuffix++ = ' ';
  311. if (exponent)
  312. *psuffix++ = (! (opts & human_base_1024) && exponent == 1
  313. ? 'k'
  314. : power_letter[exponent]);
  315. if (opts & human_B)
  316. {
  317. if ((opts & human_base_1024) && exponent)
  318. *psuffix++ = 'i';
  319. *psuffix++ = 'B';
  320. }
  321. }
  322. *psuffix = '\0';
  323. return p;
  324. }
  325. /* The default block size used for output. This number may change in
  326. the future as disks get larger. */
  327. #ifndef DEFAULT_BLOCK_SIZE
  328. # define DEFAULT_BLOCK_SIZE 1024
  329. #endif
  330. static char const *const block_size_args[] = { "human-readable", "si", 0 };
  331. static int const block_size_opts[] =
  332. {
  333. human_autoscale + human_SI + human_base_1024,
  334. human_autoscale + human_SI
  335. };
  336. static uintmax_t
  337. default_block_size (void)
  338. {
  339. return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE;
  340. }
  341. static strtol_error
  342. humblock (char const *spec, uintmax_t *block_size, int *options)
  343. {
  344. int i;
  345. int opts = 0;
  346. if (! spec
  347. && ! (spec = getenv ("BLOCK_SIZE"))
  348. && ! (spec = getenv ("BLOCKSIZE")))
  349. *block_size = default_block_size ();
  350. else
  351. {
  352. if (*spec == '\'')
  353. {
  354. opts |= human_group_digits;
  355. spec++;
  356. }
  357. if (0 <= (i = ARGMATCH (spec, block_size_args, block_size_opts)))
  358. {
  359. opts |= block_size_opts[i];
  360. *block_size = 1;
  361. }
  362. else
  363. {
  364. char *ptr;
  365. strtol_error e = xstrtoumax (spec, &ptr, 0, block_size,
  366. "eEgGkKmMpPtTyYzZ0");
  367. if (e != LONGINT_OK)
  368. {
  369. *options = 0;
  370. return e;
  371. }
  372. for (; ! ('0' <= *spec && *spec <= '9'); spec++)
  373. if (spec == ptr)
  374. {
  375. opts |= human_SI;
  376. if (ptr[-1] == 'B')
  377. opts |= human_B;
  378. if (ptr[-1] != 'B' || ptr[-2] == 'i')
  379. opts |= human_base_1024;
  380. break;
  381. }
  382. }
  383. }
  384. *options = opts;
  385. return LONGINT_OK;
  386. }
  387. enum strtol_error
  388. human_options (char const *spec, int *opts, uintmax_t *block_size)
  389. {
  390. strtol_error e = humblock (spec, block_size, opts);
  391. if (*block_size == 0)
  392. {
  393. *block_size = default_block_size ();
  394. e = LONGINT_INVALID;
  395. }
  396. return e;
  397. }