*** empty log message ***
[coreutils.git] / lib / human.c
blob92b051cae593ce9c41e0e54ee8eae1ae9e1a2ab8
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)
7 any later version.
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. */
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <sys/types.h>
27 #include <stdio.h>
29 #if HAVE_LIMITS_H
30 # include <limits.h>
31 #endif
33 #if HAVE_STRING_H
34 # include <string.h>
35 #else
36 # include <strings.h>
37 #endif
39 #ifndef CHAR_BIT
40 # define CHAR_BIT 8
41 #endif
42 #if HAVE_STDLIB_H
43 # include <stdlib.h>
44 #endif
46 #ifndef HAVE_DECL_GETENV
47 "this configure-time declaration test was not run"
48 #endif
49 #if !HAVE_DECL_GETENV
50 char *getenv ();
51 #endif
53 #if ENABLE_NLS
54 # include <libintl.h>
55 # define _(Text) gettext (Text)
56 #else
57 # define _(Text) Text
58 #endif
60 #include <argmatch.h>
61 #include <error.h>
62 #include <xstrtol.h>
64 #include "human.h"
66 static const char suffixes[] =
68 0, /* not used */
69 'k', /* kilo */
70 'M', /* Mega */
71 'G', /* Giga */
72 'T', /* Tera */
73 'P', /* Peta */
74 'E', /* Exa */
75 'Z', /* Zetta */
76 'Y' /* Yotta */
79 /* If INEXACT_STYLE is not human_round_to_even, and if easily
80 possible, adjust VALUE according to the style. */
81 static double
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)
89 uintmax_t u = value;
90 value = u + (inexact_style == human_ceiling && u != value);
93 return value;
96 /* Like human_readable_inexact, except always round to even. */
97 char *
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
108 be nonnegative.
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. */
128 char *
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)
133 uintmax_t amt;
134 int base;
135 int to_block_size;
136 int tenths = 0;
137 int power;
138 char *p;
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. */
144 int rounding = 0;
146 if (output_block_size < 0)
148 base = -output_block_size;
149 to_block_size = 1;
151 else
153 base = 0;
154 to_block_size = output_block_size;
157 p = buf + LONGEST_HUMAN_READABLE;
158 *p = '\0';
160 #ifdef lint
161 /* Suppress `used before initialized' warning. */
162 power = 0;
163 #endif
165 /* Adjust AMT out of FROM_BLOCK_SIZE units and into TO_BLOCK_SIZE units. */
168 int multiplier;
169 int divisor;
170 int r2;
171 int r10;
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,
181 amt = n / divisor,
182 tenths = r10 / divisor,
183 rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2),
184 0)))
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);
192 if (! base)
193 sprintf (buf, "%.0f", adjust_value (inexact_style, damt));
194 else
196 double e = 1;
197 power = 0;
201 e *= base;
202 power++;
204 while (e * base <= damt && power < sizeof suffixes - 1);
206 damt /= e;
208 sprintf (buf, "%.1f%c", adjust_value (inexact_style, damt),
209 suffixes[power]);
210 if (4 < strlen (buf))
211 sprintf (buf, "%.0f%c",
212 adjust_value (inexact_style, damt * 10) / 10,
213 suffixes[power]);
216 return buf;
220 /* Use power of BASE notation if adjusted AMT is large enough. */
222 if (base && base <= amt)
224 power = 0;
228 int r10 = (amt % base) * 10 + tenths;
229 int r2 = (r10 % base) * 2 + (rounding >> 1);
230 amt /= base;
231 tenths = r10 / base;
232 rounding = (r2 < base
233 ? 0 < r2 + rounding
234 : 2 + (base < r2 + rounding));
235 power++;
237 while (base <= amt && power < sizeof suffixes - 1);
239 *--p = suffixes[power];
241 if (amt < 10)
243 if (2 * (1 - (int) inexact_style)
244 < rounding + (tenths & (inexact_style == human_round_to_even)))
246 tenths++;
247 rounding = 0;
249 if (tenths == 10)
251 amt++;
252 tenths = 0;
256 if (amt < 10)
258 *--p = '0' + tenths;
259 *--p = '.';
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)
271 amt++;
273 if (amt == base && power < sizeof suffixes - 1)
275 *p = suffixes[power + 1];
276 *--p = '0';
277 *--p = '.';
278 amt = 1;
283 *--p = '0' + (int) (amt % 10);
284 while ((amt /= 10) != 0);
286 return p;
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
294 #endif
296 static char const *const block_size_args[] = { "human-readable", "si", 0 };
297 static int const block_size_types[] = { -1024, -1000 };
299 static int
300 default_block_size (void)
302 return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE;
305 static strtol_error
306 humblock (char const *spec, int *block_size)
308 int i;
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];
314 else
316 char *ptr;
317 unsigned long val;
318 strtol_error e = xstrtoul (spec, &ptr, 0, &val, "eEgGkKmMpPtTyYzZ0");
319 if (e != LONGINT_OK)
320 return e;
321 if (*ptr)
322 return LONGINT_INVALID_SUFFIX_CHAR;
323 if ((int) val < 0 || val != (int) val)
324 return LONGINT_OVERFLOW;
325 *block_size = (int) val;
328 return LONGINT_OK;
331 void
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 ();
338 e = LONGINT_INVALID;
340 if (e != LONGINT_OK && report_errors)
341 STRTOL_FATAL_ERROR (spec, _("block size"), e);