Mercurial > hg > octave-shane > gnulib-hg
annotate lib/human.c @ 17255:d81be792518a
update from texinfo
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Tue, 01 Jan 2013 15:51:49 -0800 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
1 /* human.c -- print human readable file size |
3688
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16235
diff
changeset
|
3 Copyright (C) 1996-2007, 2009-2013 Free Software Foundation, Inc. |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9137
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9137
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9137
diff
changeset
|
8 (at your option) any later version. |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
9 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
13 GNU General Public License for more details. |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
14 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9137
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
17 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
18 /* Written by Paul Eggert and Larry McVoy. */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
20 #include <config.h> |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
21 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
22 #include "human.h" |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
23 |
5510
fd10cf72c81b
Import chamges from coreutils, so that the code now assumes
Paul Eggert <eggert@cs.ucla.edu>
parents:
5458
diff
changeset
|
24 #include <locale.h> |
4543
a6b4c227ffae
Include stdio.h, for sprintf.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4351
diff
changeset
|
25 #include <stdio.h> |
4658 | 26 #include <stdlib.h> |
27 #include <string.h> | |
4543
a6b4c227ffae
Include stdio.h, for sprintf.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4351
diff
changeset
|
28 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
29 #include <argmatch.h> |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
30 #include <error.h> |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5569
diff
changeset
|
31 #include <intprops.h> |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
32 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
33 /* The maximum length of a suffix like "KiB". */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
34 #define HUMAN_READABLE_SUFFIX_LENGTH_MAX 3 |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
35 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
36 static const char power_letter[] = |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
37 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
38 0, /* not used */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
39 'K', /* kibi ('k' for kilo is a special case) */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
40 'M', /* mega or mebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
41 'G', /* giga or gibi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
42 'T', /* tera or tebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
43 'P', /* peta or pebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
44 'E', /* exa or exbi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
45 'Z', /* zetta or 2**70 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
46 'Y' /* yotta or 2**80 */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
47 }; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
48 |
3688
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
49 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
50 /* If INEXACT_STYLE is not human_round_to_nearest, and if easily |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
51 possible, adjust VALUE according to the style. */ |
3688
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
52 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
53 static long double |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
54 adjust_value (int inexact_style, long double value) |
2593
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
55 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
56 /* Do not use the floorl or ceill functions, as that would mean |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
57 checking for their presence and possibly linking with the |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
58 standard math library, which is a porting pain. So leave the |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
59 value alone if it is too large to easily round. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
60 if (inexact_style != human_round_to_nearest && value < UINTMAX_MAX) |
2593
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
61 { |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
62 uintmax_t u = value; |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
63 value = u + (inexact_style == human_ceiling && u != value); |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
64 } |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
65 |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
66 return value; |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
67 } |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
68 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
69 /* Group the digits of NUMBER according to the grouping rules of the |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
70 current locale. NUMBER contains NUMBERLEN digits. Modify the |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
71 bytes pointed to by NUMBER in place, subtracting 1 from NUMBER for |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
72 each byte inserted. Return the starting address of the modified |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
73 number. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
74 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
75 To group the digits, use GROUPING and THOUSANDS_SEP as in 'struct |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
76 lconv' from <locale.h>. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
77 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
78 static char * |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
79 group_number (char *number, size_t numberlen, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
80 char const *grouping, char const *thousands_sep) |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
81 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
82 register char *d; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
83 size_t grouplen = SIZE_MAX; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
84 size_t thousands_seplen = strlen (thousands_sep); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
85 size_t i = numberlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
86 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
87 /* The maximum possible value for NUMBERLEN is the number of digits |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5569
diff
changeset
|
88 in the square of the largest uintmax_t, so double the size needed. */ |
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5569
diff
changeset
|
89 char buf[2 * INT_STRLEN_BOUND (uintmax_t) + 1]; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
90 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
91 memcpy (buf, number, numberlen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
92 d = number + numberlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
93 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
94 for (;;) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
95 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
96 unsigned char g = *grouping; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
97 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
98 if (g) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
99 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
100 grouplen = g < CHAR_MAX ? g : i; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
101 grouping++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
102 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
103 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
104 if (i < grouplen) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
105 grouplen = i; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
106 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
107 d -= grouplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
108 i -= grouplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
109 memcpy (d, buf + i, grouplen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
110 if (i == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
111 return d; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
112 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
113 d -= thousands_seplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
114 memcpy (d, thousands_sep, thousands_seplen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
115 } |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
116 } |
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
117 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
118 /* Convert N to a human readable format in BUF, using the options OPTS. |
1142
ff40370aead6
(human_readable): Convert to ANSI-style definition.
Jim Meyering <jim@meyering.net>
parents:
1137
diff
changeset
|
119 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
120 N is expressed in units of FROM_BLOCK_SIZE. FROM_BLOCK_SIZE must |
1848
afab915d3e18
(human_readable): Allow from_block_size to be zero.
Jim Meyering <jim@meyering.net>
parents:
1841
diff
changeset
|
121 be nonnegative. |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
122 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
123 Use units of TO_BLOCK_SIZE in the output number. TO_BLOCK_SIZE |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
124 must be positive. |
1142
ff40370aead6
(human_readable): Convert to ANSI-style definition.
Jim Meyering <jim@meyering.net>
parents:
1137
diff
changeset
|
125 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
126 Use (OPTS & (human_round_to_nearest | human_floor | human_ceiling)) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
127 to determine whether to take the ceiling or floor of any result |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
128 that cannot be expressed exactly. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
129 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
130 If (OPTS & human_group_digits), group the thousands digits |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
131 according to the locale, e.g., "1,000,000" in an American English |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
132 locale. |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
133 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
134 If (OPTS & human_autoscale), deduce the output block size |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
135 automatically; TO_BLOCK_SIZE must be 1 but it has no effect on the |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
136 output. Use powers of 1024 if (OPTS & human_base_1024), and powers |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
137 of 1000 otherwise. For example, assuming powers of 1024, 8500 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
138 would be converted to 8.3, 133456345 to 127, 56990456345 to 53, and |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
139 so on. Numbers smaller than the power aren't modified. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
140 human_autoscale is normally used together with human_SI. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
141 |
5458 | 142 If (OPTS & human_space_before_unit), use a space to separate the |
143 number from any suffix that is appended as described below. | |
144 | |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
145 If (OPTS & human_SI), append an SI prefix indicating which power is |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
146 being used. If in addition (OPTS & human_B), append "B" (if base |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
147 1000) or "iB" (if base 1024) to the SI prefix. When ((OPTS & |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
148 human_SI) && ! (OPTS & human_autoscale)), TO_BLOCK_SIZE must be a |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
149 power of 1024 or of 1000, depending on (OPTS & |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
150 human_base_1024). */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
151 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
152 char * |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
153 human_readable (uintmax_t n, char *buf, int opts, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
154 uintmax_t from_block_size, uintmax_t to_block_size) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
155 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
156 int inexact_style = |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
157 opts & (human_round_to_nearest | human_floor | human_ceiling); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
158 unsigned int base = opts & human_base_1024 ? 1024 : 1000; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
159 uintmax_t amt; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
160 int tenths; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
161 int exponent = -1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
162 int exponent_max = sizeof power_letter - 1; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
163 char *p; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
164 char *psuffix; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
165 char const *integerlim; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
166 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
167 /* 0 means adjusted N == AMT.TENTHS; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
168 1 means AMT.TENTHS < adjusted N < AMT.TENTHS + 0.05; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
169 2 means adjusted N == AMT.TENTHS + 0.05; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
170 3 means AMT.TENTHS + 0.05 < adjusted N < AMT.TENTHS + 0.1. */ |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
171 int rounding; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
172 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
173 char const *decimal_point = "."; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
174 size_t decimal_pointlen = 1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
175 char const *grouping = ""; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
176 char const *thousands_sep = ""; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
177 struct lconv const *l = localeconv (); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
178 size_t pointlen = strlen (l->decimal_point); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
179 if (0 < pointlen && pointlen <= MB_LEN_MAX) |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
180 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
181 decimal_point = l->decimal_point; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
182 decimal_pointlen = pointlen; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
183 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
184 grouping = l->grouping; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
185 if (strlen (l->thousands_sep) <= MB_LEN_MAX) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
186 thousands_sep = l->thousands_sep; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
187 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
188 psuffix = buf + LONGEST_HUMAN_READABLE - HUMAN_READABLE_SUFFIX_LENGTH_MAX; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
189 p = psuffix; |
2590
a3c74265aa47
(human_readable_inexact): Allow an input block
Jim Meyering <jim@meyering.net>
parents:
2293
diff
changeset
|
190 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
191 /* Adjust AMT out of FROM_BLOCK_SIZE units and into TO_BLOCK_SIZE |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
192 units. If this can be done exactly with integer arithmetic, do |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
193 not use floating point operations. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
194 if (to_block_size <= from_block_size) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
195 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
196 if (from_block_size % to_block_size == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
197 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
198 uintmax_t multiplier = from_block_size / to_block_size; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
199 amt = n * multiplier; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
200 if (amt / multiplier == n) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
201 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
202 tenths = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
203 rounding = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
204 goto use_integer_arithmetic; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
205 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
206 } |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
207 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
208 else if (from_block_size != 0 && to_block_size % from_block_size == 0) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
209 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
210 uintmax_t divisor = to_block_size / from_block_size; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
211 uintmax_t r10 = (n % divisor) * 10; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
212 uintmax_t r2 = (r10 % divisor) * 2; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
213 amt = n / divisor; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
214 tenths = r10 / divisor; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
215 rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
216 goto use_integer_arithmetic; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
217 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
218 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
219 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
220 /* Either the result cannot be computed easily using uintmax_t, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
221 or from_block_size is zero. Fall back on floating point. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
222 FIXME: This can yield answers that are slightly off. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
223 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
224 long double dto_block_size = to_block_size; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
225 long double damt = n * (from_block_size / dto_block_size); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
226 size_t buflen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
227 size_t nonintegerlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
228 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
229 if (! (opts & human_autoscale)) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
230 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
231 sprintf (buf, "%.0Lf", adjust_value (inexact_style, damt)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
232 buflen = strlen (buf); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
233 nonintegerlen = 0; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
234 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
235 else |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
236 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
237 long double e = 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
238 exponent = 0; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
239 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
240 do |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
241 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
242 e *= base; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
243 exponent++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
244 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
245 while (e * base <= damt && exponent < exponent_max); |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
246 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
247 damt /= e; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
248 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
249 sprintf (buf, "%.1Lf", adjust_value (inexact_style, damt)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
250 buflen = strlen (buf); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
251 nonintegerlen = decimal_pointlen + 1; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
252 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
253 if (1 + nonintegerlen + ! (opts & human_base_1024) < buflen |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
254 || ((opts & human_suppress_point_zero) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
255 && buf[buflen - 1] == '0')) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
256 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
257 sprintf (buf, "%.0Lf", |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
258 adjust_value (inexact_style, damt * 10) / 10); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
259 buflen = strlen (buf); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
260 nonintegerlen = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
261 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
262 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
263 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
264 p = psuffix - buflen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
265 memmove (p, buf, buflen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
266 integerlim = p + buflen - nonintegerlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
267 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
268 goto do_grouping; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
269 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
270 use_integer_arithmetic: |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
271 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
272 /* The computation can be done exactly, with integer arithmetic. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
273 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
274 Use power of BASE notation if requested and if adjusted AMT is |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
275 large enough. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
276 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
277 if (opts & human_autoscale) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
278 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
279 exponent = 0; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
280 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
281 if (base <= amt) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
282 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
283 do |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
284 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
285 unsigned int r10 = (amt % base) * 10 + tenths; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
286 unsigned int r2 = (r10 % base) * 2 + (rounding >> 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
287 amt /= base; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
288 tenths = r10 / base; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
289 rounding = (r2 < base |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
290 ? (r2 + rounding) != 0 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
291 : 2 + (base < r2 + rounding)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
292 exponent++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
293 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
294 while (base <= amt && exponent < exponent_max); |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
295 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
296 if (amt < 10) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
297 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
298 if (inexact_style == human_round_to_nearest |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
299 ? 2 < rounding + (tenths & 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
300 : inexact_style == human_ceiling && 0 < rounding) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
301 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
302 tenths++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
303 rounding = 0; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
304 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
305 if (tenths == 10) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
306 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
307 amt++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
308 tenths = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
309 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
310 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
311 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
312 if (amt < 10 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
313 && (tenths || ! (opts & human_suppress_point_zero))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
314 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
315 *--p = '0' + tenths; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
316 p -= decimal_pointlen; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
317 memcpy (p, decimal_point, decimal_pointlen); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
318 tenths = rounding = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
319 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
320 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
321 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
322 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
323 |
4620
b0acbb185a60
(human_readable): Fix bug that rounded 10501 to 10k.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4543
diff
changeset
|
324 if (inexact_style == human_round_to_nearest |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
325 ? 5 < tenths + (0 < rounding + (amt & 1)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
326 : inexact_style == human_ceiling && 0 < tenths + rounding) |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
327 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
328 amt++; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
329 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
330 if ((opts & human_autoscale) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
331 && amt == base && exponent < exponent_max) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
332 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
333 exponent++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
334 if (! (opts & human_suppress_point_zero)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
335 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
336 *--p = '0'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
337 p -= decimal_pointlen; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
338 memcpy (p, decimal_point, decimal_pointlen); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
339 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
340 amt = 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
341 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
342 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
343 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
344 integerlim = p; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
345 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
346 do |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
347 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
348 int digit = amt % 10; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
349 *--p = digit + '0'; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
350 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
351 while ((amt /= 10) != 0); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
352 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
353 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
354 do_grouping: |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
355 if (opts & human_group_digits) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
356 p = group_number (p, integerlim - p, grouping, thousands_sep); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
357 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
358 if (opts & human_SI) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
359 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
360 if (exponent < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
361 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
362 uintmax_t power; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
363 exponent = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
364 for (power = 1; power < to_block_size; power *= base) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
365 if (++exponent == exponent_max) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
366 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
367 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
368 |
5458 | 369 if ((exponent | (opts & human_B)) && (opts & human_space_before_unit)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
370 *psuffix++ = ' '; |
5458 | 371 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
372 if (exponent) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
373 *psuffix++ = (! (opts & human_base_1024) && exponent == 1 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
374 ? 'k' |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
375 : power_letter[exponent]); |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
376 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
377 if (opts & human_B) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
378 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
379 if ((opts & human_base_1024) && exponent) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
380 *psuffix++ = 'i'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
381 *psuffix++ = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
382 } |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
383 } |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
384 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
385 *psuffix = '\0'; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
386 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
387 return p; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
388 } |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
389 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
390 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
391 /* The default block size used for output. This number may change in |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
392 the future as disks get larger. */ |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
393 #ifndef DEFAULT_BLOCK_SIZE |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
394 # define DEFAULT_BLOCK_SIZE 1024 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
395 #endif |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
396 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
397 static char const *const block_size_args[] = { "human-readable", "si", 0 }; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
398 static int const block_size_opts[] = |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
399 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
400 human_autoscale + human_SI + human_base_1024, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
401 human_autoscale + human_SI |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
402 }; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
403 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
404 static uintmax_t |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
405 default_block_size (void) |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
406 { |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
407 return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE; |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
408 } |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
409 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
410 static strtol_error |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
411 humblock (char const *spec, uintmax_t *block_size, int *options) |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
412 { |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
413 int i; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
414 int opts = 0; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
415 |
4985
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
416 if (! spec |
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
417 && ! (spec = getenv ("BLOCK_SIZE")) |
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
418 && ! (spec = getenv ("BLOCKSIZE"))) |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
419 *block_size = default_block_size (); |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
420 else |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
421 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
422 if (*spec == '\'') |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
423 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
424 opts |= human_group_digits; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
425 spec++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
426 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
427 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
428 if (0 <= (i = ARGMATCH (spec, block_size_args, block_size_opts))) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
429 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
430 opts |= block_size_opts[i]; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
431 *block_size = 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
432 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
433 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
434 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
435 char *ptr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
436 strtol_error e = xstrtoumax (spec, &ptr, 0, block_size, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
437 "eEgGkKmMpPtTyYzZ0"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
438 if (e != LONGINT_OK) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
439 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
440 *options = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
441 return e; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
442 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
443 for (; ! ('0' <= *spec && *spec <= '9'); spec++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
444 if (spec == ptr) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
445 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
446 opts |= human_SI; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
447 if (ptr[-1] == 'B') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
448 opts |= human_B; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
449 if (ptr[-1] != 'B' || ptr[-2] == 'i') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
450 opts |= human_base_1024; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
451 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
452 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
453 } |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
454 } |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
455 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
456 *options = opts; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
457 return LONGINT_OK; |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
458 } |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
459 |
9137
629a6928f108
* NEWS: Describe interface changes to human, xstrtol.
Paul Eggert <eggert@cs.ucla.edu>
parents:
9127
diff
changeset
|
460 enum strtol_error |
629a6928f108
* NEWS: Describe interface changes to human, xstrtol.
Paul Eggert <eggert@cs.ucla.edu>
parents:
9127
diff
changeset
|
461 human_options (char const *spec, int *opts, uintmax_t *block_size) |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
462 { |
9137
629a6928f108
* NEWS: Describe interface changes to human, xstrtol.
Paul Eggert <eggert@cs.ucla.edu>
parents:
9127
diff
changeset
|
463 strtol_error e = humblock (spec, block_size, opts); |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
464 if (*block_size == 0) |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
465 { |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
466 *block_size = default_block_size (); |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
467 e = LONGINT_INVALID; |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
468 } |
9137
629a6928f108
* NEWS: Describe interface changes to human, xstrtol.
Paul Eggert <eggert@cs.ucla.edu>
parents:
9127
diff
changeset
|
469 return e; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
470 } |