strtol.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: BSD-4-Clause-UC */
  2. /*-
  3. * Copyright (c) 1990, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <limits.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include <stdlib.h>
  38. /*
  39. * Convert a string to a long integer.
  40. *
  41. * Ignores `locale' stuff. Assumes that the upper and lower case
  42. * alphabets and digits are each contiguous.
  43. */
  44. long strtol(const char *nptr, char **endptr, register int base)
  45. {
  46. register const char *s = nptr;
  47. register unsigned long acc;
  48. register int c;
  49. register unsigned long cutoff;
  50. register int neg = 0, any, cutlim;
  51. /*
  52. * Skip white space and pick up leading +/- sign if any.
  53. * If base is 0, allow 0x for hex and 0 for octal, else
  54. * assume decimal; if base is already 16, allow 0x.
  55. */
  56. do {
  57. c = *s++;
  58. } while (isspace(c));
  59. if (c == '-') {
  60. neg = 1;
  61. c = *s++;
  62. } else if (c == '+') {
  63. c = *s++;
  64. }
  65. if ((base == 0 || base == 16) &&
  66. c == '0' && (*s == 'x' || *s == 'X')) {
  67. c = s[1];
  68. s += 2;
  69. base = 16;
  70. }
  71. if (base == 0) {
  72. base = c == '0' ? 8 : 10;
  73. }
  74. /*
  75. * Compute the cutoff value between legal numbers and illegal
  76. * numbers. That is the largest legal value, divided by the
  77. * base. An input number that is greater than this value, if
  78. * followed by a legal input character, is too big. One that
  79. * is equal to this value may be valid or not; the limit
  80. * between valid and invalid numbers is then based on the last
  81. * digit. For instance, if the range for longs is
  82. * [-2147483648..2147483647] and the input base is 10,
  83. * cutoff will be set to 214748364 and cutlim to either
  84. * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  85. * a value > 214748364, or equal but the next digit is > 7 (or 8),
  86. * the number is too big, and we will return a range error.
  87. *
  88. * Set any if any `digits' consumed; make it negative to indicate
  89. * overflow.
  90. */
  91. cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  92. cutlim = cutoff % (unsigned long)base;
  93. cutoff /= (unsigned long)base;
  94. for (acc = 0, any = 0;; c = *s++) {
  95. if (isdigit(c)) {
  96. c -= '0';
  97. } else if (isalpha(c)) {
  98. c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  99. } else {
  100. break;
  101. }
  102. if (c >= base) {
  103. break;
  104. }
  105. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
  106. any = -1;
  107. } else {
  108. any = 1;
  109. acc *= base;
  110. acc += c;
  111. }
  112. }
  113. if (any < 0) {
  114. acc = neg ? LONG_MIN : LONG_MAX;
  115. errno = ERANGE;
  116. } else if (neg) {
  117. acc = -acc;
  118. }
  119. if (endptr != NULL) {
  120. *endptr = (char *)(any ? s - 1 : nptr);
  121. }
  122. return acc;
  123. }