3 /* Convert string representation of a number into an intmax_t value.
5 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
7 This program 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 2, or (at your option)
12 This program 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 this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Paul Eggert. */
28 # include <inttypes.h>
36 # if defined PROTOTYPES || defined __STDC__
37 # define PARAMS(Args) Args
39 # define PARAMS(Args) ()
43 /* Verify a requirement at compile-time (unlike assert, which is runtime). */
44 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
47 # ifndef HAVE_DECL_STRTOUL
48 "this configure-time declaration test was not run"
50 # if !HAVE_DECL_STRTOUL
51 unsigned long strtoul
PARAMS ((char const *, char **, int));
53 # ifndef HAVE_DECL_STRTOULL
54 "this configure-time declaration test was not run"
56 # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
57 unsigned long long strtoull
PARAMS ((char const *, char **, int));
62 # ifndef HAVE_DECL_STRTOL
63 "this configure-time declaration test was not run"
65 # if !HAVE_DECL_STRTOL
66 long strtol
PARAMS ((char const *, char **, int));
68 # ifndef HAVE_DECL_STRTOLL
69 "this configure-time declaration test was not run"
71 # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
72 long long strtoll
PARAMS ((char const *, char **, int));
77 # undef HAVE_LONG_LONG
78 # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
79 # define INT uintmax_t
80 # define strtoimax strtoumax
81 # define strtol strtoul
82 # define strtoll strtoull
88 strtoimax (char const *ptr
, char **endptr
, int base
)
91 verify (size_is_that_of_long_or_long_long
,
92 (sizeof (INT
) == sizeof (long)
93 || sizeof (INT
) == sizeof (long long)));
95 if (sizeof (INT
) != sizeof (long))
96 return strtoll (ptr
, endptr
, base
);
98 verify (size_is_that_of_long
,
99 sizeof (INT
) == sizeof (long));
102 return strtol (ptr
, endptr
, base
);