1/* strtoumax - convert string representation of a number into an uintmax_t value. */ 2 3/* Copyright 1999-2005 Free Software Foundation, Inc. 4 5 This file is part of GNU Bash, the Bourne Again SHell. 6 7 Bash 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 3 of the License, or 10 (at your option) any later version. 11 12 Bash 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. 16 17 You should have received a copy of the GNU General Public License 18 along with Bash. If not, see <http://www.gnu.org/licenses/>. 19*/ 20/* Written by Paul Eggert. Modified by Chet Ramey for Bash. */ 21 22#if HAVE_CONFIG_H 23# include <config.h> 24#endif 25 26#if HAVE_INTTYPES_H 27# include <inttypes.h> 28#endif 29 30#if HAVE_STDLIB_H 31# include <stdlib.h> 32#endif 33 34#include <stdc.h> 35 36/* Verify a requirement at compile-time (unlike assert, which is runtime). */ 37#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } 38 39#ifndef HAVE_DECL_STRTOUL 40"this configure-time declaration test was not run" 41#endif 42#if !HAVE_DECL_STRTOUL 43extern unsigned long strtoul __P((const char *, char **, int)); 44#endif 45 46#ifndef HAVE_DECL_STRTOULL 47"this configure-time declaration test was not run" 48#endif 49#if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG 50extern unsigned long long strtoull __P((const char *, char **, int)); 51#endif 52 53#ifdef strtoumax 54#undef strtoumax 55#endif 56 57uintmax_t 58strtoumax (ptr, endptr, base) 59 const char *ptr; 60 char **endptr; 61 int base; 62{ 63#if HAVE_UNSIGNED_LONG_LONG 64 verify (size_is_that_of_unsigned_long_or_unsigned_long_long, 65 (sizeof (uintmax_t) == sizeof (unsigned long) || 66 sizeof (uintmax_t) == sizeof (unsigned long long))); 67 68 if (sizeof (uintmax_t) != sizeof (unsigned long)) 69 return (strtoull (ptr, endptr, base)); 70#else 71 verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long)); 72#endif 73 74 return (strtoul (ptr, endptr, base)); 75} 76 77#ifdef TESTING 78# include <stdio.h> 79int 80main () 81{ 82 char *p, *endptr; 83 uintmax_t x; 84#if HAVE_UNSIGNED_LONG_LONG 85 unsigned long long y; 86#endif 87 unsigned long z; 88 89 printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t)); 90 91#if HAVE_UNSIGNED_LONG_LONG 92 printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long)); 93#endif 94 printf ("sizeof unsigned long: %d\n", sizeof (unsigned long)); 95 96 x = strtoumax("42", &endptr, 10); 97#if HAVE_LONG_LONG 98 y = strtoull("42", &endptr, 10); 99#else 100 y = 0; 101#endif 102 z = strtoul("42", &endptr, 10); 103 104 printf ("%llu %llu %lu\n", x, y, z); 105 106 exit (0); 107} 108#endif 109