strtoimax.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Convert string representation of a number into an intmax_t value.
  2. Copyright (C) 1999, 2001-2004, 2006, 2009-2011 Free Software Foundation,
  3. 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 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by Paul Eggert. */
  15. #include <config.h>
  16. /* Verify interface. */
  17. #include <inttypes.h>
  18. #include <stdlib.h>
  19. #include "verify.h"
  20. #ifdef UNSIGNED
  21. # ifndef HAVE_DECL_STRTOULL
  22. "this configure-time declaration test was not run"
  23. # endif
  24. # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG_INT
  25. unsigned long long int strtoull (char const *, char **, int);
  26. # endif
  27. #else
  28. # ifndef HAVE_DECL_STRTOLL
  29. "this configure-time declaration test was not run"
  30. # endif
  31. # if !HAVE_DECL_STRTOLL && HAVE_LONG_LONG_INT
  32. long long int strtoll (char const *, char **, int);
  33. # endif
  34. #endif
  35. #ifdef UNSIGNED
  36. # define Have_long_long HAVE_UNSIGNED_LONG_LONG_INT
  37. # define Int uintmax_t
  38. # define Unsigned unsigned
  39. # define strtoimax strtoumax
  40. # define strtol strtoul
  41. # define strtoll strtoull
  42. #else
  43. # define Have_long_long HAVE_LONG_LONG_INT
  44. # define Int intmax_t
  45. # define Unsigned
  46. #endif
  47. Int
  48. strtoimax (char const *ptr, char **endptr, int base)
  49. {
  50. #if Have_long_long
  51. verify (sizeof (Int) == sizeof (Unsigned long int)
  52. || sizeof (Int) == sizeof (Unsigned long long int));
  53. if (sizeof (Int) != sizeof (Unsigned long int))
  54. return strtoll (ptr, endptr, base);
  55. #else
  56. verify (sizeof (Int) == sizeof (Unsigned long int));
  57. #endif
  58. return strtol (ptr, endptr, base);
  59. }