Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/xstrtol.c @ 17935:0ad1f4c9eed5 default tip
tests: support stderr verification with returns_()
* tests/init.sh (returns_): Disable tracing for this wrapper
function, so that stderr of the wrapped command is unchanged,
allowing for verification of the contents.
author | Pádraig Brady <P@draigBrady.com> |
---|---|
date | Mon, 16 Feb 2015 17:20:39 +0000 |
parents | ab58d4870664 |
children |
rev | line source |
---|---|
617 | 1 /* A more useful interface to strtol. |
4333 | 2 |
17848 | 3 Copyright (C) 1995-1996, 1998-2001, 2003-2007, 2009-2015 Free Software |
12518
b5e42ef33b49
update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents:
12421
diff
changeset
|
4 Foundation, Inc. |
617 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9141
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
617 | 7 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:
9141
diff
changeset
|
8 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:
9141
diff
changeset
|
9 (at your option) any later version. |
617 | 10 |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9141
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
617 | 18 |
741 | 19 /* Written by Jim Meyering. */ |
617 | 20 |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
21 #ifndef __strtol |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
22 # define __strtol strtol |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
23 # define __strtol_t long int |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
24 # define __xstrtol xstrtol |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
25 # define STRTOL_T_MINIMUM LONG_MIN |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
26 # define STRTOL_T_MAXIMUM LONG_MAX |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
27 #endif |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
28 |
7455
8d41d3f68c28
Don't include <config.h> twice; this doesn't work in some cases,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
29 #include <config.h> |
8d41d3f68c28
Don't include <config.h> twice; this doesn't work in some cases,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
30 |
8d41d3f68c28
Don't include <config.h> twice; this doesn't work in some cases,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
31 #include "xstrtol.h" |
8d41d3f68c28
Don't include <config.h> twice; this doesn't work in some cases,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
32 |
1455
08b8008462f8
Remove duplicate include of <stdio.h>.
Jim Meyering <jim@meyering.net>
parents:
1438
diff
changeset
|
33 /* Some pre-ANSI implementations (e.g. SunOS 4) |
08b8008462f8
Remove duplicate include of <stdio.h>.
Jim Meyering <jim@meyering.net>
parents:
1438
diff
changeset
|
34 need stderr defined if assertion checking is enabled. */ |
1425
41271f2d809e
Include stdio.h. Required on some systems when using assert.
Jim Meyering <jim@meyering.net>
parents:
1402
diff
changeset
|
35 #include <stdio.h> |
41271f2d809e
Include stdio.h. Required on some systems when using assert.
Jim Meyering <jim@meyering.net>
parents:
1402
diff
changeset
|
36 |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
37 #include <ctype.h> |
5159 | 38 #include <errno.h> |
39 #include <limits.h> | |
4691 | 40 #include <stdlib.h> |
41 #include <string.h> | |
366 | 42 |
17840 | 43 #include "assure.h" |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
44 #include "intprops.h" |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
45 |
15960
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
46 /* xstrtoll.c and xstrtoull.c, which include this file, require that |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
47 ULLONG_MAX, LLONG_MAX, LLONG_MIN are defined, but <limits.h> does not |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
48 define them on all platforms. */ |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
49 #ifndef ULLONG_MAX |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
50 # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long) |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
51 #endif |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
52 #ifndef LLONG_MAX |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
53 # define LLONG_MAX TYPE_MAXIMUM (long long int) |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
54 #endif |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
55 #ifndef LLONG_MIN |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
56 # define LLONG_MIN TYPE_MINIMUM (long long int) |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
57 #endif |
85f5b7651b9a
xstrtoll: Fix compilation failure.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
58 |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
59 static strtol_error |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
60 bkm_scale (__strtol_t *x, int scale_factor) |
1294
24aee8551316
Merge with the version from textutils.
Jim Meyering <jim@meyering.net>
parents:
1257
diff
changeset
|
61 { |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
62 if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor) |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
63 { |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
64 *x = STRTOL_T_MINIMUM; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
65 return LONGINT_OVERFLOW; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
66 } |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
67 if (STRTOL_T_MAXIMUM / scale_factor < *x) |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
68 { |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
69 *x = STRTOL_T_MAXIMUM; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
70 return LONGINT_OVERFLOW; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
71 } |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
72 *x *= scale_factor; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
73 return LONGINT_OK; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
74 } |
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
75 |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
76 static strtol_error |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
77 bkm_scale_by_power (__strtol_t *x, int base, int power) |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
78 { |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
79 strtol_error err = LONGINT_OK; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
80 while (power--) |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
81 err |= bkm_scale (x, base); |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
82 return err; |
1294
24aee8551316
Merge with the version from textutils.
Jim Meyering <jim@meyering.net>
parents:
1257
diff
changeset
|
83 } |
366 | 84 |
85 /* FIXME: comment. */ | |
86 | |
87 strtol_error | |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
88 __xstrtol (const char *s, char **ptr, int strtol_base, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
89 __strtol_t *val, const char *valid_suffixes) |
366 | 90 { |
91 char *t_ptr; | |
92 char **p; | |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
93 __strtol_t tmp; |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
94 strtol_error err = LONGINT_OK; |
366 | 95 |
17840 | 96 assure (0 <= strtol_base && strtol_base <= 36); |
366 | 97 |
98 p = (ptr ? ptr : &t_ptr); | |
99 | |
17845
e0af97efdba6
xstrtol: ensure errno is reset
Pádraig Brady <P@draigBrady.com>
parents:
17840
diff
changeset
|
100 errno = 0; |
e0af97efdba6
xstrtol: ensure errno is reset
Pádraig Brady <P@draigBrady.com>
parents:
17840
diff
changeset
|
101 |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
102 if (! TYPE_SIGNED (__strtol_t)) |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
103 { |
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
104 const char *q = s; |
5159 | 105 unsigned char ch = *q; |
6927
fa896bb33133
* lib/memcasecmp.c: Include <limits.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
106 while (isspace (ch)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
107 ch = *++q; |
5159 | 108 if (ch == '-') |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
109 return LONGINT_INVALID; |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
110 } |
1499
ed7ad3597c87
(__xstrtol) [STRING_TO_UNSIGNED]: Return
Jim Meyering <jim@meyering.net>
parents:
1455
diff
changeset
|
111 |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
112 tmp = __strtol (s, p, strtol_base); |
3687
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
113 |
366 | 114 if (*p == s) |
3687
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
115 { |
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
116 /* If there is no number but there is a valid suffix, assume the |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
117 number is 1. The string is invalid otherwise. */ |
3687
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
118 if (valid_suffixes && **p && strchr (valid_suffixes, **p)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
119 tmp = 1; |
3687
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
120 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
121 return LONGINT_INVALID; |
3687
855a23bea86f
(__xstrtol): If there is no number but there
Jim Meyering <jim@meyering.net>
parents:
3525
diff
changeset
|
122 } |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
123 else if (errno != 0) |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
124 { |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
125 if (errno != ERANGE) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
126 return LONGINT_INVALID; |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
127 err = LONGINT_OVERFLOW; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
128 } |
870
09bc76111e29
(__xstrtol): Change interpretation of
Jim Meyering <jim@meyering.net>
parents:
758
diff
changeset
|
129 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
130 /* Let valid_suffixes == NULL mean "allow any suffix". */ |
1781
a26162438677
(__strtol, __strtol_t, __xstrtol):
Jim Meyering <jim@meyering.net>
parents:
1499
diff
changeset
|
131 /* FIXME: update all callers except the ones that allow suffixes |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
132 after the number, changing last parameter NULL to "". */ |
368
6907c56379d2
Rearrange to accept a user-specified set of suffix chars (must be
Jim Meyering <jim@meyering.net>
parents:
366
diff
changeset
|
133 if (!valid_suffixes) |
366 | 134 { |
870
09bc76111e29
(__xstrtol): Change interpretation of
Jim Meyering <jim@meyering.net>
parents:
758
diff
changeset
|
135 *val = tmp; |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
136 return err; |
366 | 137 } |
138 | |
737
4b0aafb36f94
(__xstrtol): Return an error for invalid suffix.
Jim Meyering <jim@meyering.net>
parents:
617
diff
changeset
|
139 if (**p != '\0') |
366 | 140 { |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
141 int base = 1024; |
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
142 int suffixes = 1; |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
143 strtol_error overflow; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
144 |
737
4b0aafb36f94
(__xstrtol): Return an error for invalid suffix.
Jim Meyering <jim@meyering.net>
parents:
617
diff
changeset
|
145 if (!strchr (valid_suffixes, **p)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
146 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
147 *val = tmp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
148 return err | LONGINT_INVALID_SUFFIX_CHAR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
149 } |
737
4b0aafb36f94
(__xstrtol): Return an error for invalid suffix.
Jim Meyering <jim@meyering.net>
parents:
617
diff
changeset
|
150 |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
151 if (strchr (valid_suffixes, '0')) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
152 { |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
153 /* The "valid suffix" '0' is a special flag meaning that |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
154 an optional second suffix is allowed, which can change |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
155 the base. A suffix "B" (e.g. "100MB") stands for a power |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
156 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
157 a power of 1024. If no suffix (e.g. "100M"), assume |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
158 power-of-1024. */ |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
159 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
160 switch (p[0][1]) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
161 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
162 case 'i': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
163 if (p[0][2] == 'B') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
164 suffixes += 2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
165 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
166 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
167 case 'B': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
168 case 'D': /* 'D' is obsolescent */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
169 base = 1000; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
170 suffixes++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
171 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
172 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
173 } |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
174 |
368
6907c56379d2
Rearrange to accept a user-specified set of suffix chars (must be
Jim Meyering <jim@meyering.net>
parents:
366
diff
changeset
|
175 switch (**p) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
176 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
177 case 'b': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
178 overflow = bkm_scale (&tmp, 512); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
179 break; |
366 | 180 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
181 case 'B': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
182 overflow = bkm_scale (&tmp, 1024); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
183 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
184 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
185 case 'c': |
17666
889867abe1ae
mbsstr, quotearg, xstrtol: pacify IRIX 6.5 cc
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
186 overflow = LONGINT_OK; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
187 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
188 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
189 case 'E': /* exa or exbi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
190 overflow = bkm_scale_by_power (&tmp, base, 6); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
191 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
192 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
193 case 'G': /* giga or gibi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
194 case 'g': /* 'g' is undocumented; for compatibility only */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
195 overflow = bkm_scale_by_power (&tmp, base, 3); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
196 break; |
368
6907c56379d2
Rearrange to accept a user-specified set of suffix chars (must be
Jim Meyering <jim@meyering.net>
parents:
366
diff
changeset
|
197 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
198 case 'k': /* kilo */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
199 case 'K': /* kibi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
200 overflow = bkm_scale_by_power (&tmp, base, 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
201 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
202 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
203 case 'M': /* mega or mebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
204 case 'm': /* 'm' is undocumented; for compatibility only */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
205 overflow = bkm_scale_by_power (&tmp, base, 2); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
206 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
207 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
208 case 'P': /* peta or pebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
209 overflow = bkm_scale_by_power (&tmp, base, 5); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
210 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
211 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
212 case 'T': /* tera or tebi */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
213 case 't': /* 't' is undocumented; for compatibility only */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
214 overflow = bkm_scale_by_power (&tmp, base, 4); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
215 break; |
366 | 216 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
217 case 'w': |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
218 overflow = bkm_scale (&tmp, 2); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
219 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
220 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
221 case 'Y': /* yotta or 2**80 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
222 overflow = bkm_scale_by_power (&tmp, base, 8); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
223 break; |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
224 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
225 case 'Z': /* zetta or 2**70 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
226 overflow = bkm_scale_by_power (&tmp, base, 7); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
227 break; |
366 | 228 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
229 default: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
230 *val = tmp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
231 return err | LONGINT_INVALID_SUFFIX_CHAR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
232 } |
1394
06bd54ef3e0e
* lib/xstrtol.c (bkm_scale): Don't assume that you can convert
Jim Meyering <jim@meyering.net>
parents:
1294
diff
changeset
|
233 |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
234 err |= overflow; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
235 *p += suffixes; |
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
236 if (**p) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
237 err |= LONGINT_INVALID_SUFFIX_CHAR; |
366 | 238 } |
239 | |
240 *val = tmp; | |
4788
33e8f958b651
Handle invalid suffixes and overflow independently, so that
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
241 return err; |
366 | 242 } |