strto.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if (!(var >= '0' && var <= '9'))
  33. break;
  34. } while (var);
  35. }
  36. }
  37. if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
  38. s += 2;
  39. return s;
  40. }
  41. unsigned long simple_strtoul(const char *cp, char **endp,
  42. unsigned int base)
  43. {
  44. unsigned long result = 0;
  45. unsigned long value;
  46. cp = _parse_integer_fixup_radix(cp, &base);
  47. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  48. ? toupper(*cp) : *cp)-'A'+10) < base) {
  49. result = result*base + value;
  50. cp++;
  51. }
  52. if (endp)
  53. *endp = (char *)cp;
  54. return result;
  55. }
  56. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  57. {
  58. char *tail;
  59. unsigned long val;
  60. size_t len;
  61. *res = 0;
  62. len = strlen(cp);
  63. if (len == 0)
  64. return -EINVAL;
  65. val = simple_strtoul(cp, &tail, base);
  66. if (tail == cp)
  67. return -EINVAL;
  68. if ((*tail == '\0') ||
  69. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  70. *res = val;
  71. return 0;
  72. }
  73. return -EINVAL;
  74. }
  75. long simple_strtol(const char *cp, char **endp, unsigned int base)
  76. {
  77. if (*cp == '-')
  78. return -simple_strtoul(cp + 1, endp, base);
  79. return simple_strtoul(cp, endp, base);
  80. }
  81. unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
  82. {
  83. unsigned long result = simple_strtoul(cp, endp, base);
  84. switch (tolower(**endp)) {
  85. case 'g':
  86. result *= 1024;
  87. /* fall through */
  88. case 'm':
  89. result *= 1024;
  90. /* fall through */
  91. case 'k':
  92. result *= 1024;
  93. (*endp)++;
  94. if (**endp == 'i')
  95. (*endp)++;
  96. if (**endp == 'B')
  97. (*endp)++;
  98. }
  99. return result;
  100. }
  101. unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
  102. {
  103. unsigned long long result = simple_strtoull(cp, endp, base);
  104. switch (tolower(**endp)) {
  105. case 'g':
  106. result *= 1024;
  107. /* fall through */
  108. case 'm':
  109. result *= 1024;
  110. /* fall through */
  111. case 'k':
  112. result *= 1024;
  113. (*endp)++;
  114. if (**endp == 'i')
  115. (*endp)++;
  116. if (**endp == 'B')
  117. (*endp)++;
  118. }
  119. return result;
  120. }
  121. unsigned long long simple_strtoull(const char *cp, char **endp,
  122. unsigned int base)
  123. {
  124. unsigned long long result = 0, value;
  125. cp = _parse_integer_fixup_radix(cp, &base);
  126. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
  127. : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
  128. result = result * base + value;
  129. cp++;
  130. }
  131. if (endp)
  132. *endp = (char *) cp;
  133. return result;
  134. }
  135. long trailing_strtoln(const char *str, const char *end)
  136. {
  137. const char *p;
  138. if (!end)
  139. end = str + strlen(str);
  140. if (isdigit(end[-1])) {
  141. for (p = end - 1; p > str; p--) {
  142. if (!isdigit(*p))
  143. return simple_strtoul(p + 1, NULL, 10);
  144. }
  145. }
  146. return -1;
  147. }
  148. long trailing_strtol(const char *str)
  149. {
  150. return trailing_strtoln(str, NULL);
  151. }
  152. void str_to_upper(const char *in, char *out, size_t len)
  153. {
  154. for (; len > 0 && *in; len--)
  155. *out++ = toupper(*in++);
  156. if (len)
  157. *out = '\0';
  158. }