strtoul.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 an unsigned long integer.
  40. *
  41. * Ignores `locale' stuff. Assumes that the upper and lower case
  42. * alphabets and digits are each contiguous.
  43. */
  44. unsigned long strtoul(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. * See strtol for comments as to the logic used.
  53. */
  54. do {
  55. c = *s++;
  56. } while (isspace(c));
  57. if (c == '-') {
  58. neg = 1;
  59. c = *s++;
  60. } else if (c == '+') {
  61. c = *s++;
  62. }
  63. if ((base == 0 || base == 16) &&
  64. c == '0' && (*s == 'x' || *s == 'X')) {
  65. c = s[1];
  66. s += 2;
  67. base = 16;
  68. }
  69. if (base == 0) {
  70. base = c == '0' ? 8 : 10;
  71. }
  72. cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  73. cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  74. for (acc = 0, any = 0;; c = *s++) {
  75. if (isdigit(c)) {
  76. c -= '0';
  77. } else if (isalpha(c)) {
  78. c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  79. } else {
  80. break;
  81. }
  82. if (c >= base) {
  83. break;
  84. }
  85. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
  86. any = -1;
  87. } else {
  88. any = 1;
  89. acc *= base;
  90. acc += c;
  91. }
  92. }
  93. if (any < 0) {
  94. acc = ULONG_MAX;
  95. errno = ERANGE;
  96. } else if (neg) {
  97. acc = -acc;
  98. }
  99. if (endptr != NULL) {
  100. *endptr = (char *)(any ? s - 1 : nptr);
  101. }
  102. return acc;
  103. }