118fd37a7SXin LI /* Convert string representation of a number into an intmax_t value.
218fd37a7SXin LI Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
318fd37a7SXin LI
418fd37a7SXin LI This program is free software; you can redistribute it and/or modify
518fd37a7SXin LI it under the terms of the GNU General Public License as published by
618fd37a7SXin LI the Free Software Foundation; either version 2, or (at your option)
718fd37a7SXin LI any later version.
818fd37a7SXin LI
918fd37a7SXin LI This program is distributed in the hope that it will be useful,
1018fd37a7SXin LI but WITHOUT ANY WARRANTY; without even the implied warranty of
1118fd37a7SXin LI MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1218fd37a7SXin LI GNU General Public License for more details.
1318fd37a7SXin LI
1418fd37a7SXin LI You should have received a copy of the GNU General Public License
1518fd37a7SXin LI along with this program; if not, write to the Free Software Foundation,
1618fd37a7SXin LI Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1718fd37a7SXin LI
1818fd37a7SXin LI /* Written by Paul Eggert. */
1918fd37a7SXin LI
2018fd37a7SXin LI #if HAVE_CONFIG_H
2118fd37a7SXin LI # include <config.h>
2218fd37a7SXin LI #endif
2318fd37a7SXin LI
2418fd37a7SXin LI #if HAVE_INTTYPES_H
2518fd37a7SXin LI # include <inttypes.h>
2618fd37a7SXin LI #elif HAVE_STDINT_H
2718fd37a7SXin LI # include <stdint.h>
2818fd37a7SXin LI #endif
2918fd37a7SXin LI
3018fd37a7SXin LI #include <stdlib.h>
3118fd37a7SXin LI
3218fd37a7SXin LI /* Verify a requirement at compile-time (unlike assert, which is runtime). */
3318fd37a7SXin LI #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
3418fd37a7SXin LI
3518fd37a7SXin LI #ifdef UNSIGNED
3618fd37a7SXin LI # ifndef HAVE_DECL_STRTOULL
3718fd37a7SXin LI "this configure-time declaration test was not run"
3818fd37a7SXin LI # endif
3918fd37a7SXin LI # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
4018fd37a7SXin LI unsigned long long strtoull (char const *, char **, int);
4118fd37a7SXin LI # endif
4218fd37a7SXin LI
4318fd37a7SXin LI #else
4418fd37a7SXin LI
4518fd37a7SXin LI # ifndef HAVE_DECL_STRTOLL
4618fd37a7SXin LI "this configure-time declaration test was not run"
4718fd37a7SXin LI # endif
4818fd37a7SXin LI # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
4918fd37a7SXin LI long long strtoll (char const *, char **, int);
5018fd37a7SXin LI # endif
5118fd37a7SXin LI #endif
5218fd37a7SXin LI
5318fd37a7SXin LI #ifdef UNSIGNED
5418fd37a7SXin LI # undef HAVE_LONG_LONG
5518fd37a7SXin LI # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
5618fd37a7SXin LI # define INT uintmax_t
5718fd37a7SXin LI # define strtoimax strtoumax
5818fd37a7SXin LI # define strtol strtoul
5918fd37a7SXin LI # define strtoll strtoull
6018fd37a7SXin LI #else
6118fd37a7SXin LI # define INT intmax_t
6218fd37a7SXin LI #endif
6318fd37a7SXin LI
6418fd37a7SXin LI INT
strtoimax(char const * ptr,char ** endptr,int base)6518fd37a7SXin LI strtoimax (char const *ptr, char **endptr, int base)
6618fd37a7SXin LI {
6718fd37a7SXin LI #if HAVE_LONG_LONG
6818fd37a7SXin LI verify (size_is_that_of_long_or_long_long,
6918fd37a7SXin LI (sizeof (INT) == sizeof (long)
7018fd37a7SXin LI || sizeof (INT) == sizeof (long long)));
7118fd37a7SXin LI
7218fd37a7SXin LI if (sizeof (INT) != sizeof (long))
7318fd37a7SXin LI return strtoll (ptr, endptr, base);
7418fd37a7SXin LI #else
7518fd37a7SXin LI verify (size_is_that_of_long,
7618fd37a7SXin LI sizeof (INT) == sizeof (long));
7718fd37a7SXin LI #endif
7818fd37a7SXin LI
7918fd37a7SXin LI return strtol (ptr, endptr, base);
8018fd37a7SXin LI }
81