Mercurial > hg > octave-shane > gnulib-hg
annotate lib/human.c @ 5691:ec62790f0938
Factor int-properties macros into a single file, except for
glibc-related files.
* lib/intprops.h: New file.
* lib/getloadavg.c: Include it instead of limits.h.
(INT_STRLEN_BOUND): Remove.
* lib/human.c: Include intprops.h.
(group_number): Use INT_STRLEN_BOUND instead of rolling it ourself.
* lib/human.h (LONGEST_HUMAN_READABLE): Use 146/485 rather than 302/1000.
* lib/inttostr.h: Include intprops.h instead of limits.h.
(INT_STRLEN_BOUND, INT_BUFSIZE_BOUND): Remove.
* lib/mktime.c (TYPE_IS_INTEGER, TYPE_TWOS_COMPLEMENT): New macros,
for consistency with intprops.h.
(time_t_is_integer, twos_complement_arithmetic): Use them.
* lib/sig2str.h: Include <signal.h>, intprops.h.
(INT_STRLEN_BOUND): Remove.
* lib/strftime.c (TYPE_SIGNED): Remove.
(INT_STRLEN_BOUND): Switch to same implementation as intprops.h.
* lib/strtol.c: Adjust comments to match intprops.h.
* lib/userspec.c: Include intprops.h.
(TYPE_SIGNED, TYPE_MINIMUM, TYPE_MAXIMUM): Remove.
* lib/utimecmp.c, lib/xnanosleep.c, lib/xstrtol.c: Likewise.
* lib/utimecmp.c (utimecmp): Use TYPE_IS_INTEGER, TYPE_TWOS_COMPLEMENT
instead of rolling our own expressions.
* lib/xstrtol.c: Include xstrtol.h first, to test interface.
* modules/getloadavg (Files): Add lib/intprops.h.
* modules/human (Files): Likewise.
* modules/inttostr (Files): Likewise.
* modules/sig2str (Files): Likewise.
* modules/userspec (Files): Likewise.
* modules/utimecmp (Files): Likewise.
* modules/xnanosleep (Files): Likewise.
* modules/xstrtol (Files): Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 09 Mar 2005 19:11:23 +0000 |
parents | 5b0578864f09 |
children | 2ef479ccc298 |
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 |
5569
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
4985
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
4 Free Software Foundation, Inc. |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
5 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
8 the Free Software Foundation; either version 2, or (at your option) |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
9 any later version. |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
10 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
11 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
|
12 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
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
14 GNU General Public License for more details. |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
15 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
17 along with this program; if not, write to the Free Software Foundation, |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
19 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
20 /* Written by Paul Eggert and Larry McVoy. */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
21 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
22 #if HAVE_CONFIG_H |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
23 # include <config.h> |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
24 #endif |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
25 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
26 #include "human.h" |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
27 |
5510
fd10cf72c81b
Import chamges from coreutils, so that the code now assumes
Paul Eggert <eggert@cs.ucla.edu>
parents:
5458
diff
changeset
|
28 #include <locale.h> |
4543
a6b4c227ffae
Include stdio.h, for sprintf.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4351
diff
changeset
|
29 #include <stdio.h> |
4658 | 30 #include <stdlib.h> |
31 #include <string.h> | |
4543
a6b4c227ffae
Include stdio.h, for sprintf.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4351
diff
changeset
|
32 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3688
diff
changeset
|
33 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3688
diff
changeset
|
34 #define _(msgid) gettext (msgid) |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
35 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
36 #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
|
37 #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
|
38 #include <intprops.h> |
1786
027e08403b18
Include xstrtol.h, not xstrtoul.h, since xstrtol.h now defines all the
Jim Meyering <jim@meyering.net>
parents:
1596
diff
changeset
|
39 #include <xstrtol.h> |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
40 |
5569
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
41 #ifndef SIZE_MAX |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
42 # define SIZE_MAX ((size_t) -1) |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
43 #endif |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
44 #ifndef UINTMAX_MAX |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
45 # define UINTMAX_MAX ((uintmax_t) -1) |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
46 #endif |
5b0578864f09
* human.c (SIZE_MAX, UINTMAX_MAX): Move these conditional
Paul Eggert <eggert@cs.ucla.edu>
parents:
5510
diff
changeset
|
47 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
48 /* 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
|
49 #define HUMAN_READABLE_SUFFIX_LENGTH_MAX 3 |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
50 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
51 static const char power_letter[] = |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
52 { |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
53 0, /* not used */ |
3688
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
54 'K', /* kibi ('k' for kilo is a special case) */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
55 'M', /* mega or mebi */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
56 'G', /* giga or gibi */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
57 'T', /* tera or tebi */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
58 'P', /* peta or pebi */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
59 'E', /* exa or exbi */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
60 'Z', /* zetta or 2**70 */ |
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
61 'Y' /* yotta or 2**80 */ |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
62 }; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
63 |
3688
0cd372c853e5
(suffixes): Prefer K to k for 1024.
Jim Meyering <jim@meyering.net>
parents:
2593
diff
changeset
|
64 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
65 /* 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
|
66 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
|
67 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
68 static long double |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
69 adjust_value (int inexact_style, long double value) |
2593
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
70 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
71 /* 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 { |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
77 uintmax_t u = value; |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
78 value = u + (inexact_style == human_ceiling && u != value); |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
79 } |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
80 |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
81 return value; |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
82 } |
3ebe28cfe9b4
(adjust_value): New function.
Jim Meyering <jim@meyering.net>
parents:
2590
diff
changeset
|
83 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
84 /* 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
|
85 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
|
86 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
|
87 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
|
88 number. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
89 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
90 To group the digits, use GROUPING and THOUSANDS_SEP as in `struct |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
91 lconv' from <locale.h>. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
92 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
93 static char * |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
94 group_number (char *number, size_t numberlen, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
95 char const *grouping, char const *thousands_sep) |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
96 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
97 register char *d; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
98 size_t grouplen = SIZE_MAX; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
99 size_t thousands_seplen = strlen (thousands_sep); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
100 size_t i = numberlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
101 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
102 /* 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
|
103 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
|
104 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
|
105 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
106 memcpy (buf, number, numberlen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
107 d = number + numberlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
108 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
109 for (;;) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
110 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
111 unsigned char g = *grouping; |
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 if (g) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
114 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
115 grouplen = g < CHAR_MAX ? g : i; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
116 grouping++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
117 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
118 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
119 if (i < grouplen) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
120 grouplen = i; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
121 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
122 d -= grouplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
123 i -= grouplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
124 memcpy (d, buf + i, grouplen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
125 if (i == 0) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
126 return d; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
127 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
128 d -= thousands_seplen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
129 memcpy (d, thousands_sep, thousands_seplen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
130 } |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
131 } |
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
132 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
133 /* 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
|
134 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
135 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
|
136 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
|
137 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
138 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
|
139 must be positive. |
1142
ff40370aead6
(human_readable): Convert to ANSI-style definition.
Jim Meyering <jim@meyering.net>
parents:
1137
diff
changeset
|
140 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
141 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
|
142 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
|
143 that cannot be expressed exactly. |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
144 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
145 If (OPTS & human_group_digits), group the thousands digits |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
146 according to the locale, e.g., `1,000,000' in an American English |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
147 locale. |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
148 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 |
5458 | 157 If (OPTS & human_space_before_unit), use a space to separate the |
158 number from any suffix that is appended as described below. | |
159 | |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 human_base_1024). */ |
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 char * |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
168 human_readable (uintmax_t n, char *buf, int opts, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
169 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
|
170 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
171 int inexact_style = |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
172 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
|
173 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
|
174 uintmax_t amt; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
175 int tenths; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
176 int exponent = -1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
177 int exponent_max = sizeof power_letter - 1; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
178 char *p; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
179 char *psuffix; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
180 char const *integerlim; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
181 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
182 /* 0 means adjusted N == AMT.TENTHS; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
183 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
|
184 2 means adjusted N == AMT.TENTHS + 0.05; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
185 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
|
186 int rounding; |
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 char const *decimal_point = "."; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
189 size_t decimal_pointlen = 1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
190 char const *grouping = ""; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
191 char const *thousands_sep = ""; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
192 struct lconv const *l = localeconv (); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
193 size_t pointlen = strlen (l->decimal_point); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
194 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
|
195 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
196 decimal_point = l->decimal_point; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
197 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
|
198 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
199 grouping = l->grouping; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
200 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
|
201 thousands_sep = l->thousands_sep; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
202 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
203 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
|
204 p = psuffix; |
2590
a3c74265aa47
(human_readable_inexact): Allow an input block
Jim Meyering <jim@meyering.net>
parents:
2293
diff
changeset
|
205 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
206 /* 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
|
207 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
|
208 not use floating point operations. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
209 if (to_block_size <= from_block_size) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
210 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
211 if (from_block_size % to_block_size == 0) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
212 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
213 uintmax_t multiplier = from_block_size / to_block_size; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
214 amt = n * multiplier; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
215 if (amt / multiplier == n) |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
216 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
217 tenths = 0; |
1863
e6b535c4a46e
(human_readable): New function.
Jim Meyering <jim@meyering.net>
parents:
1848
diff
changeset
|
218 rounding = 0; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
219 goto use_integer_arithmetic; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
220 } |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
221 } |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
222 } |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
223 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
|
224 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
225 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
|
226 uintmax_t r10 = (n % divisor) * 10; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
227 uintmax_t r2 = (r10 % divisor) * 2; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
228 amt = n / divisor; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
229 tenths = r10 / divisor; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
230 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
|
231 goto use_integer_arithmetic; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
232 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
233 |
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 /* 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
|
236 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
|
237 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
|
238 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
239 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
|
240 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
|
241 size_t buflen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
242 size_t nonintegerlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
243 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
244 if (! (opts & human_autoscale)) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
245 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
246 sprintf (buf, "%.0Lf", adjust_value (inexact_style, damt)); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
247 buflen = strlen (buf); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
248 nonintegerlen = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
249 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
250 else |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
251 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
252 long double e = 1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
253 exponent = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
254 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
255 do |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
256 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
257 e *= base; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
258 exponent++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
259 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
260 while (e * base <= damt && exponent < exponent_max); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
261 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
262 damt /= e; |
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 sprintf (buf, "%.1Lf", adjust_value (inexact_style, damt)); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
265 buflen = strlen (buf); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
266 nonintegerlen = decimal_pointlen + 1; |
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 if (1 + nonintegerlen + ! (opts & human_base_1024) < buflen |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
269 || ((opts & human_suppress_point_zero) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
270 && buf[buflen - 1] == '0')) |
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 sprintf (buf, "%.0Lf", |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
273 adjust_value (inexact_style, damt * 10) / 10); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
274 buflen = strlen (buf); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
275 nonintegerlen = 0; |
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 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
278 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
279 p = psuffix - buflen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
280 memmove (p, buf, buflen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
281 integerlim = p + buflen - nonintegerlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
282 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
283 goto do_grouping; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
284 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
285 use_integer_arithmetic: |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
286 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
287 /* 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
|
288 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
289 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
|
290 large enough. */ |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
291 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
292 if (opts & human_autoscale) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
293 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
294 exponent = 0; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
295 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
296 if (base <= amt) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
297 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
298 do |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
299 { |
5159 | 300 unsigned int r10 = (amt % base) * 10 + tenths; |
301 unsigned int r2 = (r10 % base) * 2 + (rounding >> 1); | |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
302 amt /= base; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
303 tenths = r10 / base; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
304 rounding = (r2 < base |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
305 ? (r2 + rounding) != 0 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
306 : 2 + (base < r2 + rounding)); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
307 exponent++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
308 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
309 while (base <= amt && exponent < exponent_max); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
310 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
311 if (amt < 10) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
312 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
313 if (inexact_style == human_round_to_nearest |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
314 ? 2 < rounding + (tenths & 1) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
315 : inexact_style == human_ceiling && 0 < rounding) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
316 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
317 tenths++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
318 rounding = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
319 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
320 if (tenths == 10) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
321 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
322 amt++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
323 tenths = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
324 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
325 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
326 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
327 if (amt < 10 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
328 && (tenths || ! (opts & human_suppress_point_zero))) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
329 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
330 *--p = '0' + tenths; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
331 p -= decimal_pointlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
332 memcpy (p, decimal_point, decimal_pointlen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
333 tenths = rounding = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
334 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
335 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
336 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
337 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
338 |
4620
b0acbb185a60
(human_readable): Fix bug that rounded 10501 to 10k.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4543
diff
changeset
|
339 if (inexact_style == human_round_to_nearest |
b0acbb185a60
(human_readable): Fix bug that rounded 10501 to 10k.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4543
diff
changeset
|
340 ? 5 < tenths + (0 < rounding + (amt & 1)) |
b0acbb185a60
(human_readable): Fix bug that rounded 10501 to 10k.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4543
diff
changeset
|
341 : 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
|
342 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
343 amt++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
344 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
345 if ((opts & human_autoscale) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
346 && amt == base && exponent < exponent_max) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
347 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
348 exponent++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
349 if (! (opts & human_suppress_point_zero)) |
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 *--p = '0'; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
352 p -= decimal_pointlen; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
353 memcpy (p, decimal_point, decimal_pointlen); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
354 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
355 amt = 1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
356 } |
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 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
359 integerlim = p; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
360 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
361 do |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
362 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
363 int digit = amt % 10; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
364 *--p = digit + '0'; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
365 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
366 while ((amt /= 10) != 0); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
367 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
368 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
369 do_grouping: |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
370 if (opts & human_group_digits) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
371 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
|
372 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
373 if (opts & human_SI) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
374 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
375 if (exponent < 0) |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
376 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
377 uintmax_t power; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
378 exponent = 0; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
379 for (power = 1; power < to_block_size; power *= base) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
380 if (++exponent == exponent_max) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
381 break; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
382 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
383 |
5458 | 384 if ((exponent | (opts & human_B)) && (opts & human_space_before_unit)) |
385 *psuffix++ = ' '; | |
386 | |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
387 if (exponent) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
388 *psuffix++ = (! (opts & human_base_1024) && exponent == 1 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
389 ? 'k' |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
390 : power_letter[exponent]); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
391 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
392 if (opts & human_B) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
393 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
394 if ((opts & human_base_1024) && exponent) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
395 *psuffix++ = 'i'; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
396 *psuffix++ = 'B'; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
397 } |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
398 } |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
399 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
400 *psuffix = '\0'; |
1137
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
401 |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
402 return p; |
2c9059ccc457
New file. The interface is inspired
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
403 } |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
404 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
405 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
406 /* 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
|
407 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
|
408 #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
|
409 # 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
|
410 #endif |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
411 |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
412 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
|
413 static int const block_size_opts[] = |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
414 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
415 human_autoscale + human_SI + human_base_1024, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
416 human_autoscale + human_SI |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
417 }; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
418 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
419 static uintmax_t |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
420 default_block_size (void) |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
421 { |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
422 return getenv ("POSIXLY_CORRECT") ? 512 : DEFAULT_BLOCK_SIZE; |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
423 } |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
424 |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
425 static strtol_error |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
426 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
|
427 { |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
428 int i; |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
429 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
|
430 |
4985
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
431 if (! spec |
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
432 && ! (spec = getenv ("BLOCK_SIZE")) |
41bd93f66780
(humblock): Inspect BLOCKSIZE if BLOCK_SIZE isn't set.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4788
diff
changeset
|
433 && ! (spec = getenv ("BLOCKSIZE"))) |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
434 *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
|
435 else |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
436 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
437 if (*spec == '\'') |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
438 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
439 opts |= human_group_digits; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
440 spec++; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
441 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
442 |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
443 if (0 <= (i = ARGMATCH (spec, block_size_args, block_size_opts))) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
444 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
445 opts |= block_size_opts[i]; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
446 *block_size = 1; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
447 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
448 else |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
449 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
450 char *ptr; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
451 strtol_error e = xstrtoumax (spec, &ptr, 0, block_size, |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
452 "eEgGkKmMpPtTyYzZ0"); |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
453 if (e != LONGINT_OK) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
454 return e; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
455 for (; ! ('0' <= *spec && *spec <= '9'); spec++) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
456 if (spec == ptr) |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
457 { |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
458 opts |= human_SI; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
459 if (ptr[-1] == 'B') |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
460 opts |= human_B; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
461 if (ptr[-1] != 'B' || ptr[-2] == 'i') |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
462 opts |= human_base_1024; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
463 break; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
464 } |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
465 } |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
466 } |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
467 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
468 *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
|
469 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
|
470 } |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
471 |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
472 int |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
473 human_options (char const *spec, bool report_errors, 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
|
474 { |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
475 int opts; |
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
476 strtol_error e = humblock (spec, block_size, &opts); |
2008
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
477 if (*block_size == 0) |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
478 { |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
479 *block_size = default_block_size (); |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
480 e = LONGINT_INVALID; |
2d8ab27e705c
(default_block_size): New function.
Jim Meyering <jim@meyering.net>
parents:
1863
diff
changeset
|
481 } |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
482 if (e != LONGINT_OK && report_errors) |
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
483 STRTOL_FATAL_ERROR (spec, _("block size"), e); |
4351
d0d257fdad20
Merge human.c changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4333
diff
changeset
|
484 return opts; |
1391
c9a7b76bd443
* lib/human.c, lib/human.h (human_readable): Coalesce last two args
Jim Meyering <jim@meyering.net>
parents:
1142
diff
changeset
|
485 } |