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]))
32 var = tolower(s[i++]);
33 if (var >= 'a' && var <= 'f') {
38 if (!(var >= '0' && var <= '9'))
44 if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
49 unsigned long simple_strtoul(const char *cp, char **endp,
52 unsigned long result = 0;
55 cp = _parse_integer_fixup_radix(cp, &base);
57 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
58 ? toupper(*cp) : *cp)-'A'+10) < base) {
59 result = result*base + value;
69 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
80 val = simple_strtoul(cp, &tail, base);
84 if ((*tail == '\0') ||
85 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
93 long simple_strtol(const char *cp, char **endp, unsigned int base)
96 return -simple_strtoul(cp + 1, endp, base);
98 return simple_strtoul(cp, endp, base);
101 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
103 unsigned long result = simple_strtoul(cp, endp, base);
104 switch (tolower(**endp)) {
122 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
124 unsigned long long result = simple_strtoull(cp, endp, base);
125 switch (tolower(**endp)) {
143 unsigned long long simple_strtoull(const char *cp, char **endp,
146 unsigned long long result = 0, value;
148 cp = _parse_integer_fixup_radix(cp, &base);
150 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
151 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
152 result = result * base + value;
162 long trailing_strtoln(const char *str, const char *end)
167 end = str + strlen(str);
168 if (isdigit(end[-1])) {
169 for (p = end - 1; p > str; p--) {
171 return simple_strtoul(p + 1, NULL, 10);
178 long trailing_strtol(const char *str)
180 return trailing_strtoln(str, NULL);
183 void str_to_upper(const char *in, char *out, size_t len)
185 for (; len > 0 && *in; len--)
186 *out++ = toupper(*in++);