strtoimax.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Convert string representation of a number into an uintmax_t value.
  2. Copyright 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Paul Eggert. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #if HAVE_INTTYPES_H
  19. # include <inttypes.h>
  20. #endif
  21. #if HAVE_STDLIB_H
  22. # include <stdlib.h>
  23. #endif
  24. #ifndef PARAMS
  25. # if defined PROTOTYPES || defined __STDC__
  26. # define PARAMS(Args) Args
  27. # else
  28. # define PARAMS(Args) ()
  29. # endif
  30. #endif
  31. #ifdef STRTOUXMAX_UNSIGNED
  32. # ifndef HAVE_DECL_STRTOUL
  33. "this configure-time declaration test was not run"
  34. # endif
  35. # if !HAVE_DECL_STRTOUL
  36. unsigned long strtoul PARAMS ((char const *, char **, int));
  37. # endif
  38. # ifndef HAVE_DECL_STRTOULL
  39. "this configure-time declaration test was not run"
  40. # endif
  41. # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
  42. unsigned long long strtoull PARAMS ((char const *, char **, int));
  43. # endif
  44. #else
  45. # ifndef HAVE_DECL_STRTOL
  46. "this configure-time declaration test was not run"
  47. # endif
  48. # if !HAVE_DECL_STRTOL
  49. long strtol PARAMS ((char const *, char **, int));
  50. # endif
  51. # ifndef HAVE_DECL_STRTOLL
  52. "this configure-time declaration test was not run"
  53. # endif
  54. # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
  55. long long strtoll PARAMS ((char const *, char **, int));
  56. # endif
  57. #endif
  58. #ifndef STRTOUXMAX_UNSIGNED
  59. # define strtoumax strtoimax
  60. # define uintmax_t intmax_t
  61. # define strtoull strtoll
  62. # define strtoul strtol
  63. #endif
  64. uintmax_t
  65. strtoumax (char const *ptr, char **endptr, int base)
  66. {
  67. #define USE_IF_EQUIVALENT(function) \
  68. if (sizeof (uintmax_t) == sizeof function (ptr, endptr, base)) \
  69. return function (ptr, endptr, base);
  70. #if HAVE_UNSIGNED_LONG_LONG
  71. USE_IF_EQUIVALENT (strtoull)
  72. #endif
  73. USE_IF_EQUIVALENT (strtoul)
  74. abort ();
  75. }
  76. #ifdef TESTING
  77. # include <stdio.h>
  78. int
  79. main ()
  80. {
  81. char *p, *endptr;
  82. printf ("sizeof xintmax_t: %d\n", sizeof (uintmax_t));
  83. printf ("sizeof strtoxll(): %d\n", sizeof strtoull(p, &endptr, 10));
  84. printf ("sizeof strtoxl(): %d\n", sizeof strtoul(p, &endptr, 10));
  85. exit (0);
  86. }
  87. #endif