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