1 /* Convert string representation of a number into an intmax_t value.
2 Copyright 1999-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert. Modified by Chet Ramey for Bash. */
25 # include <inttypes.h>
34 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
35 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
37 #ifndef HAVE_DECL_STRTOL
38 "this configure-time declaration test was not run"
41 extern long strtol __P((const char *, char **, int));
44 #ifndef HAVE_DECL_STRTOLL
45 "this configure-time declaration test was not run"
47 #if !HAVE_DECL_STRTOLL && HAVE_LONG_LONG
48 extern long long strtoll __P((const char *, char **, int));
56 strtoimax (ptr, endptr, base)
62 verify(size_is_that_of_long_or_long_long,
63 (sizeof (intmax_t) == sizeof (long) ||
64 sizeof (intmax_t) == sizeof (long long)));
66 if (sizeof (intmax_t) != sizeof (long))
67 return (strtoll (ptr, endptr, base));
69 verify (size_is_that_of_long, sizeof (intmax_t) == sizeof (long));
72 return (strtol (ptr, endptr, base));
87 printf ("sizeof intmax_t: %d\n", sizeof (intmax_t));
90 printf ("sizeof long long: %d\n", sizeof (long long));
92 printf ("sizeof long: %d\n", sizeof (long));
94 x = strtoimax("42", &endptr, 10);
96 y = strtoll("42", &endptr, 10);
100 z = strtol("42", &endptr, 10);
102 printf ("%lld %lld %ld\n", x, y, z);