1 /* Convert string representation of a number into an uintmax_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_STRTOUL
38 "this configure-time declaration test was not run"
40 #if !HAVE_DECL_STRTOUL
41 extern unsigned long strtoul __P((const char *, char **, int));
44 #ifndef HAVE_DECL_STRTOULL
45 "this configure-time declaration test was not run"
47 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
48 extern unsigned long long strtoull __P((const char *, char **, int));
56 strtoumax (ptr, endptr, base)
61 #if HAVE_UNSIGNED_LONG_LONG
62 verify (size_is_that_of_unsigned_long_or_unsigned_long_long,
63 (sizeof (uintmax_t) == sizeof (unsigned long) ||
64 sizeof (uintmax_t) == sizeof (unsigned long long)));
66 if (sizeof (uintmax_t) != sizeof (unsigned long))
67 return (strtoull (ptr, endptr, base));
69 verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long));
72 return (strtoul (ptr, endptr, base));
82 #if HAVE_UNSIGNED_LONG_LONG
87 printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t));
89 #if HAVE_UNSIGNED_LONG_LONG
90 printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long));
92 printf ("sizeof unsigned long: %d\n", sizeof (unsigned long));
94 x = strtoumax("42", &endptr, 10);
96 y = strtoull("42", &endptr, 10);
100 z = strtoul("42", &endptr, 10);
102 printf ("%llu %llu %lu\n", x, y, z);