1170754Sdelphij/* Convert string representation of a number into an intmax_t value.
2170754Sdelphij   Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3170754Sdelphij
4170754Sdelphij   This program is free software; you can redistribute it and/or modify
5170754Sdelphij   it under the terms of the GNU General Public License as published by
6170754Sdelphij   the Free Software Foundation; either version 2, or (at your option)
7170754Sdelphij   any later version.
8170754Sdelphij
9170754Sdelphij   This program is distributed in the hope that it will be useful,
10170754Sdelphij   but WITHOUT ANY WARRANTY; without even the implied warranty of
11170754Sdelphij   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12170754Sdelphij   GNU General Public License for more details.
13170754Sdelphij
14170754Sdelphij   You should have received a copy of the GNU General Public License
15170754Sdelphij   along with this program; if not, write to the Free Software Foundation,
16170754Sdelphij   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17170754Sdelphij
18170754Sdelphij/* Written by Paul Eggert. */
19170754Sdelphij
20170754Sdelphij#if HAVE_CONFIG_H
21170754Sdelphij# include <config.h>
22170754Sdelphij#endif
23170754Sdelphij
24170754Sdelphij#if HAVE_INTTYPES_H
25170754Sdelphij# include <inttypes.h>
26170754Sdelphij#elif HAVE_STDINT_H
27170754Sdelphij# include <stdint.h>
28170754Sdelphij#endif
29170754Sdelphij
30170754Sdelphij#include <stdlib.h>
31170754Sdelphij
32170754Sdelphij/* Verify a requirement at compile-time (unlike assert, which is runtime).  */
33170754Sdelphij#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
34170754Sdelphij
35170754Sdelphij#ifdef UNSIGNED
36170754Sdelphij# ifndef HAVE_DECL_STRTOULL
37170754Sdelphij"this configure-time declaration test was not run"
38170754Sdelphij# endif
39170754Sdelphij# if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
40170754Sdelphijunsigned long long strtoull (char const *, char **, int);
41170754Sdelphij# endif
42170754Sdelphij
43170754Sdelphij#else
44170754Sdelphij
45170754Sdelphij# ifndef HAVE_DECL_STRTOLL
46170754Sdelphij"this configure-time declaration test was not run"
47170754Sdelphij# endif
48170754Sdelphij# if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
49170754Sdelphijlong long strtoll (char const *, char **, int);
50170754Sdelphij# endif
51170754Sdelphij#endif
52170754Sdelphij
53170754Sdelphij#ifdef UNSIGNED
54170754Sdelphij# undef HAVE_LONG_LONG
55170754Sdelphij# define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
56170754Sdelphij# define INT uintmax_t
57170754Sdelphij# define strtoimax strtoumax
58170754Sdelphij# define strtol strtoul
59170754Sdelphij# define strtoll strtoull
60170754Sdelphij#else
61170754Sdelphij# define INT intmax_t
62170754Sdelphij#endif
63170754Sdelphij
64170754SdelphijINT
65170754Sdelphijstrtoimax (char const *ptr, char **endptr, int base)
66170754Sdelphij{
67170754Sdelphij#if HAVE_LONG_LONG
68170754Sdelphij  verify (size_is_that_of_long_or_long_long,
69170754Sdelphij	  (sizeof (INT) == sizeof (long)
70170754Sdelphij	   || sizeof (INT) == sizeof (long long)));
71170754Sdelphij
72170754Sdelphij  if (sizeof (INT) != sizeof (long))
73170754Sdelphij    return strtoll (ptr, endptr, base);
74170754Sdelphij#else
75170754Sdelphij  verify (size_is_that_of_long,
76170754Sdelphij	  sizeof (INT) == sizeof (long));
77170754Sdelphij#endif
78170754Sdelphij
79170754Sdelphij  return strtol (ptr, endptr, base);
80170754Sdelphij}
81