strtol.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Convert string representation of a number into an integer value.
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdlib.h>
  16. #include <wchar.h>
  17. #include <locale/localeinfo.h>
  18. #ifndef UNSIGNED
  19. # define UNSIGNED 0
  20. # define INT LONG int
  21. #else
  22. # define INT unsigned LONG int
  23. #endif
  24. #if UNSIGNED
  25. # ifdef USE_WIDE_CHAR
  26. # ifdef QUAD
  27. # define strtol wcstoull
  28. # define __strtol_l __wcstoull_l
  29. # else
  30. # define strtol wcstoul
  31. # define __strtol_l __wcstoul_l
  32. # endif
  33. # else
  34. # ifdef QUAD
  35. # define strtol strtoull
  36. # define __strtol_l __strtoull_l
  37. # else
  38. # define strtol strtoul
  39. # define __strtol_l __strtoul_l
  40. # endif
  41. # endif
  42. #else
  43. # ifdef USE_WIDE_CHAR
  44. # ifdef QUAD
  45. # define strtol wcstoll
  46. # define __strtol_l __wcstoll_l
  47. # else
  48. # define strtol wcstol
  49. # define __strtol_l __wcstol_l
  50. # endif
  51. # else
  52. # ifdef QUAD
  53. # define strtol strtoll
  54. # define __strtol_l __strtoll_l
  55. # endif
  56. # endif
  57. #endif
  58. /* If QUAD is defined, we are defining `strtoll' or `strtoull',
  59. operating on `long long int's. */
  60. #ifdef QUAD
  61. # define LONG long long
  62. #else
  63. # define LONG long
  64. #endif
  65. #ifdef USE_WIDE_CHAR
  66. # define STRING_TYPE wchar_t
  67. #else
  68. # define STRING_TYPE char
  69. #endif
  70. #define INTERNAL(X) INTERNAL1(X)
  71. #define INTERNAL1(X) __##X##_internal
  72. #define SYM__(X) SYM__1 (X)
  73. #define SYM__1(X) __ ## X
  74. #define __strtol SYM__ (strtol)
  75. extern INT INTERNAL (__strtol_l) (const STRING_TYPE *, STRING_TYPE **, int,
  76. int, locale_t);
  77. INT
  78. INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  79. int base, int group)
  80. {
  81. return INTERNAL (__strtol_l) (nptr, endptr, base, group, _NL_CURRENT_LOCALE);
  82. }
  83. libc_hidden_def (INTERNAL (strtol))
  84. INT
  85. __strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr, int base)
  86. {
  87. return INTERNAL (__strtol_l) (nptr, endptr, base, 0, _NL_CURRENT_LOCALE);
  88. }
  89. weak_alias (__strtol, strtol)
  90. libc_hidden_weak (strtol)