4
0

human.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* human.h -- 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. #ifndef HUMAN_H_
  15. # define HUMAN_H_ 1
  16. # include <limits.h>
  17. # include <stdbool.h>
  18. # include <stdint.h>
  19. # include <unistd.h>
  20. # include <xstrtol.h>
  21. /* A conservative bound on the maximum length of a human-readable string.
  22. The output can be the square of the largest uintmax_t, so double
  23. its size before converting to a bound.
  24. log10 (2.0) < 146/485. Add 1 for integer division truncation.
  25. Also, the output can have a thousands separator between every digit,
  26. so multiply by MB_LEN_MAX + 1 and then subtract MB_LEN_MAX.
  27. Append 1 for a space before the suffix.
  28. Finally, append 3, the maximum length of a suffix. */
  29. # define LONGEST_HUMAN_READABLE \
  30. ((2 * sizeof (uintmax_t) * CHAR_BIT * 146 / 485 + 1) * (MB_LEN_MAX + 1) \
  31. - MB_LEN_MAX + 1 + 3)
  32. /* Options for human_readable. */
  33. enum
  34. {
  35. /* Unless otherwise specified these options may be ORed together. */
  36. /* The following three options are mutually exclusive. */
  37. /* Round to plus infinity (default). */
  38. human_ceiling = 0,
  39. /* Round to nearest, ties to even. */
  40. human_round_to_nearest = 1,
  41. /* Round to minus infinity. */
  42. human_floor = 2,
  43. /* Group digits together, e.g. "1,000,000". This uses the
  44. locale-defined grouping; the traditional C locale does not group,
  45. so this has effect only if some other locale is in use. */
  46. human_group_digits = 4,
  47. /* When autoscaling, suppress ".0" at end. */
  48. human_suppress_point_zero = 8,
  49. /* Scale output and use SI-style units, ignoring the output block size. */
  50. human_autoscale = 16,
  51. /* Prefer base 1024 to base 1000. */
  52. human_base_1024 = 32,
  53. /* Prepend " " before unit symbol. */
  54. human_space_before_unit = 64,
  55. /* Append SI prefix, e.g. "k" or "M". */
  56. human_SI = 128,
  57. /* Append "B" (if base 1000) or "iB" (if base 1024) to SI prefix. */
  58. human_B = 256
  59. };
  60. char *human_readable (uintmax_t, char *, int, uintmax_t, uintmax_t);
  61. enum strtol_error human_options (char const *, int *, uintmax_t *);
  62. #endif /* HUMAN_H_ */