1 /* strtoimax - convert string representation of a number into an intmax_t value. */
3 /* Copyright 1999-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 /* Written by Paul Eggert. Modified by Chet Ramey for Bash. */
28 # include <inttypes.h>
41 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
42 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
44 #ifndef HAVE_DECL_STRTOL
45 "this configure-time declaration test was not run"
48 extern long strtol
PARAMS((const char *, char **, int));
51 #ifndef HAVE_DECL_STRTOLL
52 "this configure-time declaration test was not run"
54 #if !HAVE_DECL_STRTOLL && HAVE_LONG_LONG_INT
55 extern long long strtoll
PARAMS((const char *, char **, int));
63 strtoimax (ptr
, endptr
, base
)
68 #if HAVE_LONG_LONG_INT
69 verify(size_is_that_of_long_or_long_long
,
70 (sizeof (intmax_t) == sizeof (long) ||
71 sizeof (intmax_t) == sizeof (long long)));
73 if (sizeof (intmax_t) != sizeof (long))
74 return (strtoll (ptr
, endptr
, base
));
76 verify (size_is_that_of_long
, sizeof (intmax_t) == sizeof (long));
79 return (strtol (ptr
, endptr
, base
));
89 #if HAVE_LONG_LONG_INT
94 printf ("sizeof intmax_t: %d\n", sizeof (intmax_t));
96 #if HAVE_LONG_LONG_INT
97 printf ("sizeof long long: %d\n", sizeof (long long));
99 printf ("sizeof long: %d\n", sizeof (long));
101 x
= strtoimax("42", &endptr
, 10);
102 #if HAVE_LONG_LONG_INT
103 y
= strtoll("42", &endptr
, 10);
107 z
= strtol("42", &endptr
, 10);
109 printf ("%lld %lld %ld\n", x
, y
, z
);