4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 #include <linux/ctype.h>
16 /* from lib/kstrtox.c */
17 static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
21 if (tolower(s[1]) == 'x' && isxdigit(s[2]))
28 if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
33 unsigned long simple_strtoul(const char *cp, char **endp,
36 unsigned long result = 0;
39 cp = _parse_integer_fixup_radix(cp, &base);
41 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
42 ? toupper(*cp) : *cp)-'A'+10) < base) {
43 result = result*base + value;
53 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
64 val = simple_strtoul(cp, &tail, base);
68 if ((*tail == '\0') ||
69 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
77 long simple_strtol(const char *cp, char **endp, unsigned int base)
80 return -simple_strtoul(cp + 1, endp, base);
82 return simple_strtoul(cp, endp, base);
85 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
87 unsigned long result = simple_strtoul(cp, endp, base);
88 switch (tolower(**endp)) {
106 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
108 unsigned long long result = simple_strtoull(cp, endp, base);
109 switch (tolower(**endp)) {
127 unsigned long long simple_strtoull(const char *cp, char **endp,
130 unsigned long long result = 0, value;
132 cp = _parse_integer_fixup_radix(cp, &base);
134 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
135 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
136 result = result * base + value;
146 long trailing_strtoln(const char *str, const char *end)
151 end = str + strlen(str);
152 if (isdigit(end[-1])) {
153 for (p = end - 1; p > str; p--) {
155 return simple_strtoul(p + 1, NULL, 10);
162 long trailing_strtol(const char *str)
164 return trailing_strtoln(str, NULL);