strtoumax.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
  2. /*-
  3. * Copyright (c) 1992 The Regents of the University of California.
  4. * 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. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <inttypes.h>
  33. /* Disable warnings */
  34. /*
  35. * Convert a string to a uintmax_t.
  36. *
  37. * Ignores `locale' stuff. Assumes that the upper and lower case
  38. * alphabets and digits are each contiguous.
  39. */
  40. uintmax_t
  41. strtoumax(const char *nptr, char **endptr, int base)
  42. {
  43. const char *s;
  44. uintmax_t acc, cutoff;
  45. int c;
  46. int neg, any, cutlim;
  47. /*
  48. * See strtoq for comments as to the logic used.
  49. */
  50. s = nptr;
  51. do {
  52. c = (unsigned char) *s++;
  53. } while (isspace(c));
  54. if (c == '-') {
  55. neg = 1;
  56. c = *s++;
  57. } else {
  58. neg = 0;
  59. if (c == '+')
  60. c = *s++;
  61. }
  62. if ((base == 0 || base == 16) &&
  63. c == '0' && (*s == 'x' || *s == 'X')) {
  64. c = s[1];
  65. s += 2;
  66. base = 16;
  67. }
  68. if (base == 0)
  69. base = c == '0' ? 8 : 10;
  70. cutoff = UINTMAX_MAX / (uintmax_t)base;
  71. cutlim = UINTMAX_MAX % (uintmax_t)base;
  72. for (acc = 0, any = 0;; c = (unsigned char) *s++) {
  73. if (isdigit(c))
  74. c -= '0';
  75. else if (isalpha(c))
  76. c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  77. else
  78. break;
  79. if (c >= base)
  80. break;
  81. if (any < 0)
  82. continue;
  83. if (acc > cutoff || (acc == cutoff && c > cutlim)) {
  84. any = -1;
  85. acc = UINTMAX_MAX;
  86. errno = ERANGE;
  87. } else {
  88. any = 1;
  89. acc *= (uintmax_t)base;
  90. acc += c;
  91. }
  92. }
  93. if (neg && any > 0)
  94. acc = -acc;
  95. if (endptr != 0)
  96. *endptr = (char *) (any ? s - 1 : nptr);
  97. return (acc);
  98. }