Rev 4279 | Rev 5270 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4279 | Rev 5056 | ||
---|---|---|---|
Line 15... | Line 15... | ||
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade |
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade |
16 | * - scnprintf and vscnprintf |
16 | * - scnprintf and vscnprintf |
17 | */ |
17 | */ |
Line 18... | Line 18... | ||
18 | 18 | ||
19 | #include |
19 | #include |
20 | #include |
20 | #include |
21 | #include |
21 | #include |
22 | #include |
22 | #include |
23 | #include |
23 | #include |
24 | #include |
24 | #include |
25 | #include |
25 | #include |
26 | #include |
26 | #include |
Line 27... | Line 27... | ||
27 | #include |
27 | #include |
Line -... | Line 28... | ||
- | 28 | ||
- | 29 | #include |
|
- | 30 | ||
- | 31 | ||
- | 32 | static inline u64 div_u64(u64 dividend, u32 divisor) |
|
- | 33 | { |
|
- | 34 | u32 remainder; |
|
- | 35 | return div_u64_rem(dividend, divisor, &remainder); |
|
- | 36 | } |
|
- | 37 | ||
- | 38 | static inline s64 div_s64(s64 dividend, s32 divisor) |
|
- | 39 | { |
|
- | 40 | s32 remainder; |
|
28 | 41 | return div_s64_rem(dividend, divisor, &remainder); |
|
29 | #include |
42 | } |
30 | 43 | ||
31 | struct va_format { |
44 | struct va_format { |
Line -... | Line 45... | ||
- | 45 | const char *fmt; |
|
- | 46 | va_list *va; |
|
- | 47 | }; |
|
- | 48 | ||
- | 49 | #define ZERO_SIZE_PTR ((void *)16) |
|
32 | const char *fmt; |
50 | |
33 | va_list *va; |
51 | #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \ |
34 | }; |
52 | (unsigned long)ZERO_SIZE_PTR) |
Line -... | Line 53... | ||
- | 53 | ||
- | 54 | #ifndef dereference_function_descriptor |
|
35 | 55 | #define dereference_function_descriptor(p) (p) |
|
Line 36... | Line 56... | ||
36 | #ifndef dereference_function_descriptor |
56 | #endif |
37 | #define dereference_function_descriptor(p) (p) |
57 | |
Line 38... | Line 58... | ||
38 | #endif |
58 | #define KSTRTOX_OVERFLOW (1U << 31) |
- | 59 | ||
- | 60 | const char hex_asc[] = "0123456789abcdef"; |
|
- | 61 | ||
- | 62 | /* Works only for digits and letters, but small and fast */ |
|
- | 63 | #define TOLOWER(x) ((x) | 0x20) |
|
- | 64 | ||
- | 65 | static inline char *hex_byte_pack(char *buf, u8 byte) |
|
- | 66 | { |
|
39 | 67 | *buf++ = hex_asc_hi(byte); |
|
- | 68 | *buf++ = hex_asc_lo(byte); |
|
- | 69 | return buf; |
|
- | 70 | } |
|
- | 71 | ||
- | 72 | ||
- | 73 | char *skip_spaces(const char *str) |
|
- | 74 | { |
|
- | 75 | while (isspace(*str)) |
|
- | 76 | ++str; |
|
40 | const char hex_asc[] = "0123456789abcdef"; |
77 | return (char *)str; |
41 | 78 | } |
|
42 | /* Works only for digits and letters, but small and fast */ |
79 | EXPORT_SYMBOL(skip_spaces); |
43 | #define TOLOWER(x) ((x) | 0x20) |
80 | |
- | 81 | const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) |
|
44 | 82 | { |
|
- | 83 | if (*base == 0) { |
|
45 | static unsigned int simple_guess_base(const char *cp) |
84 | if (s[0] == '0') { |
- | 85 | if (_tolower(s[1]) == 'x' && isxdigit(s[2])) |
|
- | 86 | *base = 16; |
|
46 | { |
87 | else |
47 | if (cp[0] == '0') { |
88 | *base = 8; |
- | 89 | } else |
|
- | 90 | *base = 10; |
|
- | 91 | } |
|
- | 92 | if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') |
|
- | 93 | s += 2; |
|
- | 94 | return s; |
|
- | 95 | } |
|
- | 96 | ||
- | 97 | /* |
|
- | 98 | * Convert non-negative integer string representation in explicitly given radix |
|
- | 99 | * to an integer. |
|
- | 100 | * Return number of characters consumed maybe or-ed with overflow bit. |
|
- | 101 | * If overflow occurs, result integer (incorrect) is still returned. |
|
- | 102 | * |
|
- | 103 | * Don't you dare use this function. |
|
- | 104 | */ |
|
- | 105 | unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p) |
|
- | 106 | { |
|
- | 107 | unsigned long long res; |
|
- | 108 | unsigned int rv; |
|
- | 109 | int overflow; |
|
- | 110 | ||
- | 111 | res = 0; |
|
- | 112 | rv = 0; |
|
- | 113 | overflow = 0; |
|
- | 114 | while (*s) { |
|
- | 115 | unsigned int val; |
|
- | 116 | ||
- | 117 | if ('0' <= *s && *s <= '9') |
|
- | 118 | val = *s - '0'; |
|
- | 119 | else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f') |
|
- | 120 | val = _tolower(*s) - 'a' + 10; |
|
- | 121 | else |
|
- | 122 | break; |
|
- | 123 | ||
- | 124 | if (val >= base) |
|
- | 125 | break; |
|
- | 126 | /* |
|
- | 127 | * Check for overflow only if we are within range of |
|
- | 128 | * it in the max base we support (16) |
|
- | 129 | */ |
|
- | 130 | if (unlikely(res & (~0ull << 60))) { |
|
- | 131 | if (res > div_u64(ULLONG_MAX - val, base)) |
|
- | 132 | overflow = 1; |
|
- | 133 | } |
|
- | 134 | res = res * base + val; |
|
48 | if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) |
135 | rv++; |
Line -... | Line 136... | ||
- | 136 | s++; |
|
49 | return 16; |
137 | } |
50 | else |
138 | *p = res; |
51 | return 8; |
139 | if (overflow) |
52 | } else { |
140 | rv |= KSTRTOX_OVERFLOW; |
53 | return 10; |
141 | return rv; |
- | 142 | } |
|
- | 143 | ||
54 | } |
144 | |
55 | } |
145 | /** |
56 | 146 | * simple_strtoull - convert a string to an unsigned long long |
|
57 | /** |
147 | * @cp: The start of the string |
- | 148 | * @endp: A pointer to the end of the parsed string will be placed here |
|
Line -... | Line 149... | ||
- | 149 | * @base: The number base to use |
|
- | 150 | * |
|
58 | * simple_strtoull - convert a string to an unsigned long long |
151 | * This function is obsolete. Please use kstrtoull instead. |
59 | * @cp: The start of the string |
152 | */ |
Line 60... | Line -... | ||
60 | * @endp: A pointer to the end of the parsed string will be placed here |
- | |
61 | * @base: The number base to use |
- | |
62 | */ |
- | |
63 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
- | |
64 | { |
- | |
65 | unsigned long long result = 0; |
- | |
66 | - | ||
67 | if (!base) |
- | |
68 | base = simple_guess_base(cp); |
- | |
69 | - | ||
70 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
- | |
71 | cp += 2; |
- | |
72 | 153 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
|
73 | while (isxdigit(*cp)) { |
154 | { |
Line 74... | Line 155... | ||
74 | unsigned int value; |
155 | unsigned long long result; |
75 | 156 | unsigned int rv; |
|
Line 89... | Line 170... | ||
89 | /** |
170 | /** |
90 | * simple_strtoul - convert a string to an unsigned long |
171 | * simple_strtoul - convert a string to an unsigned long |
91 | * @cp: The start of the string |
172 | * @cp: The start of the string |
92 | * @endp: A pointer to the end of the parsed string will be placed here |
173 | * @endp: A pointer to the end of the parsed string will be placed here |
93 | * @base: The number base to use |
174 | * @base: The number base to use |
- | 175 | * |
|
- | 176 | * This function is obsolete. Please use kstrtoul instead. |
|
94 | */ |
177 | */ |
95 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
178 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
96 | { |
179 | { |
97 | return simple_strtoull(cp, endp, base); |
180 | return simple_strtoull(cp, endp, base); |
98 | } |
181 | } |
Line 101... | Line 184... | ||
101 | /** |
184 | /** |
102 | * simple_strtol - convert a string to a signed long |
185 | * simple_strtol - convert a string to a signed long |
103 | * @cp: The start of the string |
186 | * @cp: The start of the string |
104 | * @endp: A pointer to the end of the parsed string will be placed here |
187 | * @endp: A pointer to the end of the parsed string will be placed here |
105 | * @base: The number base to use |
188 | * @base: The number base to use |
- | 189 | * |
|
- | 190 | * This function is obsolete. Please use kstrtol instead. |
|
106 | */ |
191 | */ |
107 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
192 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
108 | { |
193 | { |
109 | if (*cp == '-') |
194 | if (*cp == '-') |
110 | return -simple_strtoul(cp + 1, endp, base); |
195 | return -simple_strtoul(cp + 1, endp, base); |
Line 116... | Line 201... | ||
116 | /** |
201 | /** |
117 | * simple_strtoll - convert a string to a signed long long |
202 | * simple_strtoll - convert a string to a signed long long |
118 | * @cp: The start of the string |
203 | * @cp: The start of the string |
119 | * @endp: A pointer to the end of the parsed string will be placed here |
204 | * @endp: A pointer to the end of the parsed string will be placed here |
120 | * @base: The number base to use |
205 | * @base: The number base to use |
- | 206 | * |
|
- | 207 | * This function is obsolete. Please use kstrtoll instead. |
|
121 | */ |
208 | */ |
122 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
209 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
123 | { |
210 | { |
124 | if (*cp == '-') |
211 | if (*cp == '-') |
125 | return -simple_strtoull(cp + 1, endp, base); |
212 | return -simple_strtoull(cp + 1, endp, base); |
Line 126... | Line 213... | ||
126 | 213 | ||
127 | return simple_strtoull(cp, endp, base); |
214 | return simple_strtoull(cp, endp, base); |
128 | } |
215 | } |
Line 129... | Line -... | ||
129 | EXPORT_SYMBOL(simple_strtoll); |
- | |
130 | - | ||
131 | /** |
- | |
132 | * strict_strtoul - convert a string to an unsigned long strictly |
216 | EXPORT_SYMBOL(simple_strtoll); |
133 | * @cp: The string to be converted |
217 | |
134 | * @base: The number base to use |
- | |
135 | * @res: The converted result value |
- | |
136 | * |
- | |
137 | * strict_strtoul converts a string to an unsigned long only if the |
- | |
138 | * string is really an unsigned long string, any string containing |
- | |
139 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
- | |
140 | * only a newline char at the tail is acceptible because people generally |
- | |
141 | * change a module parameter in the following way: |
- | |
142 | * |
- | |
143 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
- | |
144 | * |
- | |
145 | * echo will append a newline to the tail. |
- | |
146 | * |
- | |
147 | * It returns 0 if conversion is successful and *res is set to the converted |
- | |
148 | * value, otherwise it returns -EINVAL and *res is set to 0. |
- | |
149 | * |
- | |
150 | * simple_strtoul just ignores the successive invalid characters and |
- | |
151 | * return the converted value of prefix part of the string. |
- | |
152 | */ |
218 | static noinline_for_stack |
153 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
- | |
154 | { |
- | |
155 | char *tail; |
- | |
156 | unsigned long val; |
219 | int skip_atoi(const char **s) |
157 | - | ||
158 | *res = 0; |
- | |
159 | if (!*cp) |
- | |
160 | return -EINVAL; |
- | |
161 | - | ||
162 | val = simple_strtoul(cp, &tail, base); |
- | |
Line 163... | Line -... | ||
163 | if (tail == cp) |
- | |
164 | return -EINVAL; |
220 | { |
165 | 221 | int i = 0; |
|
166 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
- | |
Line 167... | Line 222... | ||
167 | *res = val; |
222 | |
168 | return 0; |
223 | while (isdigit(**s)) |
169 | } |
- | |
Line 170... | Line -... | ||
170 | - | ||
171 | return -EINVAL; |
224 | i = i*10 + *((*s)++) - '0'; |
172 | } |
- | |
173 | EXPORT_SYMBOL(strict_strtoul); |
- | |
174 | - | ||
175 | /** |
- | |
176 | * strict_strtol - convert a string to a long strictly |
225 | |
177 | * @cp: The string to be converted |
226 | return i; |
178 | * @base: The number base to use |
- | |
179 | * @res: The converted result value |
227 | } |
180 | * |
228 | |
181 | * strict_strtol is similiar to strict_strtoul, but it allows the first |
229 | /* Decimal conversion is by far the most typical, and is used |
- | 230 | * for /proc and /sys data. This directly impacts e.g. top performance |
|
- | 231 | * with many processes running. We optimize it for speed |
|
182 | * character of a string is '-'. |
232 | * using ideas described at |
- | 233 | * (with permission from the author, Douglas W. Jones). |
|
- | 234 | */ |
|
183 | * |
235 | |
184 | * It returns 0 if conversion is successful and *res is set to the converted |
236 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
185 | * value, otherwise it returns -EINVAL and *res is set to 0. |
- | |
186 | */ |
- | |
187 | int strict_strtol(const char *cp, unsigned int base, long *res) |
- | |
188 | { |
- | |
189 | int ret; |
- | |
190 | if (*cp == '-') { |
- | |
191 | ret = strict_strtoul(cp + 1, base, (unsigned long *)res); |
- | |
Line -... | Line 237... | ||
- | 237 | /* Formats correctly any integer in [0, 999999999] */ |
|
- | 238 | static noinline_for_stack |
|
- | 239 | char *put_dec_full9(char *buf, unsigned q) |
|
- | 240 | { |
|
- | 241 | unsigned r; |
|
- | 242 | ||
- | 243 | /* |
|
- | 244 | * Possible ways to approx. divide by 10 |
|
- | 245 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) |
|
- | 246 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) |
|
- | 247 | * (x * 0x6667) >> 18 x < 43699 |
|
- | 248 | * (x * 0x3334) >> 17 x < 16389 |
|
- | 249 | * (x * 0x199a) >> 16 x < 16389 |
|
- | 250 | * (x * 0x0ccd) >> 15 x < 16389 |
|
- | 251 | * (x * 0x0667) >> 14 x < 2739 |
|
- | 252 | * (x * 0x0334) >> 13 x < 1029 |
|
- | 253 | * (x * 0x019a) >> 12 x < 1029 |
|
- | 254 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) |
|
- | 255 | * (x * 0x0067) >> 10 x < 179 |
|
- | 256 | * (x * 0x0034) >> 9 x < 69 same |
|
- | 257 | * (x * 0x001a) >> 8 x < 69 same |
|
- | 258 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) |
|
- | 259 | * (x * 0x0007) >> 6 x < 19 |
|
- | 260 | * See |
|
- | 261 | */ |
|
- | 262 | r = (q * (uint64_t)0x1999999a) >> 32; |
|
- | 263 | *buf++ = (q - 10 * r) + '0'; /* 1 */ |
|
- | 264 | q = (r * (uint64_t)0x1999999a) >> 32; |
|
- | 265 | *buf++ = (r - 10 * q) + '0'; /* 2 */ |
|
- | 266 | r = (q * (uint64_t)0x1999999a) >> 32; |
|
- | 267 | *buf++ = (q - 10 * r) + '0'; /* 3 */ |
|
- | 268 | q = (r * (uint64_t)0x1999999a) >> 32; |
|
- | 269 | *buf++ = (r - 10 * q) + '0'; /* 4 */ |
|
- | 270 | r = (q * (uint64_t)0x1999999a) >> 32; |
|
- | 271 | *buf++ = (q - 10 * r) + '0'; /* 5 */ |
|
- | 272 | /* Now value is under 10000, can avoid 64-bit multiply */ |
|
- | 273 | q = (r * 0x199a) >> 16; |
|
192 | if (!ret) |
274 | *buf++ = (r - 10 * q) + '0'; /* 6 */ |
193 | *res = -(*res); |
275 | r = (q * 0xcd) >> 11; |
194 | } else { |
276 | *buf++ = (q - 10 * r) + '0'; /* 7 */ |
Line 195... | Line -... | ||
195 | ret = strict_strtoul(cp, base, (unsigned long *)res); |
- | |
196 | } |
- | |
197 | - | ||
198 | return ret; |
- | |
199 | } |
- | |
200 | EXPORT_SYMBOL(strict_strtol); |
- | |
201 | - | ||
202 | /** |
- | |
203 | * strict_strtoull - convert a string to an unsigned long long strictly |
- | |
204 | * @cp: The string to be converted |
- | |
205 | * @base: The number base to use |
- | |
206 | * @res: The converted result value |
- | |
207 | * |
- | |
208 | * strict_strtoull converts a string to an unsigned long long only if the |
- | |
209 | * string is really an unsigned long long string, any string containing |
277 | q = (r * 0xcd) >> 11; |
210 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
- | |
211 | * only a newline char at the tail is acceptible because people generally |
278 | *buf++ = (r - 10 * q) + '0'; /* 8 */ |
212 | * change a module parameter in the following way: |
- | |
213 | * |
- | |
214 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
279 | *buf++ = q + '0'; /* 9 */ |
215 | * |
- | |
216 | * echo will append a newline to the tail of the string. |
280 | return buf; |
- | 281 | } |
|
217 | * |
282 | #endif |
218 | * It returns 0 if conversion is successful and *res is set to the converted |
283 | |
219 | * value, otherwise it returns -EINVAL and *res is set to 0. |
- | |
220 | * |
284 | /* Similar to above but do not pad with zeros. |
Line 221... | Line -... | ||
221 | * simple_strtoull just ignores the successive invalid characters and |
- | |
222 | * return the converted value of prefix part of the string. |
- | |
223 | */ |
- | |
224 | int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) |
- | |
225 | { |
285 | * Code can be easily arranged to print 9 digits too, but our callers |
226 | char *tail; |
286 | * always call put_dec_full9() instead when the number has 9 decimal digits. |
227 | unsigned long long val; |
287 | */ |
228 | 288 | static noinline_for_stack |
|
229 | *res = 0; |
289 | char *put_dec_trunc8(char *buf, unsigned r) |
230 | if (!*cp) |
- | |
231 | return -EINVAL; |
290 | { |
Line -... | Line 291... | ||
- | 291 | unsigned q; |
|
- | 292 | ||
- | 293 | /* Copy of previous function's body with added early returns */ |
|
- | 294 | while (r >= 10000) { |
|
- | 295 | q = r + '0'; |
|
- | 296 | r = (r * (uint64_t)0x1999999a) >> 32; |
|
- | 297 | *buf++ = q - 10*r; |
|
- | 298 | } |
|
- | 299 | ||
- | 300 | q = (r * 0x199a) >> 16; /* r <= 9999 */ |
|
- | 301 | *buf++ = (r - 10 * q) + '0'; |
|
- | 302 | if (q == 0) |
|
- | 303 | return buf; |
|
232 | 304 | r = (q * 0xcd) >> 11; /* q <= 999 */ |
|
233 | val = simple_strtoull(cp, &tail, base); |
305 | *buf++ = (q - 10 * r) + '0'; |
234 | if (tail == cp) |
- | |
Line -... | Line 306... | ||
- | 306 | if (r == 0) |
|
- | 307 | return buf; |
|
- | 308 | q = (r * 0xcd) >> 11; /* r <= 99 */ |
|
- | 309 | *buf++ = (r - 10 * q) + '0'; |
|
235 | return -EINVAL; |
310 | if (q == 0) |
- | 311 | return buf; |
|
236 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
312 | *buf++ = q + '0'; /* q <= 9 */ |
237 | *res = val; |
313 | return buf; |
- | 314 | } |
|
- | 315 | ||
238 | return 0; |
316 | /* There are two algorithms to print larger numbers. |
- | 317 | * One is generic: divide by 1000000000 and repeatedly print |
|
239 | } |
318 | * groups of (up to) 9 digits. It's conceptually simple, |
240 | 319 | * but requires a (unsigned long long) / 1000000000 division. |
|
241 | return -EINVAL; |
- | |
242 | } |
- | |
243 | EXPORT_SYMBOL(strict_strtoull); |
- | |
244 | 320 | * |
|
245 | /** |
- | |
246 | * strict_strtoll - convert a string to a long long strictly |
321 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, |
247 | * @cp: The string to be converted |
- | |
248 | * @base: The number base to use |
- | |
249 | * @res: The converted result value |
- | |
250 | * |
- | |
251 | * strict_strtoll is similiar to strict_strtoull, but it allows the first |
- | |
252 | * character of a string is '-'. |
- | |
253 | * |
- | |
254 | * It returns 0 if conversion is successful and *res is set to the converted |
- | |
255 | * value, otherwise it returns -EINVAL and *res is set to 0. |
- | |
256 | */ |
- | |
Line 257... | Line -... | ||
257 | int strict_strtoll(const char *cp, unsigned int base, long long *res) |
- | |
258 | { |
- | |
259 | int ret; |
322 | * manipulates them cleverly and generates groups of 4 decimal digits. |
Line 260... | Line 323... | ||
260 | if (*cp == '-') { |
323 | * It so happens that it does NOT require long long division. |
- | 324 | * |
|
- | 325 | * If long is > 32 bits, division of 64-bit values is relatively easy, |
|
261 | ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); |
326 | * and we will use the first algorithm. |
262 | if (!ret) |
327 | * If long long is > 64 bits (strange architecture with VERY large long long), |
- | 328 | * second algorithm can't be used, and we again use the first one. |
|
- | 329 | * |
|
- | 330 | * Else (if long is 32 bits and long long is 64 bits) we use second one. |
|
263 | *res = -(*res); |
331 | */ |
- | 332 | ||
- | 333 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
|
- | 334 | ||
- | 335 | /* First algorithm: generic */ |
|
Line 264... | Line 336... | ||
264 | } else { |
336 | |
265 | ret = strict_strtoull(cp, base, (unsigned long long *)res); |
- | |
Line 266... | Line 337... | ||
266 | } |
337 | static |
267 | - | ||
Line 268... | Line 338... | ||
268 | return ret; |
338 | char *put_dec(char *buf, unsigned long long n) |
269 | } |
- | |
270 | EXPORT_SYMBOL(strict_strtoll); |
- | |
271 | - | ||
272 | static noinline_for_stack |
- | |
273 | int skip_atoi(const char **s) |
- | |
274 | { |
- | |
275 | int i = 0; |
- | |
276 | - | ||
277 | while (isdigit(**s)) |
- | |
278 | i = i*10 + *((*s)++) - '0'; |
339 | { |
279 | 340 | if (n >= 100*1000*1000) { |
|
280 | return i; |
341 | while (n >= 1000*1000*1000) |
281 | } |
342 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); |
282 | - | ||
283 | /* Decimal conversion is by far the most typical, and is used |
- | |
284 | * for /proc and /sys data. This directly impacts e.g. top performance |
- | |
285 | * with many processes running. We optimize it for speed |
- | |
286 | * using code from |
- | |
287 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
343 | if (n >= 100*1000*1000) |
288 | * (with permission from the author, Douglas W. Jones). */ |
- | |
289 | - | ||
290 | /* Formats correctly any integer in [0,99999]. |
344 | return put_dec_full9(buf, n); |
291 | * Outputs from one to five digits depending on input. |
- | |
292 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
345 | } |
293 | static noinline_for_stack |
346 | return put_dec_trunc8(buf, n); |
294 | char *put_dec_trunc(char *buf, unsigned q) |
- | |
295 | { |
- | |
296 | unsigned d3, d2, d1, d0; |
- | |
297 | d1 = (q>>4) & 0xf; |
- | |
298 | d2 = (q>>8) & 0xf; |
347 | } |
299 | d3 = (q>>12); |
- | |
300 | - | ||
301 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
- | |
302 | q = (d0 * 0xcd) >> 11; |
- | |
303 | d0 = d0 - 10*q; |
- | |
304 | *buf++ = d0 + '0'; /* least significant digit */ |
348 | |
305 | d1 = q + 9*d3 + 5*d2 + d1; |
349 | #else |
306 | if (d1 != 0) { |
- | |
307 | q = (d1 * 0xcd) >> 11; |
- | |
308 | d1 = d1 - 10*q; |
- | |
309 | *buf++ = d1 + '0'; /* next digit */ |
- | |
310 | - | ||
311 | d2 = q + 2*d2; |
350 | |
Line -... | Line 351... | ||
- | 351 | /* Second algorithm: valid only for 64-bit long longs */ |
|
- | 352 | ||
- | 353 | /* See comment in put_dec_full9 for choice of constants */ |
|
- | 354 | static noinline_for_stack |
|
- | 355 | void put_dec_full4(char *buf, unsigned q) |
|
- | 356 | { |
|
- | 357 | unsigned r; |
|
- | 358 | r = (q * 0xccd) >> 15; |
|
- | 359 | buf[0] = (q - 10 * r) + '0'; |
|
- | 360 | q = (r * 0xcd) >> 11; |
|
- | 361 | buf[1] = (r - 10 * q) + '0'; |
|
- | 362 | r = (q * 0xcd) >> 11; |
|
- | 363 | buf[2] = (q - 10 * r) + '0'; |
|
312 | if ((d2 != 0) || (d3 != 0)) { |
364 | buf[3] = r + '0'; |
313 | q = (d2 * 0xd) >> 7; |
365 | } |
- | 366 | ||
314 | d2 = d2 - 10*q; |
367 | /* |
- | 368 | * Call put_dec_full4 on x % 10000, return x / 10000. |
|
315 | *buf++ = d2 + '0'; /* next digit */ |
369 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
- | 370 | * holds for all x < 1,128,869,999. The largest value this |
|
- | 371 | * helper will ever be asked to convert is 1,125,520,955. |
|
- | 372 | * (d1 in the put_dec code, assuming n is all-ones). |
|
316 | 373 | */ |
|
317 | d3 = q + 4*d3; |
374 | static |
318 | if (d3 != 0) { |
- | |
319 | q = (d3 * 0xcd) >> 11; |
- | |
320 | d3 = d3 - 10*q; |
375 | unsigned put_dec_helper4(char *buf, unsigned x) |
321 | *buf++ = d3 + '0'; /* next digit */ |
- | |
322 | if (q != 0) |
- | |
323 | *buf++ = q + '0'; /* most sign. digit */ |
- | |
Line 324... | Line -... | ||
324 | } |
- | |
325 | } |
376 | { |
326 | } |
377 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
- | 378 | ||
327 | 379 | put_dec_full4(buf, x - q * 10000); |
|
328 | return buf; |
380 | return q; |
329 | } |
381 | } |
330 | /* Same with if's removed. Always emits five digits */ |
382 | |
331 | static noinline_for_stack |
- | |
332 | char *put_dec_full(char *buf, unsigned q) |
383 | /* Based on code by Douglas W. Jones found at |
333 | { |
384 | * |
334 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
385 | * (with permission from the author). |
335 | /* but anyway, gcc produces better code with full-sized ints */ |
386 | * Performs no 64-bit division and hence should be fast on 32-bit machines. |
336 | unsigned d3, d2, d1, d0; |
- | |
337 | d1 = (q>>4) & 0xf; |
387 | */ |
338 | d2 = (q>>8) & 0xf; |
388 | static |
339 | d3 = (q>>12); |
- | |
340 | - | ||
341 | /* |
389 | char *put_dec(char *buf, unsigned long long n) |
342 | * Possible ways to approx. divide by 10 |
390 | { |
343 | * gcc -O2 replaces multiply with shifts and adds |
391 | uint32_t d3, d2, d1, q, h; |
344 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
- | |
345 | * (x * 0x67) >> 10: 1100111 |
- | |
346 | * (x * 0x34) >> 9: 110100 - same |
392 | |
347 | * (x * 0x1a) >> 8: 11010 - same |
393 | if (n < 100*1000*1000) |
348 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
394 | return put_dec_trunc8(buf, n); |
349 | */ |
395 | |
350 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
396 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ |
351 | q = (d0 * 0xcd) >> 11; |
397 | h = (n >> 32); |
352 | d0 = d0 - 10*q; |
398 | d2 = (h ) & 0xffff; |
Line 353... | Line 399... | ||
353 | *buf++ = d0 + '0'; |
399 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
354 | d1 = q + 9*d3 + 5*d2 + d1; |
400 | |
- | 401 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
|
- | 402 | q = put_dec_helper4(buf, q); |
|
- | 403 | ||
- | 404 | q += 7671 * d3 + 9496 * d2 + 6 * d1; |
|
355 | q = (d1 * 0xcd) >> 11; |
405 | q = put_dec_helper4(buf+4, q); |
356 | d1 = d1 - 10*q; |
406 | |
- | 407 | q += 4749 * d3 + 42 * d2; |
|
- | 408 | q = put_dec_helper4(buf+8, q); |
|
- | 409 | ||
357 | *buf++ = d1 + '0'; |
410 | q += 281 * d3; |
358 | 411 | buf += 12; |
|
359 | d2 = q + 2*d2; |
412 | if (q) |
360 | q = (d2 * 0xd) >> 7; |
413 | buf = put_dec_trunc8(buf, q); |
- | 414 | else while (buf[-1] == '0') |
|
- | 415 | --buf; |
|
361 | d2 = d2 - 10*q; |
416 | |
362 | *buf++ = d2 + '0'; |
417 | return buf; |
363 | 418 | } |
|
- | 419 | ||
364 | d3 = q + 4*d3; |
420 | #endif |
365 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
421 | |
- | 422 | /* |
|
- | 423 | * Convert passed number to decimal string. |
|
- | 424 | * Returns the length of string. On buffer overflow, returns 0. |
|
- | 425 | * |
|
- | 426 | * If speed is not important, use snprintf(). It's easy to read the code. |
|
- | 427 | */ |
|
366 | /* q = (d3 * 0x67) >> 10; - would also work */ |
428 | int num_to_str(char *buf, int size, unsigned long long num) |
Line 367... | Line 429... | ||
367 | d3 = d3 - 10*q; |
429 | { |
368 | *buf++ = d3 + '0'; |
430 | char tmp[sizeof(num) * 3]; |
369 | *buf++ = q + '0'; |
431 | int idx, len; |
Line 407... | Line 469... | ||
407 | FORMAT_TYPE_BYTE, |
469 | FORMAT_TYPE_BYTE, |
408 | FORMAT_TYPE_USHORT, |
470 | FORMAT_TYPE_USHORT, |
409 | FORMAT_TYPE_SHORT, |
471 | FORMAT_TYPE_SHORT, |
410 | FORMAT_TYPE_UINT, |
472 | FORMAT_TYPE_UINT, |
411 | FORMAT_TYPE_INT, |
473 | FORMAT_TYPE_INT, |
412 | FORMAT_TYPE_NRCHARS, |
- | |
413 | FORMAT_TYPE_SIZE_T, |
474 | FORMAT_TYPE_SIZE_T, |
414 | FORMAT_TYPE_PTRDIFF |
475 | FORMAT_TYPE_PTRDIFF |
415 | }; |
476 | }; |
Line 416... | Line 477... | ||
416 | 477 | ||
Line 433... | Line 494... | ||
433 | char tmp[66]; |
494 | char tmp[66]; |
434 | char sign; |
495 | char sign; |
435 | char locase; |
496 | char locase; |
436 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
497 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
437 | int i; |
498 | int i; |
- | 499 | bool is_zero = num == 0LL; |
|
Line 438... | Line 500... | ||
438 | 500 | ||
439 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
501 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
440 | * produces same digits or (maybe lowercased) letters */ |
502 | * produces same digits or (maybe lowercased) letters */ |
441 | locase = (spec.flags & SMALL); |
503 | locase = (spec.flags & SMALL); |
Line 454... | Line 516... | ||
454 | sign = ' '; |
516 | sign = ' '; |
455 | spec.field_width--; |
517 | spec.field_width--; |
456 | } |
518 | } |
457 | } |
519 | } |
458 | if (need_pfx) { |
520 | if (need_pfx) { |
459 | spec.field_width--; |
- | |
460 | if (spec.base == 16) |
521 | if (spec.base == 16) |
- | 522 | spec.field_width -= 2; |
|
- | 523 | else if (!is_zero) |
|
461 | spec.field_width--; |
524 | spec.field_width--; |
462 | } |
525 | } |
Line 463... | Line 526... | ||
463 | 526 | ||
464 | /* generate full string in tmp[], in reverse order */ |
527 | /* generate full string in tmp[], in reverse order */ |
465 | i = 0; |
528 | i = 0; |
466 | if (num == 0) |
529 | if (num < spec.base) |
467 | tmp[i++] = '0'; |
530 | tmp[i++] = digits[num] | locase; |
468 | /* Generic code, for any base: |
531 | /* Generic code, for any base: |
469 | else do { |
532 | else do { |
470 | tmp[i++] = (digits[do_div(num,base)] | locase); |
533 | tmp[i++] = (digits[do_div(num,base)] | locase); |
471 | } while (num != 0); |
534 | } while (num != 0); |
Line 502... | Line 565... | ||
502 | *buf = sign; |
565 | *buf = sign; |
503 | ++buf; |
566 | ++buf; |
504 | } |
567 | } |
505 | /* "0x" / "0" prefix */ |
568 | /* "0x" / "0" prefix */ |
506 | if (need_pfx) { |
569 | if (need_pfx) { |
- | 570 | if (spec.base == 16 || !is_zero) { |
|
507 | if (buf < end) |
571 | if (buf < end) |
508 | *buf = '0'; |
572 | *buf = '0'; |
509 | ++buf; |
573 | ++buf; |
- | 574 | } |
|
510 | if (spec.base == 16) { |
575 | if (spec.base == 16) { |
511 | if (buf < end) |
576 | if (buf < end) |
512 | *buf = ('X' | locase); |
577 | *buf = ('X' | locase); |
513 | ++buf; |
578 | ++buf; |
514 | } |
579 | } |
Line 547... | Line 612... | ||
547 | static noinline_for_stack |
612 | static noinline_for_stack |
548 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
613 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
549 | { |
614 | { |
550 | int len, i; |
615 | int len, i; |
Line 551... | Line 616... | ||
551 | 616 | ||
552 | if (s == NULL) |
617 | if ((unsigned long)s < PAGE_SIZE) |
Line 553... | Line 618... | ||
553 | s = "(null)"; |
618 | s = "(null)"; |
Line 554... | Line 619... | ||
554 | 619 | ||
Line 575... | Line 640... | ||
575 | return buf; |
640 | return buf; |
576 | } |
641 | } |
Line 577... | Line 642... | ||
577 | 642 | ||
578 | static noinline_for_stack |
643 | static noinline_for_stack |
579 | char *symbol_string(char *buf, char *end, void *ptr, |
644 | char *symbol_string(char *buf, char *end, void *ptr, |
580 | struct printf_spec spec, char ext) |
645 | struct printf_spec spec, const char *fmt) |
581 | { |
646 | { |
582 | unsigned long value = (unsigned long) ptr; |
647 | unsigned long value; |
583 | #ifdef CONFIG_KALLSYMS |
648 | #ifdef CONFIG_KALLSYMS |
- | 649 | char sym[KSYM_SYMBOL_LEN]; |
|
- | 650 | #endif |
|
- | 651 | ||
- | 652 | if (fmt[1] == 'R') |
|
- | 653 | ptr = __builtin_extract_return_addr(ptr); |
|
- | 654 | value = (unsigned long)ptr; |
|
- | 655 | ||
- | 656 | #ifdef CONFIG_KALLSYMS |
|
- | 657 | if (*fmt == 'B') |
|
584 | char sym[KSYM_SYMBOL_LEN]; |
658 | sprint_backtrace(sym, value); |
585 | if (ext != 'f' && ext != 's') |
659 | else if (*fmt != 'f' && *fmt != 's') |
586 | sprint_symbol(sym, value); |
660 | sprint_symbol(sym, value); |
587 | else |
661 | else |
Line 588... | Line 662... | ||
588 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
662 | sprint_symbol_no_offset(sym, value); |
589 | 663 | ||
590 | return string(buf, end, sym, spec); |
664 | return string(buf, end, sym, spec); |
591 | #else |
665 | #else |
Line 701... | Line 775... | ||
701 | 775 | ||
702 | return string(buf, end, sym, spec); |
776 | return string(buf, end, sym, spec); |
Line 703... | Line 777... | ||
703 | } |
777 | } |
- | 778 | ||
- | 779 | static noinline_for_stack |
|
- | 780 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
|
- | 781 | const char *fmt) |
|
- | 782 | { |
|
- | 783 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
|
- | 784 | negative value, fallback to the default */ |
|
- | 785 | char separator; |
|
- | 786 | ||
- | 787 | if (spec.field_width == 0) |
|
- | 788 | /* nothing to print */ |
|
- | 789 | return buf; |
|
- | 790 | ||
- | 791 | if (ZERO_OR_NULL_PTR(addr)) |
|
- | 792 | /* NULL pointer */ |
|
- | 793 | return string(buf, end, NULL, spec); |
|
- | 794 | ||
- | 795 | switch (fmt[1]) { |
|
- | 796 | case 'C': |
|
- | 797 | separator = ':'; |
|
- | 798 | break; |
|
- | 799 | case 'D': |
|
- | 800 | separator = '-'; |
|
- | 801 | break; |
|
- | 802 | case 'N': |
|
- | 803 | separator = 0; |
|
- | 804 | break; |
|
- | 805 | default: |
|
- | 806 | separator = ' '; |
|
- | 807 | break; |
|
- | 808 | } |
|
- | 809 | ||
- | 810 | if (spec.field_width > 0) |
|
- | 811 | len = min_t(int, spec.field_width, 64); |
|
- | 812 | ||
- | 813 | for (i = 0; i < len && buf < end - 1; i++) { |
|
- | 814 | buf = hex_byte_pack(buf, addr[i]); |
|
- | 815 | ||
- | 816 | if (buf < end && separator && i != len - 1) |
|
- | 817 | *buf++ = separator; |
|
- | 818 | } |
|
- | 819 | ||
- | 820 | return buf; |
|
- | 821 | } |
|
704 | 822 | ||
705 | static noinline_for_stack |
823 | static noinline_for_stack |
706 | char *mac_address_string(char *buf, char *end, u8 *addr, |
824 | char *mac_address_string(char *buf, char *end, u8 *addr, |
707 | struct printf_spec spec, const char *fmt) |
825 | struct printf_spec spec, const char *fmt) |
708 | { |
826 | { |
709 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
827 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
710 | char *p = mac_addr; |
828 | char *p = mac_addr; |
- | 829 | int i; |
|
Line 711... | Line 830... | ||
711 | int i; |
830 | char separator; |
- | 831 | bool reversed = false; |
|
712 | char separator; |
832 | |
- | 833 | switch (fmt[1]) { |
|
- | 834 | case 'F': |
|
713 | 835 | separator = '-'; |
|
- | 836 | break; |
|
- | 837 | ||
- | 838 | case 'R': |
|
- | 839 | reversed = true; |
|
714 | if (fmt[1] == 'F') { /* FDDI canonical format */ |
840 | /* fall through */ |
- | 841 | ||
715 | separator = '-'; |
842 | default: |
Line 716... | Line 843... | ||
716 | } else { |
843 | separator = ':'; |
- | 844 | break; |
|
- | 845 | } |
|
- | 846 | ||
717 | separator = ':'; |
847 | for (i = 0; i < 6; i++) { |
- | 848 | if (reversed) |
|
718 | } |
849 | p = hex_byte_pack(p, addr[5 - i]); |
719 | 850 | else |
|
720 | for (i = 0; i < 6; i++) { |
851 | p = hex_byte_pack(p, addr[i]); |
721 | p = pack_hex_byte(p, addr[i]); |
852 | |
Line 756... | Line 887... | ||
756 | step = 1; |
887 | step = 1; |
757 | break; |
888 | break; |
758 | } |
889 | } |
759 | for (i = 0; i < 4; i++) { |
890 | for (i = 0; i < 4; i++) { |
760 | char temp[3]; /* hold each IP quad in reverse order */ |
891 | char temp[3]; /* hold each IP quad in reverse order */ |
761 | int digits = put_dec_trunc(temp, addr[index]) - temp; |
892 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
762 | if (leading_zeros) { |
893 | if (leading_zeros) { |
763 | if (digits < 3) |
894 | if (digits < 3) |
764 | *p++ = '0'; |
895 | *p++ = '0'; |
765 | if (digits < 2) |
896 | if (digits < 2) |
766 | *p++ = '0'; |
897 | *p++ = '0'; |
Line 801... | Line 932... | ||
801 | * |
932 | * |
802 | * - 'F' For symbolic function descriptor pointers with offset |
933 | * - 'F' For symbolic function descriptor pointers with offset |
803 | * - 'f' For simple symbolic function names without offset |
934 | * - 'f' For simple symbolic function names without offset |
804 | * - 'S' For symbolic direct pointers with offset |
935 | * - 'S' For symbolic direct pointers with offset |
805 | * - 's' For symbolic direct pointers without offset |
936 | * - 's' For symbolic direct pointers without offset |
- | 937 | * - '[FfSs]R' as above with __builtin_extract_return_addr() translation |
|
- | 938 | * - 'B' For backtraced symbolic direct pointers with offset |
|
806 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
939 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
807 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
940 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
808 | * - 'M' For a 6-byte MAC address, it prints the address in the |
941 | * - 'M' For a 6-byte MAC address, it prints the address in the |
809 | * usual colon-separated hex notation |
942 | * usual colon-separated hex notation |
810 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
943 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
811 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
944 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
812 | * with a dash-separated hex notation |
945 | * with a dash-separated hex notation |
- | 946 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
|
813 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
947 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
814 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
948 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
815 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
949 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
- | 950 | * [S][pfs] |
|
- | 951 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
|
- | 952 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
|
816 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
953 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
817 | * IPv6 omits the colons (01020304...0f) |
954 | * IPv6 omits the colons (01020304...0f) |
818 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
955 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
- | 956 | * [S][pfs] |
|
- | 957 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
|
- | 958 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
|
819 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
959 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order |
820 | * - 'I6c' for IPv6 addresses printed as specified by |
960 | * - 'I[6S]c' for IPv6 addresses printed as specified by |
821 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
961 | * http://tools.ietf.org/html/rfc5952 |
822 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
962 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
823 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
963 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
824 | * Options for %pU are: |
964 | * Options for %pU are: |
825 | * b big endian lower case hex (default) |
965 | * b big endian lower case hex (default) |
826 | * B big endian UPPER case hex |
966 | * B big endian UPPER case hex |
Line 834... | Line 974... | ||
834 | * call vsnprintf(->format, *->va_list). |
974 | * call vsnprintf(->format, *->va_list). |
835 | * Implements a "recursive vsnprintf". |
975 | * Implements a "recursive vsnprintf". |
836 | * Do not use this feature without some mechanism to verify the |
976 | * Do not use this feature without some mechanism to verify the |
837 | * correctness of the format string and va_list arguments. |
977 | * correctness of the format string and va_list arguments. |
838 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
978 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
- | 979 | * - 'NF' For a netdev_features_t |
|
- | 980 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
|
- | 981 | * a certain separator (' ' by default): |
|
- | 982 | * C colon |
|
- | 983 | * D dash |
|
- | 984 | * N no separator |
|
- | 985 | * The maximum supported length is 64 bytes of the input. Consider |
|
- | 986 | * to use print_hex_dump() for the larger input. |
|
- | 987 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
|
- | 988 | * (default assumed to be phys_addr_t, passed by reference) |
|
- | 989 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
|
- | 990 | * - 'D[234]' Same as 'd' but for a struct file |
|
839 | * |
991 | * |
840 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
992 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
841 | * function pointers are really function descriptors, which contain a |
993 | * function pointers are really function descriptors, which contain a |
842 | * pointer to the real address. |
994 | * pointer to the real address. |
843 | */ |
995 | */ |
844 | static noinline_for_stack |
996 | static noinline_for_stack |
845 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
997 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
846 | struct printf_spec spec) |
998 | struct printf_spec spec) |
847 | { |
999 | { |
- | 1000 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
|
- | 1001 | ||
848 | if (!ptr) { |
1002 | if (!ptr && *fmt != 'K') { |
849 | /* |
1003 | /* |
850 | * Print (null) with the same width as a pointer so it makes |
1004 | * Print (null) with the same width as a pointer so it makes |
851 | * tabular output look nice. |
1005 | * tabular output look nice. |
852 | */ |
1006 | */ |
853 | if (spec.field_width == -1) |
1007 | if (spec.field_width == -1) |
854 | spec.field_width = 2 * sizeof(void *); |
1008 | spec.field_width = default_width; |
855 | return string(buf, end, "(null)", spec); |
1009 | return string(buf, end, "(null)", spec); |
856 | } |
1010 | } |
Line 857... | Line 1011... | ||
857 | 1011 | ||
858 | switch (*fmt) { |
1012 | switch (*fmt) { |
859 | case 'F': |
1013 | case 'F': |
860 | case 'f': |
1014 | case 'f': |
861 | ptr = dereference_function_descriptor(ptr); |
1015 | ptr = dereference_function_descriptor(ptr); |
862 | /* Fallthrough */ |
1016 | /* Fallthrough */ |
863 | case 'S': |
1017 | case 'S': |
- | 1018 | case 's': |
|
864 | case 's': |
1019 | case 'B': |
865 | return symbol_string(buf, end, ptr, spec, *fmt); |
1020 | return symbol_string(buf, end, ptr, spec, fmt); |
866 | case 'R': |
1021 | case 'R': |
867 | case 'r': |
1022 | case 'r': |
- | 1023 | return resource_string(buf, end, ptr, spec, fmt); |
|
- | 1024 | case 'h': |
|
868 | return resource_string(buf, end, ptr, spec, fmt); |
1025 | return hex_string(buf, end, ptr, spec, fmt); |
869 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
1026 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
870 | case 'm': /* Contiguous: 000102030405 */ |
1027 | case 'm': /* Contiguous: 000102030405 */ |
- | 1028 | /* [mM]F (FDDI) */ |
|
871 | /* [mM]F (FDDI, bit reversed) */ |
1029 | /* [mM]R (Reverse order; Bluetooth) */ |
872 | return mac_address_string(buf, end, ptr, spec, fmt); |
1030 | return mac_address_string(buf, end, ptr, spec, fmt); |
873 | case 'I': /* Formatted IP supported |
1031 | case 'I': /* Formatted IP supported |
874 | * 4: 1.2.3.4 |
1032 | * 4: 1.2.3.4 |
875 | * 6: 0001:0203:...:0708 |
1033 | * 6: 0001:0203:...:0708 |
Line 883... | Line 1041... | ||
883 | case '4': |
1041 | case '4': |
884 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
1042 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
885 | } |
1043 | } |
886 | break; |
1044 | break; |
887 | case 'V': |
1045 | case 'V': |
- | 1046 | { |
|
888 | return buf + vsnprintf(buf, end - buf, |
1047 | va_list va; |
- | 1048 | ||
889 | ((struct va_format *)ptr)->fmt, |
1049 | va_copy(va, *((struct va_format *)ptr)->va); |
- | 1050 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
|
890 | *(((struct va_format *)ptr)->va)); |
1051 | ((struct va_format *)ptr)->fmt, va); |
- | 1052 | va_end(va); |
|
- | 1053 | return buf; |
|
- | 1054 | } |
|
- | 1055 | ||
891 | } |
1056 | } |
892 | spec.flags |= SMALL; |
1057 | spec.flags |= SMALL; |
893 | if (spec.field_width == -1) { |
1058 | if (spec.field_width == -1) { |
894 | spec.field_width = 2 * sizeof(void *); |
1059 | spec.field_width = default_width; |
895 | spec.flags |= ZEROPAD; |
1060 | spec.flags |= ZEROPAD; |
896 | } |
1061 | } |
897 | spec.base = 16; |
1062 | spec.base = 16; |
Line 898... | Line 1063... | ||
898 | 1063 | ||
Line 1004... | Line 1169... | ||
1004 | } |
1169 | } |
Line 1005... | Line 1170... | ||
1005 | 1170 | ||
1006 | qualifier: |
1171 | qualifier: |
1007 | /* get the conversion qualifier */ |
1172 | /* get the conversion qualifier */ |
1008 | spec->qualifier = -1; |
1173 | spec->qualifier = -1; |
1009 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1174 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
1010 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
1175 | _tolower(*fmt) == 'z' || *fmt == 't') { |
1011 | spec->qualifier = *fmt++; |
1176 | spec->qualifier = *fmt++; |
1012 | if (unlikely(spec->qualifier == *fmt)) { |
1177 | if (unlikely(spec->qualifier == *fmt)) { |
1013 | if (spec->qualifier == 'l') { |
1178 | if (spec->qualifier == 'l') { |
1014 | spec->qualifier = 'L'; |
1179 | spec->qualifier = 'L'; |
Line 1034... | Line 1199... | ||
1034 | case 'p': |
1199 | case 'p': |
1035 | spec->type = FORMAT_TYPE_PTR; |
1200 | spec->type = FORMAT_TYPE_PTR; |
1036 | return fmt - start; |
1201 | return fmt - start; |
1037 | /* skip alnum */ |
1202 | /* skip alnum */ |
Line 1038... | Line -... | ||
1038 | - | ||
1039 | case 'n': |
- | |
1040 | spec->type = FORMAT_TYPE_NRCHARS; |
- | |
1041 | return ++fmt - start; |
- | |
1042 | 1203 | ||
1043 | case '%': |
1204 | case '%': |
1044 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
1205 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
Line 1045... | Line 1206... | ||
1045 | return ++fmt - start; |
1206 | return ++fmt - start; |
Line 1060... | Line 1221... | ||
1060 | case 'i': |
1221 | case 'i': |
1061 | spec->flags |= SIGN; |
1222 | spec->flags |= SIGN; |
1062 | case 'u': |
1223 | case 'u': |
1063 | break; |
1224 | break; |
Line -... | Line 1225... | ||
- | 1225 | ||
- | 1226 | case 'n': |
|
- | 1227 | /* |
|
- | 1228 | * Since %n poses a greater security risk than utility, treat |
|
- | 1229 | * it as an invalid format specifier. Warn about its use so |
|
- | 1230 | * that new instances don't get added. |
|
- | 1231 | */ |
|
- | 1232 | // WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt); |
|
- | 1233 | /* Fall-through */ |
|
1064 | 1234 | ||
1065 | default: |
1235 | default: |
1066 | spec->type = FORMAT_TYPE_INVALID; |
1236 | spec->type = FORMAT_TYPE_INVALID; |
1067 | return fmt - start; |
1237 | return fmt - start; |
Line 1072... | Line 1242... | ||
1072 | else if (spec->qualifier == 'l') { |
1242 | else if (spec->qualifier == 'l') { |
1073 | if (spec->flags & SIGN) |
1243 | if (spec->flags & SIGN) |
1074 | spec->type = FORMAT_TYPE_LONG; |
1244 | spec->type = FORMAT_TYPE_LONG; |
1075 | else |
1245 | else |
1076 | spec->type = FORMAT_TYPE_ULONG; |
1246 | spec->type = FORMAT_TYPE_ULONG; |
1077 | } else if (TOLOWER(spec->qualifier) == 'z') { |
1247 | } else if (_tolower(spec->qualifier) == 'z') { |
1078 | spec->type = FORMAT_TYPE_SIZE_T; |
1248 | spec->type = FORMAT_TYPE_SIZE_T; |
1079 | } else if (spec->qualifier == 't') { |
1249 | } else if (spec->qualifier == 't') { |
1080 | spec->type = FORMAT_TYPE_PTRDIFF; |
1250 | spec->type = FORMAT_TYPE_PTRDIFF; |
1081 | } else if (spec->qualifier == 'H') { |
1251 | } else if (spec->qualifier == 'H') { |
1082 | if (spec->flags & SIGN) |
1252 | if (spec->flags & SIGN) |
Line 1108... | Line 1278... | ||
1108 | * This function follows C99 vsnprintf, but has some extensions: |
1278 | * This function follows C99 vsnprintf, but has some extensions: |
1109 | * %pS output the name of a text symbol with offset |
1279 | * %pS output the name of a text symbol with offset |
1110 | * %ps output the name of a text symbol without offset |
1280 | * %ps output the name of a text symbol without offset |
1111 | * %pF output the name of a function pointer with its offset |
1281 | * %pF output the name of a function pointer with its offset |
1112 | * %pf output the name of a function pointer without its offset |
1282 | * %pf output the name of a function pointer without its offset |
- | 1283 | * %pB output the name of a backtrace symbol with its offset |
|
1113 | * %pR output the address range in a struct resource with decoded flags |
1284 | * %pR output the address range in a struct resource with decoded flags |
1114 | * %pr output the address range in a struct resource with raw flags |
1285 | * %pr output the address range in a struct resource with raw flags |
1115 | * %pM output a 6-byte MAC address with colons |
1286 | * %pM output a 6-byte MAC address with colons |
- | 1287 | * %pMR output a 6-byte MAC address with colons in reversed order |
|
- | 1288 | * %pMF output a 6-byte MAC address with dashes |
|
1116 | * %pm output a 6-byte MAC address without colons |
1289 | * %pm output a 6-byte MAC address without colons |
- | 1290 | * %pmR output a 6-byte MAC address without colons in reversed order |
|
1117 | * %pI4 print an IPv4 address without leading zeros |
1291 | * %pI4 print an IPv4 address without leading zeros |
1118 | * %pi4 print an IPv4 address with leading zeros |
1292 | * %pi4 print an IPv4 address with leading zeros |
1119 | * %pI6 print an IPv6 address with colons |
1293 | * %pI6 print an IPv6 address with colons |
1120 | * %pi6 print an IPv6 address without colons |
1294 | * %pi6 print an IPv6 address without colons |
1121 | * %pI6c print an IPv6 address as specified by |
1295 | * %pI6c print an IPv6 address as specified by RFC 5952 |
1122 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
1296 | * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
- | 1297 | * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
|
1123 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1298 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1124 | * case. |
1299 | * case. |
- | 1300 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 |
|
- | 1301 | * bytes of the input) |
|
1125 | * %n is ignored |
1302 | * %n is ignored |
1126 | * |
1303 | * |
- | 1304 | * ** Please update Documentation/printk-formats.txt when making changes ** |
|
- | 1305 | * |
|
1127 | * The return value is the number of characters which would |
1306 | * The return value is the number of characters which would |
1128 | * be generated for the given input, excluding the trailing |
1307 | * be generated for the given input, excluding the trailing |
1129 | * '\0', as per ISO C99. If you want to have the exact |
1308 | * '\0', as per ISO C99. If you want to have the exact |
1130 | * number of characters written into @buf as return value |
1309 | * number of characters written into @buf as return value |
1131 | * (not including the trailing '\0'), use vscnprintf(). If the |
1310 | * (not including the trailing '\0'), use vscnprintf(). If the |
1132 | * return is greater than or equal to @size, the resulting |
1311 | * return is greater than or equal to @size, the resulting |
1133 | * string is truncated. |
1312 | * string is truncated. |
1134 | * |
1313 | * |
1135 | * Call this function if you are already dealing with a va_list. |
1314 | * If you're not already dealing with a va_list consider using snprintf(). |
1136 | * You probably want snprintf() instead. |
- | |
1137 | */ |
1315 | */ |
1138 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
1316 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
1139 | { |
1317 | { |
1140 | unsigned long long num; |
1318 | unsigned long long num; |
1141 | char *str, *end; |
1319 | char *str, *end; |
Line 1225... | Line 1403... | ||
1225 | if (str < end) |
1403 | if (str < end) |
1226 | *str = '%'; |
1404 | *str = '%'; |
1227 | ++str; |
1405 | ++str; |
1228 | break; |
1406 | break; |
Line 1229... | Line -... | ||
1229 | - | ||
1230 | case FORMAT_TYPE_NRCHARS: { |
- | |
1231 | u8 qualifier = spec.qualifier; |
- | |
1232 | - | ||
1233 | if (qualifier == 'l') { |
- | |
1234 | long *ip = va_arg(args, long *); |
- | |
1235 | *ip = (str - buf); |
- | |
1236 | } else if (TOLOWER(qualifier) == 'z') { |
- | |
1237 | size_t *ip = va_arg(args, size_t *); |
- | |
1238 | *ip = (str - buf); |
- | |
1239 | } else { |
- | |
1240 | int *ip = va_arg(args, int *); |
- | |
1241 | *ip = (str - buf); |
- | |
1242 | } |
- | |
1243 | break; |
- | |
1244 | } |
- | |
1245 | 1407 | ||
1246 | default: |
1408 | default: |
1247 | switch (spec.type) { |
1409 | switch (spec.type) { |
1248 | case FORMAT_TYPE_LONG_LONG: |
1410 | case FORMAT_TYPE_LONG_LONG: |
1249 | num = va_arg(args, long long); |
1411 | num = va_arg(args, long long); |
Line 1253... | Line 1415... | ||
1253 | break; |
1415 | break; |
1254 | case FORMAT_TYPE_LONG: |
1416 | case FORMAT_TYPE_LONG: |
1255 | num = va_arg(args, long); |
1417 | num = va_arg(args, long); |
1256 | break; |
1418 | break; |
1257 | case FORMAT_TYPE_SIZE_T: |
1419 | case FORMAT_TYPE_SIZE_T: |
- | 1420 | if (spec.flags & SIGN) |
|
- | 1421 | num = va_arg(args, ssize_t); |
|
- | 1422 | else |
|
1258 | num = va_arg(args, size_t); |
1423 | num = va_arg(args, size_t); |
1259 | break; |
1424 | break; |
1260 | case FORMAT_TYPE_PTRDIFF: |
1425 | case FORMAT_TYPE_PTRDIFF: |
1261 | num = va_arg(args, ptrdiff_t); |
1426 | num = va_arg(args, ptrdiff_t); |
1262 | break; |
1427 | break; |
Line 1294... | Line 1459... | ||
1294 | return str-buf; |
1459 | return str-buf; |
Line 1295... | Line 1460... | ||
1295 | 1460 | ||
1296 | } |
1461 | } |
Line -... | Line 1462... | ||
- | 1462 | EXPORT_SYMBOL(vsnprintf); |
|
- | 1463 | ||
- | 1464 | /** |
|
- | 1465 | * vscnprintf - Format a string and place it in a buffer |
|
- | 1466 | * @buf: The buffer to place the result into |
|
- | 1467 | * @size: The size of the buffer, including the trailing null space |
|
- | 1468 | * @fmt: The format string to use |
|
- | 1469 | * @args: Arguments for the format string |
|
- | 1470 | * |
|
- | 1471 | * The return value is the number of characters which have been written into |
|
- | 1472 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
|
- | 1473 | * returns 0. |
|
- | 1474 | * |
|
- | 1475 | * If you're not already dealing with a va_list consider using scnprintf(). |
|
- | 1476 | * |
|
1297 | EXPORT_SYMBOL(vsnprintf); |
1477 | * See the vsnprintf() documentation for format string extensions over C99. |
1298 | 1478 | */ |
|
1299 | int vsprintf(char *buf, const char *fmt, va_list args) |
- | |
1300 | { |
1479 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
Line -... | Line 1480... | ||
- | 1480 | { |
|
- | 1481 | int i; |
|
- | 1482 | ||
- | 1483 | i = vsnprintf(buf, size, fmt, args); |
|
- | 1484 | ||
- | 1485 | if (likely(i < size)) |
|
- | 1486 | return i; |
|
- | 1487 | if (size != 0) |
|
- | 1488 | return size - 1; |
|
Line 1301... | Line 1489... | ||
1301 | return vsnprintf(buf, INT_MAX, fmt, args); |
1489 | return 0; |
1302 | } |
1490 | } |
1303 | 1491 | EXPORT_SYMBOL(vscnprintf); |
|
1304 | 1492 | ||
Line 1327... | Line 1515... | ||
1327 | 1515 | ||
1328 | return i; |
1516 | return i; |
1329 | } |
1517 | } |
Line -... | Line 1518... | ||
- | 1518 | EXPORT_SYMBOL(snprintf); |
|
- | 1519 | ||
- | 1520 | /** |
|
- | 1521 | * scnprintf - Format a string and place it in a buffer |
|
- | 1522 | * @buf: The buffer to place the result into |
|
- | 1523 | * @size: The size of the buffer, including the trailing null space |
|
- | 1524 | * @fmt: The format string to use |
|
- | 1525 | * @...: Arguments for the format string |
|
- | 1526 | * |
|
- | 1527 | * The return value is the number of characters written into @buf not including |
|
- | 1528 | * the trailing '\0'. If @size is == 0 the function returns 0. |
|
- | 1529 | */ |
|
- | 1530 | ||
- | 1531 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
|
- | 1532 | { |
|
- | 1533 | va_list args; |
|
- | 1534 | int i; |
|
- | 1535 | ||
- | 1536 | va_start(args, fmt); |
|
- | 1537 | i = vscnprintf(buf, size, fmt, args); |
|
- | 1538 | va_end(args); |
|
- | 1539 | ||
- | 1540 | return i; |
|
- | 1541 | } |
|
- | 1542 | EXPORT_SYMBOL(scnprintf); |
|
- | 1543 | ||
- | 1544 | /** |
|
- | 1545 | * vsprintf - Format a string and place it in a buffer |
|
- | 1546 | * @buf: The buffer to place the result into |
|
- | 1547 | * @fmt: The format string to use |
|
- | 1548 | * @args: Arguments for the format string |
|
- | 1549 | * |
|
- | 1550 | * The function returns the number of characters written |
|
- | 1551 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
|
- | 1552 | * buffer overflows. |
|
- | 1553 | * |
|
- | 1554 | * If you're not already dealing with a va_list consider using sprintf(). |
|
- | 1555 | * |
|
- | 1556 | * See the vsnprintf() documentation for format string extensions over C99. |
|
- | 1557 | */ |
|
- | 1558 | int vsprintf(char *buf, const char *fmt, va_list args) |
|
- | 1559 | { |
|
- | 1560 | return vsnprintf(buf, INT_MAX, fmt, args); |
|
Line 1330... | Line 1561... | ||
1330 | EXPORT_SYMBOL(snprintf); |
1561 | } |
1331 | 1562 | EXPORT_SYMBOL(vsprintf); |
|
1332 | 1563 | ||
1333 | /** |
1564 | /** |
Line 1351... | Line 1582... | ||
1351 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1582 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1352 | va_end(args); |
1583 | va_end(args); |
Line 1353... | Line 1584... | ||
1353 | 1584 | ||
1354 | return i; |
1585 | return i; |
- | 1586 | } |
|
- | 1587 | EXPORT_SYMBOL(sprintf); |
|
- | 1588 | /** |
|
- | 1589 | * vsscanf - Unformat a buffer into a list of arguments |
|
- | 1590 | * @buf: input buffer |
|
- | 1591 | * @fmt: format of buffer |
|
- | 1592 | * @args: arguments |
|
- | 1593 | */ |
|
- | 1594 | int vsscanf(const char *buf, const char *fmt, va_list args) |
|
- | 1595 | { |
|
- | 1596 | const char *str = buf; |
|
- | 1597 | char *next; |
|
- | 1598 | char digit; |
|
- | 1599 | int num = 0; |
|
- | 1600 | u8 qualifier; |
|
- | 1601 | unsigned int base; |
|
- | 1602 | union { |
|
- | 1603 | long long s; |
|
- | 1604 | unsigned long long u; |
|
- | 1605 | } val; |
|
- | 1606 | s16 field_width; |
|
- | 1607 | bool is_sign; |
|
- | 1608 | ||
- | 1609 | while (*fmt) { |
|
- | 1610 | /* skip any white space in format */ |
|
- | 1611 | /* white space in format matchs any amount of |
|
- | 1612 | * white space, including none, in the input. |
|
- | 1613 | */ |
|
- | 1614 | if (isspace(*fmt)) { |
|
- | 1615 | fmt = skip_spaces(++fmt); |
|
- | 1616 | str = skip_spaces(str); |
|
- | 1617 | } |
|
- | 1618 | ||
- | 1619 | /* anything that is not a conversion must match exactly */ |
|
- | 1620 | if (*fmt != '%' && *fmt) { |
|
- | 1621 | if (*fmt++ != *str++) |
|
- | 1622 | break; |
|
- | 1623 | continue; |
|
Line -... | Line 1624... | ||
- | 1624 | } |
|
- | 1625 | ||
- | 1626 | if (!*fmt) |
|
- | 1627 | break; |
|
- | 1628 | ++fmt; |
|
- | 1629 | ||
- | 1630 | /* skip this conversion. |
|
- | 1631 | * advance both strings to next white space |
|
- | 1632 | */ |
|
- | 1633 | if (*fmt == '*') { |
|
- | 1634 | if (!*str) |
|
- | 1635 | break; |
|
- | 1636 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
|
- | 1637 | fmt++; |
|
- | 1638 | while (!isspace(*str) && *str) |
|
- | 1639 | str++; |
|
- | 1640 | continue; |
|
- | 1641 | } |
|
- | 1642 | ||
- | 1643 | /* get field width */ |
|
- | 1644 | field_width = -1; |
|
- | 1645 | if (isdigit(*fmt)) { |
|
- | 1646 | field_width = skip_atoi(&fmt); |
|
- | 1647 | if (field_width <= 0) |
|
- | 1648 | break; |
|
- | 1649 | } |
|
- | 1650 | ||
- | 1651 | /* get conversion qualifier */ |
|
- | 1652 | qualifier = -1; |
|
- | 1653 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
|
- | 1654 | _tolower(*fmt) == 'z') { |
|
- | 1655 | qualifier = *fmt++; |
|
- | 1656 | if (unlikely(qualifier == *fmt)) { |
|
- | 1657 | if (qualifier == 'h') { |
|
- | 1658 | qualifier = 'H'; |
|
- | 1659 | fmt++; |
|
- | 1660 | } else if (qualifier == 'l') { |
|
- | 1661 | qualifier = 'L'; |
|
- | 1662 | fmt++; |
|
- | 1663 | } |
|
- | 1664 | } |
|
- | 1665 | } |
|
- | 1666 | ||
- | 1667 | if (!*fmt) |
|
- | 1668 | break; |
|
- | 1669 | ||
- | 1670 | if (*fmt == 'n') { |
|
- | 1671 | /* return number of characters read so far */ |
|
- | 1672 | *va_arg(args, int *) = str - buf; |
|
- | 1673 | ++fmt; |
|
- | 1674 | continue; |
|
- | 1675 | } |
|
- | 1676 | ||
- | 1677 | if (!*str) |
|
- | 1678 | break; |
|
- | 1679 | ||
- | 1680 | base = 10; |
|
- | 1681 | is_sign = false; |
|
- | 1682 | ||
- | 1683 | switch (*fmt++) { |
|
- | 1684 | case 'c': |
|
- | 1685 | { |
|
- | 1686 | char *s = (char *)va_arg(args, char*); |
|
- | 1687 | if (field_width == -1) |
|
- | 1688 | field_width = 1; |
|
- | 1689 | do { |
|
- | 1690 | *s++ = *str++; |
|
- | 1691 | } while (--field_width > 0 && *str); |
|
- | 1692 | num++; |
|
- | 1693 | } |
|
- | 1694 | continue; |
|
- | 1695 | case 's': |
|
- | 1696 | { |
|
- | 1697 | char *s = (char *)va_arg(args, char *); |
|
- | 1698 | if (field_width == -1) |
|
- | 1699 | field_width = SHRT_MAX; |
|
- | 1700 | /* first, skip leading white space in buffer */ |
|
- | 1701 | str = skip_spaces(str); |
|
- | 1702 | ||
- | 1703 | /* now copy until next white space */ |
|
- | 1704 | while (*str && !isspace(*str) && field_width--) |
|
- | 1705 | *s++ = *str++; |
|
- | 1706 | *s = '\0'; |
|
- | 1707 | num++; |
|
- | 1708 | } |
|
- | 1709 | continue; |
|
- | 1710 | case 'o': |
|
- | 1711 | base = 8; |
|
- | 1712 | break; |
|
- | 1713 | case 'x': |
|
- | 1714 | case 'X': |
|
- | 1715 | base = 16; |
|
- | 1716 | break; |
|
- | 1717 | case 'i': |
|
- | 1718 | base = 0; |
|
- | 1719 | case 'd': |
|
- | 1720 | is_sign = true; |
|
- | 1721 | case 'u': |
|
- | 1722 | break; |
|
- | 1723 | case '%': |
|
- | 1724 | /* looking for '%' in str */ |
|
- | 1725 | if (*str++ != '%') |
|
- | 1726 | return num; |
|
- | 1727 | continue; |
|
- | 1728 | default: |
|
- | 1729 | /* invalid format; stop here */ |
|
- | 1730 | return num; |
|
- | 1731 | } |
|
- | 1732 | ||
- | 1733 | /* have some sort of integer conversion. |
|
- | 1734 | * first, skip white space in buffer. |
|
- | 1735 | */ |
|
- | 1736 | str = skip_spaces(str); |
|
- | 1737 | ||
- | 1738 | digit = *str; |
|
- | 1739 | if (is_sign && digit == '-') |
|
- | 1740 | digit = *(str + 1); |
|
- | 1741 | ||
- | 1742 | if (!digit |
|
- | 1743 | || (base == 16 && !isxdigit(digit)) |
|
- | 1744 | || (base == 10 && !isdigit(digit)) |
|
- | 1745 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
|
- | 1746 | || (base == 0 && !isdigit(digit))) |
|
- | 1747 | break; |
|
- | 1748 | ||
- | 1749 | if (is_sign) |
|
- | 1750 | val.s = qualifier != 'L' ? |
|
- | 1751 | simple_strtol(str, &next, base) : |
|
- | 1752 | simple_strtoll(str, &next, base); |
|
- | 1753 | else |
|
- | 1754 | val.u = qualifier != 'L' ? |
|
- | 1755 | simple_strtoul(str, &next, base) : |
|
- | 1756 | simple_strtoull(str, &next, base); |
|
- | 1757 | ||
- | 1758 | if (field_width > 0 && next - str > field_width) { |
|
- | 1759 | if (base == 0) |
|
- | 1760 | _parse_integer_fixup_radix(str, &base); |
|
- | 1761 | while (next - str > field_width) { |
|
- | 1762 | if (is_sign) |
|
- | 1763 | val.s = div_s64(val.s, base); |
|
- | 1764 | else |
|
- | 1765 | val.u = div_u64(val.u, base); |
|
- | 1766 | --next; |
|
- | 1767 | } |
|
- | 1768 | } |
|
- | 1769 | ||
- | 1770 | switch (qualifier) { |
|
- | 1771 | case 'H': /* that's 'hh' in format */ |
|
- | 1772 | if (is_sign) |
|
- | 1773 | *va_arg(args, signed char *) = val.s; |
|
- | 1774 | else |
|
- | 1775 | *va_arg(args, unsigned char *) = val.u; |
|
- | 1776 | break; |
|
- | 1777 | case 'h': |
|
- | 1778 | if (is_sign) |
|
- | 1779 | *va_arg(args, short *) = val.s; |
|
- | 1780 | else |
|
- | 1781 | *va_arg(args, unsigned short *) = val.u; |
|
- | 1782 | break; |
|
- | 1783 | case 'l': |
|
- | 1784 | if (is_sign) |
|
- | 1785 | *va_arg(args, long *) = val.s; |
|
- | 1786 | else |
|
- | 1787 | *va_arg(args, unsigned long *) = val.u; |
|
- | 1788 | break; |
|
- | 1789 | case 'L': |
|
- | 1790 | if (is_sign) |
|
- | 1791 | *va_arg(args, long long *) = val.s; |
|
- | 1792 | else |
|
- | 1793 | *va_arg(args, unsigned long long *) = val.u; |
|
- | 1794 | break; |
|
- | 1795 | case 'Z': |
|
- | 1796 | case 'z': |
|
- | 1797 | *va_arg(args, size_t *) = val.u; |
|
- | 1798 | break; |
|
- | 1799 | default: |
|
- | 1800 | if (is_sign) |
|
- | 1801 | *va_arg(args, int *) = val.s; |
|
- | 1802 | else |
|
- | 1803 | *va_arg(args, unsigned int *) = val.u; |
|
- | 1804 | break; |
|
- | 1805 | } |
|
- | 1806 | num++; |
|
- | 1807 | ||
- | 1808 | if (!next) |
|
- | 1809 | break; |
|
- | 1810 | str = next; |
|
- | 1811 | } |
|
- | 1812 | ||
- | 1813 | return num; |
|
- | 1814 | } |
|
- | 1815 | EXPORT_SYMBOL(vsscanf); |
|
- | 1816 | ||
- | 1817 | /** |
|
- | 1818 | * sscanf - Unformat a buffer into a list of arguments |
|
- | 1819 | * @buf: input buffer |
|
- | 1820 | * @fmt: formatting of buffer |
|
- | 1821 | * @...: resulting arguments |
|
- | 1822 | */ |
|
- | 1823 | int sscanf(const char *buf, const char *fmt, ...) |
|
- | 1824 | { |
|
- | 1825 | va_list args; |
|
- | 1826 | int i; |
|
- | 1827 | ||
- | 1828 | va_start(args, fmt); |
|
- | 1829 | i = vsscanf(buf, fmt, args); |
|
- | 1830 | va_end(args); |
|
- | 1831 | ||
- | 1832 | return i; |