1 /* human.c -- print human readable file size
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Originally contributed by lm@sgi.com;
19 --si, output block size selection, and large file support
20 added by eggert@twinsun.com. */
26 #include <sys/types.h>
46 #ifndef HAVE_DECL_GETENV
47 "this configure-time declaration test was not run"
55 # define _(Text) gettext (Text)
66 static const char suffixes
[] =
79 /* If INEXACT_STYLE is not human_round_to_even, and if easily
80 possible, adjust VALUE according to the style. */
82 adjust_value (enum human_inexact_style inexact_style
, double value
)
84 /* Do not use the floor or ceil functions, as that would mean
85 linking with the standard math library, which is a porting pain.
86 So leave the value alone if it is too large to easily round. */
87 if (inexact_style
!= human_round_to_even
&& value
< (uintmax_t) -1)
90 value
= u
+ (inexact_style
== human_ceiling
&& u
!= value
);
96 /* Like human_readable_inexact, except always round to even. */
98 human_readable (uintmax_t n
, char *buf
,
99 int from_block_size
, int output_block_size
)
101 return human_readable_inexact (n
, buf
, from_block_size
, output_block_size
,
102 human_round_to_even
);
105 /* Convert N to a human readable format in BUF.
107 N is expressed in units of FROM_BLOCK_SIZE. FROM_BLOCK_SIZE must
110 OUTPUT_BLOCK_SIZE must be nonzero. If it is positive, use units of
111 OUTPUT_BLOCK_SIZE in the output number.
113 Use INEXACT_STYLE to determine whether to take the ceiling or floor
114 of any result that cannot be expressed exactly.
116 If OUTPUT_BLOCK_SIZE is negative, use a format like "127k" if
117 possible, using powers of -OUTPUT_BLOCK_SIZE; otherwise, use
118 ordinary decimal format. Normally -OUTPUT_BLOCK_SIZE is either
119 1000 or 1024; it must be at least 2. Most people visually process
120 strings of 3-4 digits effectively, but longer strings of digits are
121 more prone to misinterpretation. Hence, converting to an
122 abbreviated form usually improves readability. Use a suffix
123 indicating which power is being used. For example, assuming
124 -OUTPUT_BLOCK_SIZE is 1024, 8500 would be converted to 8.3k,
125 133456345 to 127M, 56990456345 to 53G, and so on. Numbers smaller
126 than -OUTPUT_BLOCK_SIZE aren't modified. */
129 human_readable_inexact (uintmax_t n
, char *buf
,
130 int from_block_size
, int output_block_size
,
131 enum human_inexact_style inexact_style
)
140 /* 0 means adjusted N == AMT.TENTHS;
141 1 means AMT.TENTHS < adjusted N < AMT.TENTHS + 0.05;
142 2 means adjusted N == AMT.TENTHS + 0.05;
143 3 means AMT.TENTHS + 0.05 < adjusted N < AMT.TENTHS + 0.1. */
146 if (output_block_size
< 0)
148 base
= -output_block_size
;
154 to_block_size
= output_block_size
;
157 p
= buf
+ LONGEST_HUMAN_READABLE
;
161 /* Suppress `used before initialized' warning. */
165 /* Adjust AMT out of FROM_BLOCK_SIZE units and into TO_BLOCK_SIZE units. */
172 if (to_block_size
<= from_block_size
173 ? (from_block_size
% to_block_size
!= 0
174 || (multiplier
= from_block_size
/ to_block_size
,
175 (amt
= n
* multiplier
) / multiplier
!= n
))
176 : (from_block_size
== 0
177 || to_block_size
% from_block_size
!= 0
178 || (divisor
= to_block_size
/ from_block_size
,
179 r10
= (n
% divisor
) * 10,
180 r2
= (r10
% divisor
) * 2,
182 tenths
= r10
/ divisor
,
183 rounding
= r2
< divisor
? 0 < r2
: 2 + (divisor
< r2
),
186 /* Either the result cannot be computed easily using uintmax_t,
187 or from_block_size is zero. Fall back on floating point.
188 FIXME: This can yield answers that are slightly off. */
190 double damt
= n
* (from_block_size
/ (double) to_block_size
);
193 sprintf (buf
, "%.0f", adjust_value (inexact_style
, damt
));
204 while (e
* base
<= damt
&& power
< sizeof suffixes
- 1);
208 sprintf (buf
, "%.1f%c", adjust_value (inexact_style
, damt
),
210 if (4 < strlen (buf
))
211 sprintf (buf
, "%.0f%c",
212 adjust_value (inexact_style
, damt
* 10) / 10,
220 /* Use power of BASE notation if adjusted AMT is large enough. */
222 if (base
&& base
<= amt
)
228 int r10
= (amt
% base
) * 10 + tenths
;
229 int r2
= (r10
% base
) * 2 + (rounding
>> 1);
232 rounding
= (r2
< base
234 : 2 + (base
< r2
+ rounding
));
237 while (base
<= amt
&& power
< sizeof suffixes
- 1);
239 *--p
= suffixes
[power
];
243 if (2 * (1 - (int) inexact_style
)
244 < rounding
+ (tenths
& (inexact_style
== human_round_to_even
)))
260 tenths
= rounding
= 0;
265 if (inexact_style
== human_ceiling
266 ? 0 < tenths
+ rounding
267 : inexact_style
== human_round_to_even
268 ? 5 < tenths
+ (2 < rounding
+ (amt
& 1))
269 : /* inexact_style == human_floor */ 0)
273 if (amt
== base
&& power
< sizeof suffixes
- 1)
275 *p
= suffixes
[power
+ 1];
283 *--p
= '0' + (int) (amt
% 10);
284 while ((amt
/= 10) != 0);
290 /* The default block size used for output. This number may change in
291 the future as disks get larger. */
292 #ifndef DEFAULT_BLOCK_SIZE
293 # define DEFAULT_BLOCK_SIZE 1024
296 static char const *const block_size_args
[] = { "human-readable", "si", 0 };
297 static int const block_size_types
[] = { -1024, -1000 };
300 default_block_size (void)
302 return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE
;
306 humblock (char const *spec
, int *block_size
)
310 if (! spec
&& ! (spec
= getenv ("BLOCK_SIZE")))
311 *block_size
= default_block_size ();
312 else if (0 <= (i
= ARGMATCH (spec
, block_size_args
, block_size_types
)))
313 *block_size
= block_size_types
[i
];
318 strtol_error e
= xstrtoul (spec
, &ptr
, 0, &val
, "eEgGkKmMpPtTyYzZ0");
322 return LONGINT_INVALID_SUFFIX_CHAR
;
323 if ((int) val
< 0 || val
!= (int) val
)
324 return LONGINT_OVERFLOW
;
325 *block_size
= (int) val
;
332 human_block_size (char const *spec
, int report_errors
, int *block_size
)
334 strtol_error e
= humblock (spec
, block_size
);
335 if (*block_size
== 0)
337 *block_size
= default_block_size ();
340 if (e
!= LONGINT_OK
&& report_errors
)
341 STRTOL_FATAL_ERROR (spec
, _("block size"), e
);