strto.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <linux/ctype.h>
  13. /* from lib/kstrtox.c */
  14. static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
  15. {
  16. if (*base == 0) {
  17. if (s[0] == '0') {
  18. if (tolower(s[1]) == 'x' && isxdigit(s[2]))
  19. *base = 16;
  20. else
  21. *base = 8;
  22. } else {
  23. int i = 0;
  24. char var;
  25. *base = 10;
  26. do {
  27. var = tolower(s[i++]);
  28. if (var >= 'a' && var <= 'f') {
  29. *base = 16;
  30. break;
  31. }
  32. } while (var);
  33. }
  34. }
  35. if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
  36. s += 2;
  37. return s;
  38. }
  39. unsigned long simple_strtoul(const char *cp, char **endp,
  40. unsigned int base)
  41. {
  42. unsigned long result = 0;
  43. unsigned long value;
  44. cp = _parse_integer_fixup_radix(cp, &base);
  45. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  46. ? toupper(*cp) : *cp)-'A'+10) < base) {
  47. result = result*base + value;
  48. cp++;
  49. }
  50. if (endp)
  51. *endp = (char *)cp;
  52. return result;
  53. }
  54. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  55. {
  56. char *tail;
  57. unsigned long val;
  58. size_t len;
  59. *res = 0;
  60. len = strlen(cp);
  61. if (len == 0)
  62. return -EINVAL;
  63. val = simple_strtoul(cp, &tail, base);
  64. if (tail == cp)
  65. return -EINVAL;
  66. if ((*tail == '\0') ||
  67. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  68. *res = val;
  69. return 0;
  70. }
  71. return -EINVAL;
  72. }
  73. long simple_strtol(const char *cp, char **endp, unsigned int base)
  74. {
  75. if (*cp == '-')
  76. return -simple_strtoul(cp + 1, endp, base);
  77. return simple_strtoul(cp, endp, base);
  78. }
  79. unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
  80. {
  81. unsigned long result = simple_strtoul(cp, endp, base);
  82. switch (tolower(**endp)) {
  83. case 'g':
  84. result *= 1024;
  85. /* fall through */
  86. case 'm':
  87. result *= 1024;
  88. /* fall through */
  89. case 'k':
  90. result *= 1024;
  91. (*endp)++;
  92. if (**endp == 'i')
  93. (*endp)++;
  94. if (**endp == 'B')
  95. (*endp)++;
  96. }
  97. return result;
  98. }
  99. unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
  100. {
  101. unsigned long long result = simple_strtoull(cp, endp, base);
  102. switch (tolower(**endp)) {
  103. case 'g':
  104. result *= 1024;
  105. /* fall through */
  106. case 'm':
  107. result *= 1024;
  108. /* fall through */
  109. case 'k':
  110. result *= 1024;
  111. (*endp)++;
  112. if (**endp == 'i')
  113. (*endp)++;
  114. if (**endp == 'B')
  115. (*endp)++;
  116. }
  117. return result;
  118. }
  119. unsigned long long simple_strtoull(const char *cp, char **endp,
  120. unsigned int base)
  121. {
  122. unsigned long long result = 0, value;
  123. cp = _parse_integer_fixup_radix(cp, &base);
  124. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
  125. : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
  126. result = result * base + value;
  127. cp++;
  128. }
  129. if (endp)
  130. *endp = (char *) cp;
  131. return result;
  132. }
  133. long trailing_strtoln(const char *str, const char *end)
  134. {
  135. const char *p;
  136. if (!end)
  137. end = str + strlen(str);
  138. if (isdigit(end[-1])) {
  139. for (p = end - 1; p > str; p--) {
  140. if (!isdigit(*p))
  141. return simple_strtoul(p + 1, NULL, 10);
  142. }
  143. }
  144. return -1;
  145. }
  146. long trailing_strtol(const char *str)
  147. {
  148. return trailing_strtoln(str, NULL);
  149. }