]> git.cworth.org Git - tar/blob - lib/human.h
Imported Upstream version 1.20
[tar] / lib / human.h
1 /* human.h -- print human readable file size
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4    2005, 2006, 2007 Free Software Foundation, Inc.
5
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 3 of the License, or
9    (at your option) any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Paul Eggert and Larry McVoy.  */
20
21 #ifndef HUMAN_H_
22 # define HUMAN_H_ 1
23
24 # include <limits.h>
25 # include <stdbool.h>
26 # include <stdint.h>
27 # include <unistd.h>
28
29 # include <xstrtol.h>
30
31 /* A conservative bound on the maximum length of a human-readable string.
32    The output can be the square of the largest uintmax_t, so double
33    its size before converting to a bound.
34    log10 (2.0) < 146/485.  Add 1 for integer division truncation.
35    Also, the output can have a thousands separator between every digit,
36    so multiply by MB_LEN_MAX + 1 and then subtract MB_LEN_MAX.
37    Append 1 for a space before the suffix.
38    Finally, append 3, the maximum length of a suffix.  */
39 # define LONGEST_HUMAN_READABLE \
40   ((2 * sizeof (uintmax_t) * CHAR_BIT * 146 / 485 + 1) * (MB_LEN_MAX + 1) \
41    - MB_LEN_MAX + 1 + 3)
42
43 /* Options for human_readable.  */
44 enum
45 {
46   /* Unless otherwise specified these options may be ORed together.  */
47
48   /* The following three options are mutually exclusive.  */
49   /* Round to plus infinity (default).  */
50   human_ceiling = 0,
51   /* Round to nearest, ties to even.  */
52   human_round_to_nearest = 1,
53   /* Round to minus infinity.  */
54   human_floor = 2,
55
56   /* Group digits together, e.g. `1,000,000'.  This uses the
57      locale-defined grouping; the traditional C locale does not group,
58      so this has effect only if some other locale is in use.  */
59   human_group_digits = 4,
60
61   /* When autoscaling, suppress ".0" at end.  */
62   human_suppress_point_zero = 8,
63
64   /* Scale output and use SI-style units, ignoring the output block size.  */
65   human_autoscale = 16,
66
67   /* Prefer base 1024 to base 1000.  */
68   human_base_1024 = 32,
69
70   /* Prepend " " before unit symbol.  */
71   human_space_before_unit = 64,
72
73   /* Append SI prefix, e.g. "k" or "M".  */
74   human_SI = 128,
75
76   /* Append "B" (if base 1000) or "iB" (if base 1024) to SI prefix.  */
77   human_B = 256
78 };
79
80 char *human_readable (uintmax_t, char *, int, uintmax_t, uintmax_t);
81
82 enum strtol_error human_options (char const *, int *, uintmax_t *);
83
84 #endif /* HUMAN_H_ */