Rev 1871 | Rev 4279 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1871 | Rev 3031 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * linux/lib/vsprintf.c |
2 | * linux/lib/vsprintf.c |
3 | * |
3 | * |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
5 | */ |
5 | */ |
6 | 6 | ||
7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ |
7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ |
8 | /* |
8 | /* |
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
10 | */ |
10 | */ |
11 | 11 | ||
12 | /* |
12 | /* |
13 | * Fri Jul 13 2001 Crutcher Dunnavant |
13 | * Fri Jul 13 2001 Crutcher Dunnavant |
14 | * - changed to provide snprintf and vsnprintf functions |
14 | * - changed to provide snprintf and vsnprintf functions |
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 | */ |
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 |
- | 27 | #include |
|
27 | 28 | ||
28 | #include |
29 | #include |
29 | 30 | ||
30 | struct va_format { |
31 | struct va_format { |
31 | const char *fmt; |
32 | const char *fmt; |
32 | va_list *va; |
33 | va_list *va; |
33 | }; |
34 | }; |
34 | 35 | ||
35 | #ifndef dereference_function_descriptor |
36 | #ifndef dereference_function_descriptor |
36 | #define dereference_function_descriptor(p) (p) |
37 | #define dereference_function_descriptor(p) (p) |
37 | #endif |
38 | #endif |
38 | 39 | ||
39 | const char hex_asc[] = "0123456789abcdef"; |
40 | const char hex_asc[] = "0123456789abcdef"; |
40 | 41 | ||
41 | /* Works only for digits and letters, but small and fast */ |
42 | /* Works only for digits and letters, but small and fast */ |
42 | #define TOLOWER(x) ((x) | 0x20) |
43 | #define TOLOWER(x) ((x) | 0x20) |
43 | 44 | ||
44 | static unsigned int simple_guess_base(const char *cp) |
45 | static unsigned int simple_guess_base(const char *cp) |
45 | { |
46 | { |
46 | if (cp[0] == '0') { |
47 | if (cp[0] == '0') { |
47 | if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) |
48 | if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) |
48 | return 16; |
49 | return 16; |
49 | else |
50 | else |
50 | return 8; |
51 | return 8; |
51 | } else { |
52 | } else { |
52 | return 10; |
53 | return 10; |
53 | } |
54 | } |
54 | } |
55 | } |
55 | 56 | ||
56 | /** |
57 | /** |
57 | * simple_strtoull - convert a string to an unsigned long long |
58 | * simple_strtoull - convert a string to an unsigned long long |
58 | * @cp: The start of the string |
59 | * @cp: The start of the string |
59 | * @endp: A pointer to the end of the parsed string will be placed here |
60 | * @endp: A pointer to the end of the parsed string will be placed here |
60 | * @base: The number base to use |
61 | * @base: The number base to use |
61 | */ |
62 | */ |
62 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
63 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
63 | { |
64 | { |
64 | unsigned long long result = 0; |
65 | unsigned long long result = 0; |
65 | 66 | ||
66 | if (!base) |
67 | if (!base) |
67 | base = simple_guess_base(cp); |
68 | base = simple_guess_base(cp); |
68 | 69 | ||
69 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
70 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
70 | cp += 2; |
71 | cp += 2; |
71 | 72 | ||
72 | while (isxdigit(*cp)) { |
73 | while (isxdigit(*cp)) { |
73 | unsigned int value; |
74 | unsigned int value; |
74 | 75 | ||
75 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
76 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
76 | if (value >= base) |
77 | if (value >= base) |
77 | break; |
78 | break; |
78 | result = result * base + value; |
79 | result = result * base + value; |
79 | cp++; |
80 | cp++; |
80 | } |
81 | } |
81 | if (endp) |
82 | if (endp) |
82 | *endp = (char *)cp; |
83 | *endp = (char *)cp; |
83 | 84 | ||
84 | return result; |
85 | return result; |
85 | } |
86 | } |
86 | EXPORT_SYMBOL(simple_strtoull); |
87 | EXPORT_SYMBOL(simple_strtoull); |
87 | 88 | ||
88 | /** |
89 | /** |
89 | * simple_strtoul - convert a string to an unsigned long |
90 | * simple_strtoul - convert a string to an unsigned long |
90 | * @cp: The start of the string |
91 | * @cp: The start of the string |
91 | * @endp: A pointer to the end of the parsed string will be placed here |
92 | * @endp: A pointer to the end of the parsed string will be placed here |
92 | * @base: The number base to use |
93 | * @base: The number base to use |
93 | */ |
94 | */ |
94 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
95 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
95 | { |
96 | { |
96 | return simple_strtoull(cp, endp, base); |
97 | return simple_strtoull(cp, endp, base); |
97 | } |
98 | } |
98 | EXPORT_SYMBOL(simple_strtoul); |
99 | EXPORT_SYMBOL(simple_strtoul); |
99 | 100 | ||
100 | /** |
101 | /** |
101 | * simple_strtol - convert a string to a signed long |
102 | * simple_strtol - convert a string to a signed long |
102 | * @cp: The start of the string |
103 | * @cp: The start of the string |
103 | * @endp: A pointer to the end of the parsed string will be placed here |
104 | * @endp: A pointer to the end of the parsed string will be placed here |
104 | * @base: The number base to use |
105 | * @base: The number base to use |
105 | */ |
106 | */ |
106 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
107 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
107 | { |
108 | { |
108 | if (*cp == '-') |
109 | if (*cp == '-') |
109 | return -simple_strtoul(cp + 1, endp, base); |
110 | return -simple_strtoul(cp + 1, endp, base); |
110 | 111 | ||
111 | return simple_strtoul(cp, endp, base); |
112 | return simple_strtoul(cp, endp, base); |
112 | } |
113 | } |
113 | EXPORT_SYMBOL(simple_strtol); |
114 | EXPORT_SYMBOL(simple_strtol); |
114 | 115 | ||
115 | /** |
116 | /** |
116 | * simple_strtoll - convert a string to a signed long long |
117 | * simple_strtoll - convert a string to a signed long long |
117 | * @cp: The start of the string |
118 | * @cp: The start of the string |
118 | * @endp: A pointer to the end of the parsed string will be placed here |
119 | * @endp: A pointer to the end of the parsed string will be placed here |
119 | * @base: The number base to use |
120 | * @base: The number base to use |
120 | */ |
121 | */ |
121 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
122 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
122 | { |
123 | { |
123 | if (*cp == '-') |
124 | if (*cp == '-') |
124 | return -simple_strtoull(cp + 1, endp, base); |
125 | return -simple_strtoull(cp + 1, endp, base); |
125 | 126 | ||
126 | return simple_strtoull(cp, endp, base); |
127 | return simple_strtoull(cp, endp, base); |
127 | } |
128 | } |
128 | EXPORT_SYMBOL(simple_strtoll); |
129 | EXPORT_SYMBOL(simple_strtoll); |
129 | 130 | ||
130 | /** |
131 | /** |
131 | * strict_strtoul - convert a string to an unsigned long strictly |
132 | * strict_strtoul - convert a string to an unsigned long strictly |
132 | * @cp: The string to be converted |
133 | * @cp: The string to be converted |
133 | * @base: The number base to use |
134 | * @base: The number base to use |
134 | * @res: The converted result value |
135 | * @res: The converted result value |
135 | * |
136 | * |
136 | * strict_strtoul converts a string to an unsigned long only if the |
137 | * strict_strtoul converts a string to an unsigned long only if the |
137 | * string is really an unsigned long string, any string containing |
138 | * string is really an unsigned long string, any string containing |
138 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
139 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
139 | * only a newline char at the tail is acceptible because people generally |
140 | * only a newline char at the tail is acceptible because people generally |
140 | * change a module parameter in the following way: |
141 | * change a module parameter in the following way: |
141 | * |
142 | * |
142 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
143 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
143 | * |
144 | * |
144 | * echo will append a newline to the tail. |
145 | * echo will append a newline to the tail. |
145 | * |
146 | * |
146 | * It returns 0 if conversion is successful and *res is set to the converted |
147 | * It returns 0 if conversion is successful and *res is set to the converted |
147 | * value, otherwise it returns -EINVAL and *res is set to 0. |
148 | * value, otherwise it returns -EINVAL and *res is set to 0. |
148 | * |
149 | * |
149 | * simple_strtoul just ignores the successive invalid characters and |
150 | * simple_strtoul just ignores the successive invalid characters and |
150 | * return the converted value of prefix part of the string. |
151 | * return the converted value of prefix part of the string. |
151 | */ |
152 | */ |
152 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
153 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
153 | { |
154 | { |
154 | char *tail; |
155 | char *tail; |
155 | unsigned long val; |
156 | unsigned long val; |
156 | 157 | ||
157 | *res = 0; |
158 | *res = 0; |
158 | if (!*cp) |
159 | if (!*cp) |
159 | return -EINVAL; |
160 | return -EINVAL; |
160 | 161 | ||
161 | val = simple_strtoul(cp, &tail, base); |
162 | val = simple_strtoul(cp, &tail, base); |
162 | if (tail == cp) |
163 | if (tail == cp) |
163 | return -EINVAL; |
164 | return -EINVAL; |
164 | 165 | ||
165 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
166 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
166 | *res = val; |
167 | *res = val; |
167 | return 0; |
168 | return 0; |
168 | } |
169 | } |
169 | 170 | ||
170 | return -EINVAL; |
171 | return -EINVAL; |
171 | } |
172 | } |
172 | EXPORT_SYMBOL(strict_strtoul); |
173 | EXPORT_SYMBOL(strict_strtoul); |
173 | 174 | ||
174 | /** |
175 | /** |
175 | * strict_strtol - convert a string to a long strictly |
176 | * strict_strtol - convert a string to a long strictly |
176 | * @cp: The string to be converted |
177 | * @cp: The string to be converted |
177 | * @base: The number base to use |
178 | * @base: The number base to use |
178 | * @res: The converted result value |
179 | * @res: The converted result value |
179 | * |
180 | * |
180 | * strict_strtol is similiar to strict_strtoul, but it allows the first |
181 | * strict_strtol is similiar to strict_strtoul, but it allows the first |
181 | * character of a string is '-'. |
182 | * character of a string is '-'. |
182 | * |
183 | * |
183 | * It returns 0 if conversion is successful and *res is set to the converted |
184 | * It returns 0 if conversion is successful and *res is set to the converted |
184 | * value, otherwise it returns -EINVAL and *res is set to 0. |
185 | * value, otherwise it returns -EINVAL and *res is set to 0. |
185 | */ |
186 | */ |
186 | int strict_strtol(const char *cp, unsigned int base, long *res) |
187 | int strict_strtol(const char *cp, unsigned int base, long *res) |
187 | { |
188 | { |
188 | int ret; |
189 | int ret; |
189 | if (*cp == '-') { |
190 | if (*cp == '-') { |
190 | ret = strict_strtoul(cp + 1, base, (unsigned long *)res); |
191 | ret = strict_strtoul(cp + 1, base, (unsigned long *)res); |
191 | if (!ret) |
192 | if (!ret) |
192 | *res = -(*res); |
193 | *res = -(*res); |
193 | } else { |
194 | } else { |
194 | ret = strict_strtoul(cp, base, (unsigned long *)res); |
195 | ret = strict_strtoul(cp, base, (unsigned long *)res); |
195 | } |
196 | } |
196 | 197 | ||
197 | return ret; |
198 | return ret; |
198 | } |
199 | } |
199 | EXPORT_SYMBOL(strict_strtol); |
200 | EXPORT_SYMBOL(strict_strtol); |
200 | 201 | ||
201 | /** |
202 | /** |
202 | * strict_strtoull - convert a string to an unsigned long long strictly |
203 | * strict_strtoull - convert a string to an unsigned long long strictly |
203 | * @cp: The string to be converted |
204 | * @cp: The string to be converted |
204 | * @base: The number base to use |
205 | * @base: The number base to use |
205 | * @res: The converted result value |
206 | * @res: The converted result value |
206 | * |
207 | * |
207 | * strict_strtoull converts a string to an unsigned long long only if the |
208 | * strict_strtoull converts a string to an unsigned long long only if the |
208 | * string is really an unsigned long long string, any string containing |
209 | * string is really an unsigned long long string, any string containing |
209 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
210 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
210 | * only a newline char at the tail is acceptible because people generally |
211 | * only a newline char at the tail is acceptible because people generally |
211 | * change a module parameter in the following way: |
212 | * change a module parameter in the following way: |
212 | * |
213 | * |
213 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
214 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
214 | * |
215 | * |
215 | * echo will append a newline to the tail of the string. |
216 | * echo will append a newline to the tail of the string. |
216 | * |
217 | * |
217 | * It returns 0 if conversion is successful and *res is set to the converted |
218 | * It returns 0 if conversion is successful and *res is set to the converted |
218 | * value, otherwise it returns -EINVAL and *res is set to 0. |
219 | * value, otherwise it returns -EINVAL and *res is set to 0. |
219 | * |
220 | * |
220 | * simple_strtoull just ignores the successive invalid characters and |
221 | * simple_strtoull just ignores the successive invalid characters and |
221 | * return the converted value of prefix part of the string. |
222 | * return the converted value of prefix part of the string. |
222 | */ |
223 | */ |
223 | int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) |
224 | int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) |
224 | { |
225 | { |
225 | char *tail; |
226 | char *tail; |
226 | unsigned long long val; |
227 | unsigned long long val; |
227 | 228 | ||
228 | *res = 0; |
229 | *res = 0; |
229 | if (!*cp) |
230 | if (!*cp) |
230 | return -EINVAL; |
231 | return -EINVAL; |
231 | 232 | ||
232 | val = simple_strtoull(cp, &tail, base); |
233 | val = simple_strtoull(cp, &tail, base); |
233 | if (tail == cp) |
234 | if (tail == cp) |
234 | return -EINVAL; |
235 | return -EINVAL; |
235 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
236 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
236 | *res = val; |
237 | *res = val; |
237 | return 0; |
238 | return 0; |
238 | } |
239 | } |
239 | 240 | ||
240 | return -EINVAL; |
241 | return -EINVAL; |
241 | } |
242 | } |
242 | EXPORT_SYMBOL(strict_strtoull); |
243 | EXPORT_SYMBOL(strict_strtoull); |
243 | 244 | ||
244 | /** |
245 | /** |
245 | * strict_strtoll - convert a string to a long long strictly |
246 | * strict_strtoll - convert a string to a long long strictly |
246 | * @cp: The string to be converted |
247 | * @cp: The string to be converted |
247 | * @base: The number base to use |
248 | * @base: The number base to use |
248 | * @res: The converted result value |
249 | * @res: The converted result value |
249 | * |
250 | * |
250 | * strict_strtoll is similiar to strict_strtoull, but it allows the first |
251 | * strict_strtoll is similiar to strict_strtoull, but it allows the first |
251 | * character of a string is '-'. |
252 | * character of a string is '-'. |
252 | * |
253 | * |
253 | * It returns 0 if conversion is successful and *res is set to the converted |
254 | * It returns 0 if conversion is successful and *res is set to the converted |
254 | * value, otherwise it returns -EINVAL and *res is set to 0. |
255 | * value, otherwise it returns -EINVAL and *res is set to 0. |
255 | */ |
256 | */ |
256 | int strict_strtoll(const char *cp, unsigned int base, long long *res) |
257 | int strict_strtoll(const char *cp, unsigned int base, long long *res) |
257 | { |
258 | { |
258 | int ret; |
259 | int ret; |
259 | if (*cp == '-') { |
260 | if (*cp == '-') { |
260 | ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); |
261 | ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); |
261 | if (!ret) |
262 | if (!ret) |
262 | *res = -(*res); |
263 | *res = -(*res); |
263 | } else { |
264 | } else { |
264 | ret = strict_strtoull(cp, base, (unsigned long long *)res); |
265 | ret = strict_strtoull(cp, base, (unsigned long long *)res); |
265 | } |
266 | } |
266 | 267 | ||
267 | return ret; |
268 | return ret; |
268 | } |
269 | } |
269 | EXPORT_SYMBOL(strict_strtoll); |
270 | EXPORT_SYMBOL(strict_strtoll); |
270 | 271 | ||
271 | static noinline_for_stack |
272 | static noinline_for_stack |
272 | int skip_atoi(const char **s) |
273 | int skip_atoi(const char **s) |
273 | { |
274 | { |
274 | int i = 0; |
275 | int i = 0; |
275 | 276 | ||
276 | while (isdigit(**s)) |
277 | while (isdigit(**s)) |
277 | i = i*10 + *((*s)++) - '0'; |
278 | i = i*10 + *((*s)++) - '0'; |
278 | 279 | ||
279 | return i; |
280 | return i; |
280 | } |
281 | } |
281 | 282 | ||
282 | /* Decimal conversion is by far the most typical, and is used |
283 | /* Decimal conversion is by far the most typical, and is used |
283 | * for /proc and /sys data. This directly impacts e.g. top performance |
284 | * for /proc and /sys data. This directly impacts e.g. top performance |
284 | * with many processes running. We optimize it for speed |
285 | * with many processes running. We optimize it for speed |
285 | * using code from |
286 | * using code from |
286 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
287 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
287 | * (with permission from the author, Douglas W. Jones). */ |
288 | * (with permission from the author, Douglas W. Jones). */ |
288 | 289 | ||
289 | /* Formats correctly any integer in [0,99999]. |
290 | /* Formats correctly any integer in [0,99999]. |
290 | * Outputs from one to five digits depending on input. |
291 | * Outputs from one to five digits depending on input. |
291 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
292 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
292 | static noinline_for_stack |
293 | static noinline_for_stack |
293 | char *put_dec_trunc(char *buf, unsigned q) |
294 | char *put_dec_trunc(char *buf, unsigned q) |
294 | { |
295 | { |
295 | unsigned d3, d2, d1, d0; |
296 | unsigned d3, d2, d1, d0; |
296 | d1 = (q>>4) & 0xf; |
297 | d1 = (q>>4) & 0xf; |
297 | d2 = (q>>8) & 0xf; |
298 | d2 = (q>>8) & 0xf; |
298 | d3 = (q>>12); |
299 | d3 = (q>>12); |
299 | 300 | ||
300 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
301 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
301 | q = (d0 * 0xcd) >> 11; |
302 | q = (d0 * 0xcd) >> 11; |
302 | d0 = d0 - 10*q; |
303 | d0 = d0 - 10*q; |
303 | *buf++ = d0 + '0'; /* least significant digit */ |
304 | *buf++ = d0 + '0'; /* least significant digit */ |
304 | d1 = q + 9*d3 + 5*d2 + d1; |
305 | d1 = q + 9*d3 + 5*d2 + d1; |
305 | if (d1 != 0) { |
306 | if (d1 != 0) { |
306 | q = (d1 * 0xcd) >> 11; |
307 | q = (d1 * 0xcd) >> 11; |
307 | d1 = d1 - 10*q; |
308 | d1 = d1 - 10*q; |
308 | *buf++ = d1 + '0'; /* next digit */ |
309 | *buf++ = d1 + '0'; /* next digit */ |
309 | 310 | ||
310 | d2 = q + 2*d2; |
311 | d2 = q + 2*d2; |
311 | if ((d2 != 0) || (d3 != 0)) { |
312 | if ((d2 != 0) || (d3 != 0)) { |
312 | q = (d2 * 0xd) >> 7; |
313 | q = (d2 * 0xd) >> 7; |
313 | d2 = d2 - 10*q; |
314 | d2 = d2 - 10*q; |
314 | *buf++ = d2 + '0'; /* next digit */ |
315 | *buf++ = d2 + '0'; /* next digit */ |
315 | 316 | ||
316 | d3 = q + 4*d3; |
317 | d3 = q + 4*d3; |
317 | if (d3 != 0) { |
318 | if (d3 != 0) { |
318 | q = (d3 * 0xcd) >> 11; |
319 | q = (d3 * 0xcd) >> 11; |
319 | d3 = d3 - 10*q; |
320 | d3 = d3 - 10*q; |
320 | *buf++ = d3 + '0'; /* next digit */ |
321 | *buf++ = d3 + '0'; /* next digit */ |
321 | if (q != 0) |
322 | if (q != 0) |
322 | *buf++ = q + '0'; /* most sign. digit */ |
323 | *buf++ = q + '0'; /* most sign. digit */ |
323 | } |
324 | } |
324 | } |
325 | } |
325 | } |
326 | } |
326 | 327 | ||
327 | return buf; |
328 | return buf; |
328 | } |
329 | } |
329 | /* Same with if's removed. Always emits five digits */ |
330 | /* Same with if's removed. Always emits five digits */ |
330 | static noinline_for_stack |
331 | static noinline_for_stack |
331 | char *put_dec_full(char *buf, unsigned q) |
332 | char *put_dec_full(char *buf, unsigned q) |
332 | { |
333 | { |
333 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
334 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
334 | /* but anyway, gcc produces better code with full-sized ints */ |
335 | /* but anyway, gcc produces better code with full-sized ints */ |
335 | unsigned d3, d2, d1, d0; |
336 | unsigned d3, d2, d1, d0; |
336 | d1 = (q>>4) & 0xf; |
337 | d1 = (q>>4) & 0xf; |
337 | d2 = (q>>8) & 0xf; |
338 | d2 = (q>>8) & 0xf; |
338 | d3 = (q>>12); |
339 | d3 = (q>>12); |
339 | 340 | ||
340 | /* |
341 | /* |
341 | * Possible ways to approx. divide by 10 |
342 | * Possible ways to approx. divide by 10 |
342 | * gcc -O2 replaces multiply with shifts and adds |
343 | * gcc -O2 replaces multiply with shifts and adds |
343 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
344 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
344 | * (x * 0x67) >> 10: 1100111 |
345 | * (x * 0x67) >> 10: 1100111 |
345 | * (x * 0x34) >> 9: 110100 - same |
346 | * (x * 0x34) >> 9: 110100 - same |
346 | * (x * 0x1a) >> 8: 11010 - same |
347 | * (x * 0x1a) >> 8: 11010 - same |
347 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
348 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
348 | */ |
349 | */ |
349 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
350 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
350 | q = (d0 * 0xcd) >> 11; |
351 | q = (d0 * 0xcd) >> 11; |
351 | d0 = d0 - 10*q; |
352 | d0 = d0 - 10*q; |
352 | *buf++ = d0 + '0'; |
353 | *buf++ = d0 + '0'; |
353 | d1 = q + 9*d3 + 5*d2 + d1; |
354 | d1 = q + 9*d3 + 5*d2 + d1; |
354 | q = (d1 * 0xcd) >> 11; |
355 | q = (d1 * 0xcd) >> 11; |
355 | d1 = d1 - 10*q; |
356 | d1 = d1 - 10*q; |
356 | *buf++ = d1 + '0'; |
357 | *buf++ = d1 + '0'; |
357 | 358 | ||
358 | d2 = q + 2*d2; |
359 | d2 = q + 2*d2; |
359 | q = (d2 * 0xd) >> 7; |
360 | q = (d2 * 0xd) >> 7; |
360 | d2 = d2 - 10*q; |
361 | d2 = d2 - 10*q; |
361 | *buf++ = d2 + '0'; |
362 | *buf++ = d2 + '0'; |
362 | 363 | ||
363 | d3 = q + 4*d3; |
364 | d3 = q + 4*d3; |
364 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
365 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
365 | /* q = (d3 * 0x67) >> 10; - would also work */ |
366 | /* q = (d3 * 0x67) >> 10; - would also work */ |
366 | d3 = d3 - 10*q; |
367 | d3 = d3 - 10*q; |
367 | *buf++ = d3 + '0'; |
368 | *buf++ = d3 + '0'; |
368 | *buf++ = q + '0'; |
369 | *buf++ = q + '0'; |
369 | 370 | ||
370 | return buf; |
371 | return buf; |
371 | } |
372 | } |
372 | /* No inlining helps gcc to use registers better */ |
373 | /* No inlining helps gcc to use registers better */ |
373 | static noinline_for_stack |
374 | static noinline_for_stack |
374 | char *put_dec(char *buf, unsigned long long num) |
375 | char *put_dec(char *buf, unsigned long long num) |
375 | { |
376 | { |
376 | while (1) { |
377 | while (1) { |
377 | unsigned rem; |
378 | unsigned rem; |
378 | if (num < 100000) |
379 | if (num < 100000) |
379 | return put_dec_trunc(buf, num); |
380 | return put_dec_trunc(buf, num); |
380 | rem = do_div(num, 100000); |
381 | rem = do_div(num, 100000); |
381 | buf = put_dec_full(buf, rem); |
382 | buf = put_dec_full(buf, rem); |
382 | } |
383 | } |
383 | } |
384 | } |
384 | 385 | ||
385 | #define ZEROPAD 1 /* pad with zero */ |
386 | #define ZEROPAD 1 /* pad with zero */ |
386 | #define SIGN 2 /* unsigned/signed long */ |
387 | #define SIGN 2 /* unsigned/signed long */ |
387 | #define PLUS 4 /* show plus */ |
388 | #define PLUS 4 /* show plus */ |
388 | #define SPACE 8 /* space if plus */ |
389 | #define SPACE 8 /* space if plus */ |
389 | #define LEFT 16 /* left justified */ |
390 | #define LEFT 16 /* left justified */ |
390 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
391 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
391 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
392 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
392 | 393 | ||
393 | enum format_type { |
394 | enum format_type { |
394 | FORMAT_TYPE_NONE, /* Just a string part */ |
395 | FORMAT_TYPE_NONE, /* Just a string part */ |
395 | FORMAT_TYPE_WIDTH, |
396 | FORMAT_TYPE_WIDTH, |
396 | FORMAT_TYPE_PRECISION, |
397 | FORMAT_TYPE_PRECISION, |
397 | FORMAT_TYPE_CHAR, |
398 | FORMAT_TYPE_CHAR, |
398 | FORMAT_TYPE_STR, |
399 | FORMAT_TYPE_STR, |
399 | FORMAT_TYPE_PTR, |
400 | FORMAT_TYPE_PTR, |
400 | FORMAT_TYPE_PERCENT_CHAR, |
401 | FORMAT_TYPE_PERCENT_CHAR, |
401 | FORMAT_TYPE_INVALID, |
402 | FORMAT_TYPE_INVALID, |
402 | FORMAT_TYPE_LONG_LONG, |
403 | FORMAT_TYPE_LONG_LONG, |
403 | FORMAT_TYPE_ULONG, |
404 | FORMAT_TYPE_ULONG, |
404 | FORMAT_TYPE_LONG, |
405 | FORMAT_TYPE_LONG, |
405 | FORMAT_TYPE_UBYTE, |
406 | FORMAT_TYPE_UBYTE, |
406 | FORMAT_TYPE_BYTE, |
407 | FORMAT_TYPE_BYTE, |
407 | FORMAT_TYPE_USHORT, |
408 | FORMAT_TYPE_USHORT, |
408 | FORMAT_TYPE_SHORT, |
409 | FORMAT_TYPE_SHORT, |
409 | FORMAT_TYPE_UINT, |
410 | FORMAT_TYPE_UINT, |
410 | FORMAT_TYPE_INT, |
411 | FORMAT_TYPE_INT, |
411 | FORMAT_TYPE_NRCHARS, |
412 | FORMAT_TYPE_NRCHARS, |
412 | FORMAT_TYPE_SIZE_T, |
413 | FORMAT_TYPE_SIZE_T, |
413 | FORMAT_TYPE_PTRDIFF |
414 | FORMAT_TYPE_PTRDIFF |
414 | }; |
415 | }; |
415 | 416 | ||
416 | struct printf_spec { |
417 | struct printf_spec { |
417 | u8 type; /* format_type enum */ |
418 | u8 type; /* format_type enum */ |
418 | u8 flags; /* flags to number() */ |
419 | u8 flags; /* flags to number() */ |
419 | u8 base; /* number base, 8, 10 or 16 only */ |
420 | u8 base; /* number base, 8, 10 or 16 only */ |
420 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
421 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
421 | s16 field_width; /* width of output field */ |
422 | s16 field_width; /* width of output field */ |
422 | s16 precision; /* # of digits/chars */ |
423 | s16 precision; /* # of digits/chars */ |
423 | }; |
424 | }; |
424 | 425 | ||
425 | static noinline_for_stack |
426 | static noinline_for_stack |
426 | char *number(char *buf, char *end, unsigned long long num, |
427 | char *number(char *buf, char *end, unsigned long long num, |
427 | struct printf_spec spec) |
428 | struct printf_spec spec) |
428 | { |
429 | { |
429 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
430 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
430 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
431 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
431 | 432 | ||
432 | char tmp[66]; |
433 | char tmp[66]; |
433 | char sign; |
434 | char sign; |
434 | char locase; |
435 | char locase; |
435 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
436 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
436 | int i; |
437 | int i; |
437 | 438 | ||
438 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
439 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
439 | * produces same digits or (maybe lowercased) letters */ |
440 | * produces same digits or (maybe lowercased) letters */ |
440 | locase = (spec.flags & SMALL); |
441 | locase = (spec.flags & SMALL); |
441 | if (spec.flags & LEFT) |
442 | if (spec.flags & LEFT) |
442 | spec.flags &= ~ZEROPAD; |
443 | spec.flags &= ~ZEROPAD; |
443 | sign = 0; |
444 | sign = 0; |
444 | if (spec.flags & SIGN) { |
445 | if (spec.flags & SIGN) { |
445 | if ((signed long long)num < 0) { |
446 | if ((signed long long)num < 0) { |
446 | sign = '-'; |
447 | sign = '-'; |
447 | num = -(signed long long)num; |
448 | num = -(signed long long)num; |
448 | spec.field_width--; |
449 | spec.field_width--; |
449 | } else if (spec.flags & PLUS) { |
450 | } else if (spec.flags & PLUS) { |
450 | sign = '+'; |
451 | sign = '+'; |
451 | spec.field_width--; |
452 | spec.field_width--; |
452 | } else if (spec.flags & SPACE) { |
453 | } else if (spec.flags & SPACE) { |
453 | sign = ' '; |
454 | sign = ' '; |
454 | spec.field_width--; |
455 | spec.field_width--; |
455 | } |
456 | } |
456 | } |
457 | } |
457 | if (need_pfx) { |
458 | if (need_pfx) { |
458 | spec.field_width--; |
459 | spec.field_width--; |
459 | if (spec.base == 16) |
460 | if (spec.base == 16) |
460 | spec.field_width--; |
461 | spec.field_width--; |
461 | } |
462 | } |
462 | 463 | ||
463 | /* generate full string in tmp[], in reverse order */ |
464 | /* generate full string in tmp[], in reverse order */ |
464 | i = 0; |
465 | i = 0; |
465 | if (num == 0) |
466 | if (num == 0) |
466 | tmp[i++] = '0'; |
467 | tmp[i++] = '0'; |
467 | /* Generic code, for any base: |
468 | /* Generic code, for any base: |
468 | else do { |
469 | else do { |
469 | tmp[i++] = (digits[do_div(num,base)] | locase); |
470 | tmp[i++] = (digits[do_div(num,base)] | locase); |
470 | } while (num != 0); |
471 | } while (num != 0); |
471 | */ |
472 | */ |
472 | else if (spec.base != 10) { /* 8 or 16 */ |
473 | else if (spec.base != 10) { /* 8 or 16 */ |
473 | int mask = spec.base - 1; |
474 | int mask = spec.base - 1; |
474 | int shift = 3; |
475 | int shift = 3; |
475 | 476 | ||
476 | if (spec.base == 16) |
477 | if (spec.base == 16) |
477 | shift = 4; |
478 | shift = 4; |
478 | do { |
479 | do { |
479 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
480 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
480 | num >>= shift; |
481 | num >>= shift; |
481 | } while (num); |
482 | } while (num); |
482 | } else { /* base 10 */ |
483 | } else { /* base 10 */ |
483 | i = put_dec(tmp, num) - tmp; |
484 | i = put_dec(tmp, num) - tmp; |
484 | } |
485 | } |
485 | 486 | ||
486 | /* printing 100 using %2d gives "100", not "00" */ |
487 | /* printing 100 using %2d gives "100", not "00" */ |
487 | if (i > spec.precision) |
488 | if (i > spec.precision) |
488 | spec.precision = i; |
489 | spec.precision = i; |
489 | /* leading space padding */ |
490 | /* leading space padding */ |
490 | spec.field_width -= spec.precision; |
491 | spec.field_width -= spec.precision; |
491 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
492 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
492 | while (--spec.field_width >= 0) { |
493 | while (--spec.field_width >= 0) { |
493 | if (buf < end) |
494 | if (buf < end) |
494 | *buf = ' '; |
495 | *buf = ' '; |
495 | ++buf; |
496 | ++buf; |
496 | } |
497 | } |
497 | } |
498 | } |
498 | /* sign */ |
499 | /* sign */ |
499 | if (sign) { |
500 | if (sign) { |
500 | if (buf < end) |
501 | if (buf < end) |
501 | *buf = sign; |
502 | *buf = sign; |
502 | ++buf; |
503 | ++buf; |
503 | } |
504 | } |
504 | /* "0x" / "0" prefix */ |
505 | /* "0x" / "0" prefix */ |
505 | if (need_pfx) { |
506 | if (need_pfx) { |
506 | if (buf < end) |
507 | if (buf < end) |
507 | *buf = '0'; |
508 | *buf = '0'; |
508 | ++buf; |
509 | ++buf; |
509 | if (spec.base == 16) { |
510 | if (spec.base == 16) { |
510 | if (buf < end) |
511 | if (buf < end) |
511 | *buf = ('X' | locase); |
512 | *buf = ('X' | locase); |
512 | ++buf; |
513 | ++buf; |
513 | } |
514 | } |
514 | } |
515 | } |
515 | /* zero or space padding */ |
516 | /* zero or space padding */ |
516 | if (!(spec.flags & LEFT)) { |
517 | if (!(spec.flags & LEFT)) { |
517 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
518 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
518 | while (--spec.field_width >= 0) { |
519 | while (--spec.field_width >= 0) { |
519 | if (buf < end) |
520 | if (buf < end) |
520 | *buf = c; |
521 | *buf = c; |
521 | ++buf; |
522 | ++buf; |
522 | } |
523 | } |
523 | } |
524 | } |
524 | /* hmm even more zero padding? */ |
525 | /* hmm even more zero padding? */ |
525 | while (i <= --spec.precision) { |
526 | while (i <= --spec.precision) { |
526 | if (buf < end) |
527 | if (buf < end) |
527 | *buf = '0'; |
528 | *buf = '0'; |
528 | ++buf; |
529 | ++buf; |
529 | } |
530 | } |
530 | /* actual digits of result */ |
531 | /* actual digits of result */ |
531 | while (--i >= 0) { |
532 | while (--i >= 0) { |
532 | if (buf < end) |
533 | if (buf < end) |
533 | *buf = tmp[i]; |
534 | *buf = tmp[i]; |
534 | ++buf; |
535 | ++buf; |
535 | } |
536 | } |
536 | /* trailing space padding */ |
537 | /* trailing space padding */ |
537 | while (--spec.field_width >= 0) { |
538 | while (--spec.field_width >= 0) { |
538 | if (buf < end) |
539 | if (buf < end) |
539 | *buf = ' '; |
540 | *buf = ' '; |
540 | ++buf; |
541 | ++buf; |
541 | } |
542 | } |
542 | 543 | ||
543 | return buf; |
544 | return buf; |
544 | } |
545 | } |
545 | 546 | ||
546 | static noinline_for_stack |
547 | static noinline_for_stack |
547 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
548 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
548 | { |
549 | { |
549 | int len, i; |
550 | int len, i; |
550 | 551 | ||
551 | if ((unsigned long)s < PAGE_SIZE) |
552 | if ((unsigned long)s < PAGE_SIZE) |
552 | s = "(null)"; |
553 | s = "(null)"; |
553 | 554 | ||
554 | len = strnlen(s, spec.precision); |
555 | len = strnlen(s, spec.precision); |
555 | 556 | ||
556 | if (!(spec.flags & LEFT)) { |
557 | if (!(spec.flags & LEFT)) { |
557 | while (len < spec.field_width--) { |
558 | while (len < spec.field_width--) { |
558 | if (buf < end) |
559 | if (buf < end) |
559 | *buf = ' '; |
560 | *buf = ' '; |
560 | ++buf; |
561 | ++buf; |
561 | } |
562 | } |
562 | } |
563 | } |
563 | for (i = 0; i < len; ++i) { |
564 | for (i = 0; i < len; ++i) { |
564 | if (buf < end) |
565 | if (buf < end) |
565 | *buf = *s; |
566 | *buf = *s; |
566 | ++buf; ++s; |
567 | ++buf; ++s; |
567 | } |
568 | } |
568 | while (len < spec.field_width--) { |
569 | while (len < spec.field_width--) { |
569 | if (buf < end) |
570 | if (buf < end) |
570 | *buf = ' '; |
571 | *buf = ' '; |
571 | ++buf; |
572 | ++buf; |
572 | } |
573 | } |
573 | 574 | ||
574 | return buf; |
575 | return buf; |
575 | } |
576 | } |
576 | 577 | ||
577 | static noinline_for_stack |
578 | static noinline_for_stack |
578 | char *symbol_string(char *buf, char *end, void *ptr, |
579 | char *symbol_string(char *buf, char *end, void *ptr, |
579 | struct printf_spec spec, char ext) |
580 | struct printf_spec spec, char ext) |
580 | { |
581 | { |
581 | unsigned long value = (unsigned long) ptr; |
582 | unsigned long value = (unsigned long) ptr; |
582 | #ifdef CONFIG_KALLSYMS |
583 | #ifdef CONFIG_KALLSYMS |
583 | char sym[KSYM_SYMBOL_LEN]; |
584 | char sym[KSYM_SYMBOL_LEN]; |
584 | if (ext != 'f' && ext != 's') |
585 | if (ext != 'f' && ext != 's') |
585 | sprint_symbol(sym, value); |
586 | sprint_symbol(sym, value); |
586 | else |
587 | else |
587 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
588 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
588 | 589 | ||
589 | return string(buf, end, sym, spec); |
590 | return string(buf, end, sym, spec); |
590 | #else |
591 | #else |
591 | spec.field_width = 2 * sizeof(void *); |
592 | spec.field_width = 2 * sizeof(void *); |
592 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
593 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
593 | spec.base = 16; |
594 | spec.base = 16; |
594 | 595 | ||
595 | return number(buf, end, value, spec); |
596 | return number(buf, end, value, spec); |
596 | #endif |
597 | #endif |
597 | } |
598 | } |
598 | 599 | ||
599 | static noinline_for_stack |
600 | static noinline_for_stack |
600 | char *resource_string(char *buf, char *end, struct resource *res, |
601 | char *resource_string(char *buf, char *end, struct resource *res, |
601 | struct printf_spec spec, const char *fmt) |
602 | struct printf_spec spec, const char *fmt) |
602 | { |
603 | { |
603 | #ifndef IO_RSRC_PRINTK_SIZE |
604 | #ifndef IO_RSRC_PRINTK_SIZE |
604 | #define IO_RSRC_PRINTK_SIZE 6 |
605 | #define IO_RSRC_PRINTK_SIZE 6 |
605 | #endif |
606 | #endif |
606 | 607 | ||
607 | #ifndef MEM_RSRC_PRINTK_SIZE |
608 | #ifndef MEM_RSRC_PRINTK_SIZE |
608 | #define MEM_RSRC_PRINTK_SIZE 10 |
609 | #define MEM_RSRC_PRINTK_SIZE 10 |
609 | #endif |
610 | #endif |
610 | static const struct printf_spec io_spec = { |
611 | static const struct printf_spec io_spec = { |
611 | .base = 16, |
612 | .base = 16, |
612 | .field_width = IO_RSRC_PRINTK_SIZE, |
613 | .field_width = IO_RSRC_PRINTK_SIZE, |
613 | .precision = -1, |
614 | .precision = -1, |
614 | .flags = SPECIAL | SMALL | ZEROPAD, |
615 | .flags = SPECIAL | SMALL | ZEROPAD, |
615 | }; |
616 | }; |
616 | static const struct printf_spec mem_spec = { |
617 | static const struct printf_spec mem_spec = { |
617 | .base = 16, |
618 | .base = 16, |
618 | .field_width = MEM_RSRC_PRINTK_SIZE, |
619 | .field_width = MEM_RSRC_PRINTK_SIZE, |
619 | .precision = -1, |
620 | .precision = -1, |
620 | .flags = SPECIAL | SMALL | ZEROPAD, |
621 | .flags = SPECIAL | SMALL | ZEROPAD, |
621 | }; |
622 | }; |
622 | static const struct printf_spec bus_spec = { |
623 | static const struct printf_spec bus_spec = { |
623 | .base = 16, |
624 | .base = 16, |
624 | .field_width = 2, |
625 | .field_width = 2, |
625 | .precision = -1, |
626 | .precision = -1, |
626 | .flags = SMALL | ZEROPAD, |
627 | .flags = SMALL | ZEROPAD, |
627 | }; |
628 | }; |
628 | static const struct printf_spec dec_spec = { |
629 | static const struct printf_spec dec_spec = { |
629 | .base = 10, |
630 | .base = 10, |
630 | .precision = -1, |
631 | .precision = -1, |
631 | .flags = 0, |
632 | .flags = 0, |
632 | }; |
633 | }; |
633 | static const struct printf_spec str_spec = { |
634 | static const struct printf_spec str_spec = { |
634 | .field_width = -1, |
635 | .field_width = -1, |
635 | .precision = 10, |
636 | .precision = 10, |
636 | .flags = LEFT, |
637 | .flags = LEFT, |
637 | }; |
638 | }; |
638 | static const struct printf_spec flag_spec = { |
639 | static const struct printf_spec flag_spec = { |
639 | .base = 16, |
640 | .base = 16, |
640 | .precision = -1, |
641 | .precision = -1, |
641 | .flags = SPECIAL | SMALL, |
642 | .flags = SPECIAL | SMALL, |
642 | }; |
643 | }; |
643 | 644 | ||
644 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
645 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
645 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
646 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
646 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
647 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
647 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
648 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
648 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
649 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
649 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
650 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
650 | #undef max |
651 | #undef max |
651 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
652 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
652 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
653 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
653 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
654 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
654 | 655 | ||
655 | char *p = sym, *pend = sym + sizeof(sym); |
656 | char *p = sym, *pend = sym + sizeof(sym); |
656 | int decode = (fmt[0] == 'R') ? 1 : 0; |
657 | int decode = (fmt[0] == 'R') ? 1 : 0; |
657 | const struct printf_spec *specp; |
658 | const struct printf_spec *specp; |
658 | 659 | ||
659 | *p++ = '['; |
660 | *p++ = '['; |
660 | if (res->flags & IORESOURCE_IO) { |
661 | if (res->flags & IORESOURCE_IO) { |
661 | p = string(p, pend, "io ", str_spec); |
662 | p = string(p, pend, "io ", str_spec); |
662 | specp = &io_spec; |
663 | specp = &io_spec; |
663 | } else if (res->flags & IORESOURCE_MEM) { |
664 | } else if (res->flags & IORESOURCE_MEM) { |
664 | p = string(p, pend, "mem ", str_spec); |
665 | p = string(p, pend, "mem ", str_spec); |
665 | specp = &mem_spec; |
666 | specp = &mem_spec; |
666 | } else if (res->flags & IORESOURCE_IRQ) { |
667 | } else if (res->flags & IORESOURCE_IRQ) { |
667 | p = string(p, pend, "irq ", str_spec); |
668 | p = string(p, pend, "irq ", str_spec); |
668 | specp = &dec_spec; |
669 | specp = &dec_spec; |
669 | } else if (res->flags & IORESOURCE_DMA) { |
670 | } else if (res->flags & IORESOURCE_DMA) { |
670 | p = string(p, pend, "dma ", str_spec); |
671 | p = string(p, pend, "dma ", str_spec); |
671 | specp = &dec_spec; |
672 | specp = &dec_spec; |
672 | } else if (res->flags & IORESOURCE_BUS) { |
673 | } else if (res->flags & IORESOURCE_BUS) { |
673 | p = string(p, pend, "bus ", str_spec); |
674 | p = string(p, pend, "bus ", str_spec); |
674 | specp = &bus_spec; |
675 | specp = &bus_spec; |
675 | } else { |
676 | } else { |
676 | p = string(p, pend, "??? ", str_spec); |
677 | p = string(p, pend, "??? ", str_spec); |
677 | specp = &mem_spec; |
678 | specp = &mem_spec; |
678 | decode = 0; |
679 | decode = 0; |
679 | } |
680 | } |
680 | p = number(p, pend, res->start, *specp); |
681 | p = number(p, pend, res->start, *specp); |
681 | if (res->start != res->end) { |
682 | if (res->start != res->end) { |
682 | *p++ = '-'; |
683 | *p++ = '-'; |
683 | p = number(p, pend, res->end, *specp); |
684 | p = number(p, pend, res->end, *specp); |
684 | } |
685 | } |
685 | if (decode) { |
686 | if (decode) { |
686 | if (res->flags & IORESOURCE_MEM_64) |
687 | if (res->flags & IORESOURCE_MEM_64) |
687 | p = string(p, pend, " 64bit", str_spec); |
688 | p = string(p, pend, " 64bit", str_spec); |
688 | if (res->flags & IORESOURCE_PREFETCH) |
689 | if (res->flags & IORESOURCE_PREFETCH) |
689 | p = string(p, pend, " pref", str_spec); |
690 | p = string(p, pend, " pref", str_spec); |
690 | if (res->flags & IORESOURCE_WINDOW) |
691 | if (res->flags & IORESOURCE_WINDOW) |
691 | p = string(p, pend, " window", str_spec); |
692 | p = string(p, pend, " window", str_spec); |
692 | if (res->flags & IORESOURCE_DISABLED) |
693 | if (res->flags & IORESOURCE_DISABLED) |
693 | p = string(p, pend, " disabled", str_spec); |
694 | p = string(p, pend, " disabled", str_spec); |
694 | } else { |
695 | } else { |
695 | p = string(p, pend, " flags ", str_spec); |
696 | p = string(p, pend, " flags ", str_spec); |
696 | p = number(p, pend, res->flags, flag_spec); |
697 | p = number(p, pend, res->flags, flag_spec); |
697 | } |
698 | } |
698 | *p++ = ']'; |
699 | *p++ = ']'; |
699 | *p = '\0'; |
700 | *p = '\0'; |
700 | 701 | ||
701 | return string(buf, end, sym, spec); |
702 | return string(buf, end, sym, spec); |
702 | } |
703 | } |
703 | 704 | ||
704 | static noinline_for_stack |
705 | static noinline_for_stack |
705 | char *mac_address_string(char *buf, char *end, u8 *addr, |
706 | char *mac_address_string(char *buf, char *end, u8 *addr, |
706 | struct printf_spec spec, const char *fmt) |
707 | struct printf_spec spec, const char *fmt) |
707 | { |
708 | { |
708 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
709 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
709 | char *p = mac_addr; |
710 | char *p = mac_addr; |
710 | int i; |
711 | int i; |
711 | char separator; |
712 | char separator; |
712 | 713 | ||
713 | if (fmt[1] == 'F') { /* FDDI canonical format */ |
714 | if (fmt[1] == 'F') { /* FDDI canonical format */ |
714 | separator = '-'; |
715 | separator = '-'; |
715 | } else { |
716 | } else { |
716 | separator = ':'; |
717 | separator = ':'; |
717 | } |
718 | } |
718 | 719 | ||
719 | for (i = 0; i < 6; i++) { |
720 | for (i = 0; i < 6; i++) { |
720 | p = pack_hex_byte(p, addr[i]); |
721 | p = pack_hex_byte(p, addr[i]); |
721 | if (fmt[0] == 'M' && i != 5) |
722 | if (fmt[0] == 'M' && i != 5) |
722 | *p++ = separator; |
723 | *p++ = separator; |
723 | } |
724 | } |
724 | *p = '\0'; |
725 | *p = '\0'; |
725 | 726 | ||
726 | return string(buf, end, mac_addr, spec); |
727 | return string(buf, end, mac_addr, spec); |
727 | } |
728 | } |
728 | 729 | ||
729 | static noinline_for_stack |
730 | static noinline_for_stack |
730 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
731 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
731 | { |
732 | { |
732 | int i; |
733 | int i; |
733 | bool leading_zeros = (fmt[0] == 'i'); |
734 | bool leading_zeros = (fmt[0] == 'i'); |
734 | int index; |
735 | int index; |
735 | int step; |
736 | int step; |
736 | 737 | ||
737 | switch (fmt[2]) { |
738 | switch (fmt[2]) { |
738 | case 'h': |
739 | case 'h': |
739 | #ifdef __BIG_ENDIAN |
740 | #ifdef __BIG_ENDIAN |
740 | index = 0; |
741 | index = 0; |
741 | step = 1; |
742 | step = 1; |
742 | #else |
743 | #else |
743 | index = 3; |
744 | index = 3; |
744 | step = -1; |
745 | step = -1; |
745 | #endif |
746 | #endif |
746 | break; |
747 | break; |
747 | case 'l': |
748 | case 'l': |
748 | index = 3; |
749 | index = 3; |
749 | step = -1; |
750 | step = -1; |
750 | break; |
751 | break; |
751 | case 'n': |
752 | case 'n': |
752 | case 'b': |
753 | case 'b': |
753 | default: |
754 | default: |
754 | index = 0; |
755 | index = 0; |
755 | step = 1; |
756 | step = 1; |
756 | break; |
757 | break; |
757 | } |
758 | } |
758 | for (i = 0; i < 4; i++) { |
759 | for (i = 0; i < 4; i++) { |
759 | char temp[3]; /* hold each IP quad in reverse order */ |
760 | char temp[3]; /* hold each IP quad in reverse order */ |
760 | int digits = put_dec_trunc(temp, addr[index]) - temp; |
761 | int digits = put_dec_trunc(temp, addr[index]) - temp; |
761 | if (leading_zeros) { |
762 | if (leading_zeros) { |
762 | if (digits < 3) |
763 | if (digits < 3) |
763 | *p++ = '0'; |
764 | *p++ = '0'; |
764 | if (digits < 2) |
765 | if (digits < 2) |
765 | *p++ = '0'; |
766 | *p++ = '0'; |
766 | } |
767 | } |
767 | /* reverse the digits in the quad */ |
768 | /* reverse the digits in the quad */ |
768 | while (digits--) |
769 | while (digits--) |
769 | *p++ = temp[digits]; |
770 | *p++ = temp[digits]; |
770 | if (i < 3) |
771 | if (i < 3) |
771 | *p++ = '.'; |
772 | *p++ = '.'; |
772 | index += step; |
773 | index += step; |
773 | } |
774 | } |
774 | *p = '\0'; |
775 | *p = '\0'; |
775 | 776 | ||
776 | return p; |
777 | return p; |
777 | } |
778 | } |
778 | 779 | ||
779 | 780 | ||
780 | static noinline_for_stack |
781 | static noinline_for_stack |
781 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
782 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
782 | struct printf_spec spec, const char *fmt) |
783 | struct printf_spec spec, const char *fmt) |
783 | { |
784 | { |
784 | char ip4_addr[sizeof("255.255.255.255")]; |
785 | char ip4_addr[sizeof("255.255.255.255")]; |
785 | 786 | ||
786 | ip4_string(ip4_addr, addr, fmt); |
787 | ip4_string(ip4_addr, addr, fmt); |
787 | 788 | ||
788 | return string(buf, end, ip4_addr, spec); |
789 | return string(buf, end, ip4_addr, spec); |
789 | } |
790 | } |
790 | 791 | ||
791 | 792 | ||
792 | int kptr_restrict = 1; |
793 | int kptr_restrict = 1; |
793 | 794 | ||
794 | /* |
795 | /* |
795 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
796 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
796 | * by an extra set of alphanumeric characters that are extended format |
797 | * by an extra set of alphanumeric characters that are extended format |
797 | * specifiers. |
798 | * specifiers. |
798 | * |
799 | * |
799 | * Right now we handle: |
800 | * Right now we handle: |
800 | * |
801 | * |
801 | * - 'F' For symbolic function descriptor pointers with offset |
802 | * - 'F' For symbolic function descriptor pointers with offset |
802 | * - 'f' For simple symbolic function names without offset |
803 | * - 'f' For simple symbolic function names without offset |
803 | * - 'S' For symbolic direct pointers with offset |
804 | * - 'S' For symbolic direct pointers with offset |
804 | * - 's' For symbolic direct pointers without offset |
805 | * - 's' For symbolic direct pointers without offset |
805 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
806 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
806 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
807 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
807 | * - 'M' For a 6-byte MAC address, it prints the address in the |
808 | * - 'M' For a 6-byte MAC address, it prints the address in the |
808 | * usual colon-separated hex notation |
809 | * usual colon-separated hex notation |
809 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
810 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
810 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
811 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
811 | * with a dash-separated hex notation |
812 | * with a dash-separated hex notation |
812 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
813 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
813 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
814 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
814 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
815 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
815 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
816 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
816 | * IPv6 omits the colons (01020304...0f) |
817 | * IPv6 omits the colons (01020304...0f) |
817 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
818 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
818 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
819 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
819 | * - 'I6c' for IPv6 addresses printed as specified by |
820 | * - 'I6c' for IPv6 addresses printed as specified by |
820 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
821 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
821 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
822 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
822 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
823 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
823 | * Options for %pU are: |
824 | * Options for %pU are: |
824 | * b big endian lower case hex (default) |
825 | * b big endian lower case hex (default) |
825 | * B big endian UPPER case hex |
826 | * B big endian UPPER case hex |
826 | * l little endian lower case hex |
827 | * l little endian lower case hex |
827 | * L little endian UPPER case hex |
828 | * L little endian UPPER case hex |
828 | * big endian output byte order is: |
829 | * big endian output byte order is: |
829 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
830 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
830 | * little endian output byte order is: |
831 | * little endian output byte order is: |
831 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
832 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
832 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
833 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
833 | * call vsnprintf(->format, *->va_list). |
834 | * call vsnprintf(->format, *->va_list). |
834 | * Implements a "recursive vsnprintf". |
835 | * Implements a "recursive vsnprintf". |
835 | * Do not use this feature without some mechanism to verify the |
836 | * Do not use this feature without some mechanism to verify the |
836 | * correctness of the format string and va_list arguments. |
837 | * correctness of the format string and va_list arguments. |
837 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
838 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
838 | * |
839 | * |
839 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
840 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
840 | * function pointers are really function descriptors, which contain a |
841 | * function pointers are really function descriptors, which contain a |
841 | * pointer to the real address. |
842 | * pointer to the real address. |
842 | */ |
843 | */ |
843 | static noinline_for_stack |
844 | static noinline_for_stack |
844 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
845 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
845 | struct printf_spec spec) |
846 | struct printf_spec spec) |
846 | { |
847 | { |
847 | if (!ptr) { |
848 | if (!ptr) { |
848 | /* |
849 | /* |
849 | * Print (null) with the same width as a pointer so it makes |
850 | * Print (null) with the same width as a pointer so it makes |
850 | * tabular output look nice. |
851 | * tabular output look nice. |
851 | */ |
852 | */ |
852 | if (spec.field_width == -1) |
853 | if (spec.field_width == -1) |
853 | spec.field_width = 2 * sizeof(void *); |
854 | spec.field_width = 2 * sizeof(void *); |
854 | return string(buf, end, "(null)", spec); |
855 | return string(buf, end, "(null)", spec); |
855 | } |
856 | } |
856 | 857 | ||
857 | switch (*fmt) { |
858 | switch (*fmt) { |
858 | case 'F': |
859 | case 'F': |
859 | case 'f': |
860 | case 'f': |
860 | ptr = dereference_function_descriptor(ptr); |
861 | ptr = dereference_function_descriptor(ptr); |
861 | /* Fallthrough */ |
862 | /* Fallthrough */ |
862 | case 'S': |
863 | case 'S': |
863 | case 's': |
864 | case 's': |
864 | return symbol_string(buf, end, ptr, spec, *fmt); |
865 | return symbol_string(buf, end, ptr, spec, *fmt); |
865 | case 'R': |
866 | case 'R': |
866 | case 'r': |
867 | case 'r': |
867 | return resource_string(buf, end, ptr, spec, fmt); |
868 | return resource_string(buf, end, ptr, spec, fmt); |
868 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
869 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
869 | case 'm': /* Contiguous: 000102030405 */ |
870 | case 'm': /* Contiguous: 000102030405 */ |
870 | /* [mM]F (FDDI, bit reversed) */ |
871 | /* [mM]F (FDDI, bit reversed) */ |
871 | return mac_address_string(buf, end, ptr, spec, fmt); |
872 | return mac_address_string(buf, end, ptr, spec, fmt); |
872 | case 'I': /* Formatted IP supported |
873 | case 'I': /* Formatted IP supported |
873 | * 4: 1.2.3.4 |
874 | * 4: 1.2.3.4 |
874 | * 6: 0001:0203:...:0708 |
875 | * 6: 0001:0203:...:0708 |
875 | * 6c: 1::708 or 1::1.2.3.4 |
876 | * 6c: 1::708 or 1::1.2.3.4 |
876 | */ |
877 | */ |
877 | case 'i': /* Contiguous: |
878 | case 'i': /* Contiguous: |
878 | * 4: 001.002.003.004 |
879 | * 4: 001.002.003.004 |
879 | * 6: 000102...0f |
880 | * 6: 000102...0f |
880 | */ |
881 | */ |
881 | switch (fmt[1]) { |
882 | switch (fmt[1]) { |
882 | case '4': |
883 | case '4': |
883 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
884 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
884 | } |
885 | } |
885 | break; |
886 | break; |
886 | case 'V': |
887 | case 'V': |
887 | return buf + vsnprintf(buf, end - buf, |
888 | return buf + vsnprintf(buf, end - buf, |
888 | ((struct va_format *)ptr)->fmt, |
889 | ((struct va_format *)ptr)->fmt, |
889 | *(((struct va_format *)ptr)->va)); |
890 | *(((struct va_format *)ptr)->va)); |
890 | } |
891 | } |
891 | spec.flags |= SMALL; |
892 | spec.flags |= SMALL; |
892 | if (spec.field_width == -1) { |
893 | if (spec.field_width == -1) { |
893 | spec.field_width = 2 * sizeof(void *); |
894 | spec.field_width = 2 * sizeof(void *); |
894 | spec.flags |= ZEROPAD; |
895 | spec.flags |= ZEROPAD; |
895 | } |
896 | } |
896 | spec.base = 16; |
897 | spec.base = 16; |
897 | 898 | ||
898 | return number(buf, end, (unsigned long) ptr, spec); |
899 | return number(buf, end, (unsigned long) ptr, spec); |
899 | } |
900 | } |
900 | 901 | ||
901 | /* |
902 | /* |
902 | * Helper function to decode printf style format. |
903 | * Helper function to decode printf style format. |
903 | * Each call decode a token from the format and return the |
904 | * Each call decode a token from the format and return the |
904 | * number of characters read (or likely the delta where it wants |
905 | * number of characters read (or likely the delta where it wants |
905 | * to go on the next call). |
906 | * to go on the next call). |
906 | * The decoded token is returned through the parameters |
907 | * The decoded token is returned through the parameters |
907 | * |
908 | * |
908 | * 'h', 'l', or 'L' for integer fields |
909 | * 'h', 'l', or 'L' for integer fields |
909 | * 'z' support added 23/7/1999 S.H. |
910 | * 'z' support added 23/7/1999 S.H. |
910 | * 'z' changed to 'Z' --davidm 1/25/99 |
911 | * 'z' changed to 'Z' --davidm 1/25/99 |
911 | * 't' added for ptrdiff_t |
912 | * 't' added for ptrdiff_t |
912 | * |
913 | * |
913 | * @fmt: the format string |
914 | * @fmt: the format string |
914 | * @type of the token returned |
915 | * @type of the token returned |
915 | * @flags: various flags such as +, -, # tokens.. |
916 | * @flags: various flags such as +, -, # tokens.. |
916 | * @field_width: overwritten width |
917 | * @field_width: overwritten width |
917 | * @base: base of the number (octal, hex, ...) |
918 | * @base: base of the number (octal, hex, ...) |
918 | * @precision: precision of a number |
919 | * @precision: precision of a number |
919 | * @qualifier: qualifier of a number (long, size_t, ...) |
920 | * @qualifier: qualifier of a number (long, size_t, ...) |
920 | */ |
921 | */ |
921 | static noinline_for_stack |
922 | static noinline_for_stack |
922 | int format_decode(const char *fmt, struct printf_spec *spec) |
923 | int format_decode(const char *fmt, struct printf_spec *spec) |
923 | { |
924 | { |
924 | const char *start = fmt; |
925 | const char *start = fmt; |
925 | 926 | ||
926 | /* we finished early by reading the field width */ |
927 | /* we finished early by reading the field width */ |
927 | if (spec->type == FORMAT_TYPE_WIDTH) { |
928 | if (spec->type == FORMAT_TYPE_WIDTH) { |
928 | if (spec->field_width < 0) { |
929 | if (spec->field_width < 0) { |
929 | spec->field_width = -spec->field_width; |
930 | spec->field_width = -spec->field_width; |
930 | spec->flags |= LEFT; |
931 | spec->flags |= LEFT; |
931 | } |
932 | } |
932 | spec->type = FORMAT_TYPE_NONE; |
933 | spec->type = FORMAT_TYPE_NONE; |
933 | goto precision; |
934 | goto precision; |
934 | } |
935 | } |
935 | 936 | ||
936 | /* we finished early by reading the precision */ |
937 | /* we finished early by reading the precision */ |
937 | if (spec->type == FORMAT_TYPE_PRECISION) { |
938 | if (spec->type == FORMAT_TYPE_PRECISION) { |
938 | if (spec->precision < 0) |
939 | if (spec->precision < 0) |
939 | spec->precision = 0; |
940 | spec->precision = 0; |
940 | 941 | ||
941 | spec->type = FORMAT_TYPE_NONE; |
942 | spec->type = FORMAT_TYPE_NONE; |
942 | goto qualifier; |
943 | goto qualifier; |
943 | } |
944 | } |
944 | 945 | ||
945 | /* By default */ |
946 | /* By default */ |
946 | spec->type = FORMAT_TYPE_NONE; |
947 | spec->type = FORMAT_TYPE_NONE; |
947 | 948 | ||
948 | for (; *fmt ; ++fmt) { |
949 | for (; *fmt ; ++fmt) { |
949 | if (*fmt == '%') |
950 | if (*fmt == '%') |
950 | break; |
951 | break; |
951 | } |
952 | } |
952 | 953 | ||
953 | /* Return the current non-format string */ |
954 | /* Return the current non-format string */ |
954 | if (fmt != start || !*fmt) |
955 | if (fmt != start || !*fmt) |
955 | return fmt - start; |
956 | return fmt - start; |
956 | 957 | ||
957 | /* Process flags */ |
958 | /* Process flags */ |
958 | spec->flags = 0; |
959 | spec->flags = 0; |
959 | 960 | ||
960 | while (1) { /* this also skips first '%' */ |
961 | while (1) { /* this also skips first '%' */ |
961 | bool found = true; |
962 | bool found = true; |
962 | 963 | ||
963 | ++fmt; |
964 | ++fmt; |
964 | 965 | ||
965 | switch (*fmt) { |
966 | switch (*fmt) { |
966 | case '-': spec->flags |= LEFT; break; |
967 | case '-': spec->flags |= LEFT; break; |
967 | case '+': spec->flags |= PLUS; break; |
968 | case '+': spec->flags |= PLUS; break; |
968 | case ' ': spec->flags |= SPACE; break; |
969 | case ' ': spec->flags |= SPACE; break; |
969 | case '#': spec->flags |= SPECIAL; break; |
970 | case '#': spec->flags |= SPECIAL; break; |
970 | case '0': spec->flags |= ZEROPAD; break; |
971 | case '0': spec->flags |= ZEROPAD; break; |
971 | default: found = false; |
972 | default: found = false; |
972 | } |
973 | } |
973 | 974 | ||
974 | if (!found) |
975 | if (!found) |
975 | break; |
976 | break; |
976 | } |
977 | } |
977 | 978 | ||
978 | /* get field width */ |
979 | /* get field width */ |
979 | spec->field_width = -1; |
980 | spec->field_width = -1; |
980 | 981 | ||
981 | if (isdigit(*fmt)) |
982 | if (isdigit(*fmt)) |
982 | spec->field_width = skip_atoi(&fmt); |
983 | spec->field_width = skip_atoi(&fmt); |
983 | else if (*fmt == '*') { |
984 | else if (*fmt == '*') { |
984 | /* it's the next argument */ |
985 | /* it's the next argument */ |
985 | spec->type = FORMAT_TYPE_WIDTH; |
986 | spec->type = FORMAT_TYPE_WIDTH; |
986 | return ++fmt - start; |
987 | return ++fmt - start; |
987 | } |
988 | } |
988 | 989 | ||
989 | precision: |
990 | precision: |
990 | /* get the precision */ |
991 | /* get the precision */ |
991 | spec->precision = -1; |
992 | spec->precision = -1; |
992 | if (*fmt == '.') { |
993 | if (*fmt == '.') { |
993 | ++fmt; |
994 | ++fmt; |
994 | if (isdigit(*fmt)) { |
995 | if (isdigit(*fmt)) { |
995 | spec->precision = skip_atoi(&fmt); |
996 | spec->precision = skip_atoi(&fmt); |
996 | if (spec->precision < 0) |
997 | if (spec->precision < 0) |
997 | spec->precision = 0; |
998 | spec->precision = 0; |
998 | } else if (*fmt == '*') { |
999 | } else if (*fmt == '*') { |
999 | /* it's the next argument */ |
1000 | /* it's the next argument */ |
1000 | spec->type = FORMAT_TYPE_PRECISION; |
1001 | spec->type = FORMAT_TYPE_PRECISION; |
1001 | return ++fmt - start; |
1002 | return ++fmt - start; |
1002 | } |
1003 | } |
1003 | } |
1004 | } |
1004 | 1005 | ||
1005 | qualifier: |
1006 | qualifier: |
1006 | /* get the conversion qualifier */ |
1007 | /* get the conversion qualifier */ |
1007 | spec->qualifier = -1; |
1008 | spec->qualifier = -1; |
1008 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1009 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1009 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
1010 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
1010 | spec->qualifier = *fmt++; |
1011 | spec->qualifier = *fmt++; |
1011 | if (unlikely(spec->qualifier == *fmt)) { |
1012 | if (unlikely(spec->qualifier == *fmt)) { |
1012 | if (spec->qualifier == 'l') { |
1013 | if (spec->qualifier == 'l') { |
1013 | spec->qualifier = 'L'; |
1014 | spec->qualifier = 'L'; |
1014 | ++fmt; |
1015 | ++fmt; |
1015 | } else if (spec->qualifier == 'h') { |
1016 | } else if (spec->qualifier == 'h') { |
1016 | spec->qualifier = 'H'; |
1017 | spec->qualifier = 'H'; |
1017 | ++fmt; |
1018 | ++fmt; |
1018 | } |
1019 | } |
1019 | } |
1020 | } |
1020 | } |
1021 | } |
1021 | 1022 | ||
1022 | /* default base */ |
1023 | /* default base */ |
1023 | spec->base = 10; |
1024 | spec->base = 10; |
1024 | switch (*fmt) { |
1025 | switch (*fmt) { |
1025 | case 'c': |
1026 | case 'c': |
1026 | spec->type = FORMAT_TYPE_CHAR; |
1027 | spec->type = FORMAT_TYPE_CHAR; |
1027 | return ++fmt - start; |
1028 | return ++fmt - start; |
1028 | 1029 | ||
1029 | case 's': |
1030 | case 's': |
1030 | spec->type = FORMAT_TYPE_STR; |
1031 | spec->type = FORMAT_TYPE_STR; |
1031 | return ++fmt - start; |
1032 | return ++fmt - start; |
1032 | 1033 | ||
1033 | case 'p': |
1034 | case 'p': |
1034 | spec->type = FORMAT_TYPE_PTR; |
1035 | spec->type = FORMAT_TYPE_PTR; |
1035 | return fmt - start; |
1036 | return fmt - start; |
1036 | /* skip alnum */ |
1037 | /* skip alnum */ |
1037 | 1038 | ||
1038 | case 'n': |
1039 | case 'n': |
1039 | spec->type = FORMAT_TYPE_NRCHARS; |
1040 | spec->type = FORMAT_TYPE_NRCHARS; |
1040 | return ++fmt - start; |
1041 | return ++fmt - start; |
1041 | 1042 | ||
1042 | case '%': |
1043 | case '%': |
1043 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
1044 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
1044 | return ++fmt - start; |
1045 | return ++fmt - start; |
1045 | 1046 | ||
1046 | /* integer number formats - set up the flags and "break" */ |
1047 | /* integer number formats - set up the flags and "break" */ |
1047 | case 'o': |
1048 | case 'o': |
1048 | spec->base = 8; |
1049 | spec->base = 8; |
1049 | break; |
1050 | break; |
1050 | 1051 | ||
1051 | case 'x': |
1052 | case 'x': |
1052 | spec->flags |= SMALL; |
1053 | spec->flags |= SMALL; |
1053 | 1054 | ||
1054 | case 'X': |
1055 | case 'X': |
1055 | spec->base = 16; |
1056 | spec->base = 16; |
1056 | break; |
1057 | break; |
1057 | 1058 | ||
1058 | case 'd': |
1059 | case 'd': |
1059 | case 'i': |
1060 | case 'i': |
1060 | spec->flags |= SIGN; |
1061 | spec->flags |= SIGN; |
1061 | case 'u': |
1062 | case 'u': |
1062 | break; |
1063 | break; |
1063 | 1064 | ||
1064 | default: |
1065 | default: |
1065 | spec->type = FORMAT_TYPE_INVALID; |
1066 | spec->type = FORMAT_TYPE_INVALID; |
1066 | return fmt - start; |
1067 | return fmt - start; |
1067 | } |
1068 | } |
1068 | 1069 | ||
1069 | if (spec->qualifier == 'L') |
1070 | if (spec->qualifier == 'L') |
1070 | spec->type = FORMAT_TYPE_LONG_LONG; |
1071 | spec->type = FORMAT_TYPE_LONG_LONG; |
1071 | else if (spec->qualifier == 'l') { |
1072 | else if (spec->qualifier == 'l') { |
1072 | if (spec->flags & SIGN) |
1073 | if (spec->flags & SIGN) |
1073 | spec->type = FORMAT_TYPE_LONG; |
1074 | spec->type = FORMAT_TYPE_LONG; |
1074 | else |
1075 | else |
1075 | spec->type = FORMAT_TYPE_ULONG; |
1076 | spec->type = FORMAT_TYPE_ULONG; |
1076 | } else if (TOLOWER(spec->qualifier) == 'z') { |
1077 | } else if (TOLOWER(spec->qualifier) == 'z') { |
1077 | spec->type = FORMAT_TYPE_SIZE_T; |
1078 | spec->type = FORMAT_TYPE_SIZE_T; |
1078 | } else if (spec->qualifier == 't') { |
1079 | } else if (spec->qualifier == 't') { |
1079 | spec->type = FORMAT_TYPE_PTRDIFF; |
1080 | spec->type = FORMAT_TYPE_PTRDIFF; |
1080 | } else if (spec->qualifier == 'H') { |
1081 | } else if (spec->qualifier == 'H') { |
1081 | if (spec->flags & SIGN) |
1082 | if (spec->flags & SIGN) |
1082 | spec->type = FORMAT_TYPE_BYTE; |
1083 | spec->type = FORMAT_TYPE_BYTE; |
1083 | else |
1084 | else |
1084 | spec->type = FORMAT_TYPE_UBYTE; |
1085 | spec->type = FORMAT_TYPE_UBYTE; |
1085 | } else if (spec->qualifier == 'h') { |
1086 | } else if (spec->qualifier == 'h') { |
1086 | if (spec->flags & SIGN) |
1087 | if (spec->flags & SIGN) |
1087 | spec->type = FORMAT_TYPE_SHORT; |
1088 | spec->type = FORMAT_TYPE_SHORT; |
1088 | else |
1089 | else |
1089 | spec->type = FORMAT_TYPE_USHORT; |
1090 | spec->type = FORMAT_TYPE_USHORT; |
1090 | } else { |
1091 | } else { |
1091 | if (spec->flags & SIGN) |
1092 | if (spec->flags & SIGN) |
1092 | spec->type = FORMAT_TYPE_INT; |
1093 | spec->type = FORMAT_TYPE_INT; |
1093 | else |
1094 | else |
1094 | spec->type = FORMAT_TYPE_UINT; |
1095 | spec->type = FORMAT_TYPE_UINT; |
1095 | } |
1096 | } |
1096 | 1097 | ||
1097 | return ++fmt - start; |
1098 | return ++fmt - start; |
1098 | } |
1099 | } |
1099 | 1100 | ||
1100 | /** |
1101 | /** |
1101 | * vsnprintf - Format a string and place it in a buffer |
1102 | * vsnprintf - Format a string and place it in a buffer |
1102 | * @buf: The buffer to place the result into |
1103 | * @buf: The buffer to place the result into |
1103 | * @size: The size of the buffer, including the trailing null space |
1104 | * @size: The size of the buffer, including the trailing null space |
1104 | * @fmt: The format string to use |
1105 | * @fmt: The format string to use |
1105 | * @args: Arguments for the format string |
1106 | * @args: Arguments for the format string |
1106 | * |
1107 | * |
1107 | * This function follows C99 vsnprintf, but has some extensions: |
1108 | * This function follows C99 vsnprintf, but has some extensions: |
1108 | * %pS output the name of a text symbol with offset |
1109 | * %pS output the name of a text symbol with offset |
1109 | * %ps output the name of a text symbol without offset |
1110 | * %ps output the name of a text symbol without offset |
1110 | * %pF output the name of a function pointer with its offset |
1111 | * %pF output the name of a function pointer with its offset |
1111 | * %pf output the name of a function pointer without its offset |
1112 | * %pf output the name of a function pointer without its offset |
1112 | * %pR output the address range in a struct resource with decoded flags |
1113 | * %pR output the address range in a struct resource with decoded flags |
1113 | * %pr output the address range in a struct resource with raw flags |
1114 | * %pr output the address range in a struct resource with raw flags |
1114 | * %pM output a 6-byte MAC address with colons |
1115 | * %pM output a 6-byte MAC address with colons |
1115 | * %pm output a 6-byte MAC address without colons |
1116 | * %pm output a 6-byte MAC address without colons |
1116 | * %pI4 print an IPv4 address without leading zeros |
1117 | * %pI4 print an IPv4 address without leading zeros |
1117 | * %pi4 print an IPv4 address with leading zeros |
1118 | * %pi4 print an IPv4 address with leading zeros |
1118 | * %pI6 print an IPv6 address with colons |
1119 | * %pI6 print an IPv6 address with colons |
1119 | * %pi6 print an IPv6 address without colons |
1120 | * %pi6 print an IPv6 address without colons |
1120 | * %pI6c print an IPv6 address as specified by |
1121 | * %pI6c print an IPv6 address as specified by |
1121 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
1122 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
1122 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1123 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1123 | * case. |
1124 | * case. |
1124 | * %n is ignored |
1125 | * %n is ignored |
1125 | * |
1126 | * |
1126 | * The return value is the number of characters which would |
1127 | * The return value is the number of characters which would |
1127 | * be generated for the given input, excluding the trailing |
1128 | * be generated for the given input, excluding the trailing |
1128 | * '\0', as per ISO C99. If you want to have the exact |
1129 | * '\0', as per ISO C99. If you want to have the exact |
1129 | * number of characters written into @buf as return value |
1130 | * number of characters written into @buf as return value |
1130 | * (not including the trailing '\0'), use vscnprintf(). If the |
1131 | * (not including the trailing '\0'), use vscnprintf(). If the |
1131 | * return is greater than or equal to @size, the resulting |
1132 | * return is greater than or equal to @size, the resulting |
1132 | * string is truncated. |
1133 | * string is truncated. |
1133 | * |
1134 | * |
1134 | * Call this function if you are already dealing with a va_list. |
1135 | * Call this function if you are already dealing with a va_list. |
1135 | * You probably want snprintf() instead. |
1136 | * You probably want snprintf() instead. |
1136 | */ |
1137 | */ |
1137 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
1138 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
1138 | { |
1139 | { |
1139 | unsigned long long num; |
1140 | unsigned long long num; |
1140 | char *str, *end; |
1141 | char *str, *end; |
1141 | struct printf_spec spec = {0}; |
1142 | struct printf_spec spec = {0}; |
1142 | 1143 | ||
1143 | /* Reject out-of-range values early. Large positive sizes are |
1144 | /* Reject out-of-range values early. Large positive sizes are |
1144 | used for unknown buffer sizes. */ |
1145 | used for unknown buffer sizes. */ |
1145 | if ((int)size < 0) |
1146 | if ((int)size < 0) |
1146 | return 0; |
1147 | return 0; |
1147 | 1148 | ||
1148 | str = buf; |
1149 | str = buf; |
1149 | end = buf + size; |
1150 | end = buf + size; |
1150 | 1151 | ||
1151 | /* Make sure end is always >= buf */ |
1152 | /* Make sure end is always >= buf */ |
1152 | if (end < buf) { |
1153 | if (end < buf) { |
1153 | end = ((void *)-1); |
1154 | end = ((void *)-1); |
1154 | size = end - buf; |
1155 | size = end - buf; |
1155 | } |
1156 | } |
1156 | 1157 | ||
1157 | while (*fmt) { |
1158 | while (*fmt) { |
1158 | const char *old_fmt = fmt; |
1159 | const char *old_fmt = fmt; |
1159 | int read = format_decode(fmt, &spec); |
1160 | int read = format_decode(fmt, &spec); |
1160 | 1161 | ||
1161 | fmt += read; |
1162 | fmt += read; |
1162 | 1163 | ||
1163 | switch (spec.type) { |
1164 | switch (spec.type) { |
1164 | case FORMAT_TYPE_NONE: { |
1165 | case FORMAT_TYPE_NONE: { |
1165 | int copy = read; |
1166 | int copy = read; |
1166 | if (str < end) { |
1167 | if (str < end) { |
1167 | if (copy > end - str) |
1168 | if (copy > end - str) |
1168 | copy = end - str; |
1169 | copy = end - str; |
1169 | memcpy(str, old_fmt, copy); |
1170 | memcpy(str, old_fmt, copy); |
1170 | } |
1171 | } |
1171 | str += read; |
1172 | str += read; |
1172 | break; |
1173 | break; |
1173 | } |
1174 | } |
1174 | 1175 | ||
1175 | case FORMAT_TYPE_WIDTH: |
1176 | case FORMAT_TYPE_WIDTH: |
1176 | spec.field_width = va_arg(args, int); |
1177 | spec.field_width = va_arg(args, int); |
1177 | break; |
1178 | break; |
1178 | 1179 | ||
1179 | case FORMAT_TYPE_PRECISION: |
1180 | case FORMAT_TYPE_PRECISION: |
1180 | spec.precision = va_arg(args, int); |
1181 | spec.precision = va_arg(args, int); |
1181 | break; |
1182 | break; |
1182 | 1183 | ||
1183 | case FORMAT_TYPE_CHAR: { |
1184 | case FORMAT_TYPE_CHAR: { |
1184 | char c; |
1185 | char c; |
1185 | 1186 | ||
1186 | if (!(spec.flags & LEFT)) { |
1187 | if (!(spec.flags & LEFT)) { |
1187 | while (--spec.field_width > 0) { |
1188 | while (--spec.field_width > 0) { |
1188 | if (str < end) |
1189 | if (str < end) |
1189 | *str = ' '; |
1190 | *str = ' '; |
1190 | ++str; |
1191 | ++str; |
1191 | 1192 | ||
1192 | } |
1193 | } |
1193 | } |
1194 | } |
1194 | c = (unsigned char) va_arg(args, int); |
1195 | c = (unsigned char) va_arg(args, int); |
1195 | if (str < end) |
1196 | if (str < end) |
1196 | *str = c; |
1197 | *str = c; |
1197 | ++str; |
1198 | ++str; |
1198 | while (--spec.field_width > 0) { |
1199 | while (--spec.field_width > 0) { |
1199 | if (str < end) |
1200 | if (str < end) |
1200 | *str = ' '; |
1201 | *str = ' '; |
1201 | ++str; |
1202 | ++str; |
1202 | } |
1203 | } |
1203 | break; |
1204 | break; |
1204 | } |
1205 | } |
1205 | 1206 | ||
1206 | case FORMAT_TYPE_STR: |
1207 | case FORMAT_TYPE_STR: |
1207 | str = string(str, end, va_arg(args, char *), spec); |
1208 | str = string(str, end, va_arg(args, char *), spec); |
1208 | break; |
1209 | break; |
1209 | 1210 | ||
1210 | case FORMAT_TYPE_PTR: |
1211 | case FORMAT_TYPE_PTR: |
1211 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
1212 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
1212 | spec); |
1213 | spec); |
1213 | while (isalnum(*fmt)) |
1214 | while (isalnum(*fmt)) |
1214 | fmt++; |
1215 | fmt++; |
1215 | break; |
1216 | break; |
1216 | 1217 | ||
1217 | case FORMAT_TYPE_PERCENT_CHAR: |
1218 | case FORMAT_TYPE_PERCENT_CHAR: |
1218 | if (str < end) |
1219 | if (str < end) |
1219 | *str = '%'; |
1220 | *str = '%'; |
1220 | ++str; |
1221 | ++str; |
1221 | break; |
1222 | break; |
1222 | 1223 | ||
1223 | case FORMAT_TYPE_INVALID: |
1224 | case FORMAT_TYPE_INVALID: |
1224 | if (str < end) |
1225 | if (str < end) |
1225 | *str = '%'; |
1226 | *str = '%'; |
1226 | ++str; |
1227 | ++str; |
1227 | break; |
1228 | break; |
1228 | 1229 | ||
1229 | case FORMAT_TYPE_NRCHARS: { |
1230 | case FORMAT_TYPE_NRCHARS: { |
1230 | u8 qualifier = spec.qualifier; |
1231 | u8 qualifier = spec.qualifier; |
1231 | 1232 | ||
1232 | if (qualifier == 'l') { |
1233 | if (qualifier == 'l') { |
1233 | long *ip = va_arg(args, long *); |
1234 | long *ip = va_arg(args, long *); |
1234 | *ip = (str - buf); |
1235 | *ip = (str - buf); |
1235 | } else if (TOLOWER(qualifier) == 'z') { |
1236 | } else if (TOLOWER(qualifier) == 'z') { |
1236 | size_t *ip = va_arg(args, size_t *); |
1237 | size_t *ip = va_arg(args, size_t *); |
1237 | *ip = (str - buf); |
1238 | *ip = (str - buf); |
1238 | } else { |
1239 | } else { |
1239 | int *ip = va_arg(args, int *); |
1240 | int *ip = va_arg(args, int *); |
1240 | *ip = (str - buf); |
1241 | *ip = (str - buf); |
1241 | } |
1242 | } |
1242 | break; |
1243 | break; |
1243 | } |
1244 | } |
1244 | 1245 | ||
1245 | default: |
1246 | default: |
1246 | switch (spec.type) { |
1247 | switch (spec.type) { |
1247 | case FORMAT_TYPE_LONG_LONG: |
1248 | case FORMAT_TYPE_LONG_LONG: |
1248 | num = va_arg(args, long long); |
1249 | num = va_arg(args, long long); |
1249 | break; |
1250 | break; |
1250 | case FORMAT_TYPE_ULONG: |
1251 | case FORMAT_TYPE_ULONG: |
1251 | num = va_arg(args, unsigned long); |
1252 | num = va_arg(args, unsigned long); |
1252 | break; |
1253 | break; |
1253 | case FORMAT_TYPE_LONG: |
1254 | case FORMAT_TYPE_LONG: |
1254 | num = va_arg(args, long); |
1255 | num = va_arg(args, long); |
1255 | break; |
1256 | break; |
1256 | case FORMAT_TYPE_SIZE_T: |
1257 | case FORMAT_TYPE_SIZE_T: |
1257 | num = va_arg(args, size_t); |
1258 | num = va_arg(args, size_t); |
1258 | break; |
1259 | break; |
1259 | case FORMAT_TYPE_PTRDIFF: |
1260 | case FORMAT_TYPE_PTRDIFF: |
1260 | num = va_arg(args, ptrdiff_t); |
1261 | num = va_arg(args, ptrdiff_t); |
1261 | break; |
1262 | break; |
1262 | case FORMAT_TYPE_UBYTE: |
1263 | case FORMAT_TYPE_UBYTE: |
1263 | num = (unsigned char) va_arg(args, int); |
1264 | num = (unsigned char) va_arg(args, int); |
1264 | break; |
1265 | break; |
1265 | case FORMAT_TYPE_BYTE: |
1266 | case FORMAT_TYPE_BYTE: |
1266 | num = (signed char) va_arg(args, int); |
1267 | num = (signed char) va_arg(args, int); |
1267 | break; |
1268 | break; |
1268 | case FORMAT_TYPE_USHORT: |
1269 | case FORMAT_TYPE_USHORT: |
1269 | num = (unsigned short) va_arg(args, int); |
1270 | num = (unsigned short) va_arg(args, int); |
1270 | break; |
1271 | break; |
1271 | case FORMAT_TYPE_SHORT: |
1272 | case FORMAT_TYPE_SHORT: |
1272 | num = (short) va_arg(args, int); |
1273 | num = (short) va_arg(args, int); |
1273 | break; |
1274 | break; |
1274 | case FORMAT_TYPE_INT: |
1275 | case FORMAT_TYPE_INT: |
1275 | num = (int) va_arg(args, int); |
1276 | num = (int) va_arg(args, int); |
1276 | break; |
1277 | break; |
1277 | default: |
1278 | default: |
1278 | num = va_arg(args, unsigned int); |
1279 | num = va_arg(args, unsigned int); |
1279 | } |
1280 | } |
1280 | 1281 | ||
1281 | str = number(str, end, num, spec); |
1282 | str = number(str, end, num, spec); |
1282 | } |
1283 | } |
1283 | } |
1284 | } |
1284 | 1285 | ||
1285 | if (size > 0) { |
1286 | if (size > 0) { |
1286 | if (str < end) |
1287 | if (str < end) |
1287 | *str = '\0'; |
1288 | *str = '\0'; |
1288 | else |
1289 | else |
1289 | end[-1] = '\0'; |
1290 | end[-1] = '\0'; |
1290 | } |
1291 | } |
1291 | 1292 | ||
1292 | /* the trailing null byte doesn't count towards the total */ |
1293 | /* the trailing null byte doesn't count towards the total */ |
1293 | return str-buf; |
1294 | return str-buf; |
1294 | 1295 | ||
1295 | } |
1296 | } |
1296 | EXPORT_SYMBOL(vsnprintf); |
1297 | EXPORT_SYMBOL(vsnprintf); |
1297 | 1298 | ||
1298 | int vsprintf(char *buf, const char *fmt, va_list args) |
1299 | int vsprintf(char *buf, const char *fmt, va_list args) |
1299 | { |
1300 | { |
1300 | return vsnprintf(buf, INT_MAX, fmt, args); |
1301 | return vsnprintf(buf, INT_MAX, fmt, args); |
1301 | } |
1302 | } |
1302 | 1303 | ||
1303 | 1304 | ||
1304 | /** |
1305 | /** |
1305 | * snprintf - Format a string and place it in a buffer |
1306 | * snprintf - Format a string and place it in a buffer |
1306 | * @buf: The buffer to place the result into |
1307 | * @buf: The buffer to place the result into |
1307 | * @size: The size of the buffer, including the trailing null space |
1308 | * @size: The size of the buffer, including the trailing null space |
1308 | * @fmt: The format string to use |
1309 | * @fmt: The format string to use |
1309 | * @...: Arguments for the format string |
1310 | * @...: Arguments for the format string |
1310 | * |
1311 | * |
1311 | * The return value is the number of characters which would be |
1312 | * The return value is the number of characters which would be |
1312 | * generated for the given input, excluding the trailing null, |
1313 | * generated for the given input, excluding the trailing null, |
1313 | * as per ISO C99. If the return is greater than or equal to |
1314 | * as per ISO C99. If the return is greater than or equal to |
1314 | * @size, the resulting string is truncated. |
1315 | * @size, the resulting string is truncated. |
1315 | * |
1316 | * |
1316 | * See the vsnprintf() documentation for format string extensions over C99. |
1317 | * See the vsnprintf() documentation for format string extensions over C99. |
1317 | */ |
1318 | */ |
1318 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1319 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1319 | { |
1320 | { |
1320 | va_list args; |
1321 | va_list args; |
1321 | int i; |
1322 | int i; |
1322 | 1323 | ||
1323 | va_start(args, fmt); |
1324 | va_start(args, fmt); |
1324 | i = vsnprintf(buf, size, fmt, args); |
1325 | i = vsnprintf(buf, size, fmt, args); |
1325 | va_end(args); |
1326 | va_end(args); |
1326 | 1327 | ||
1327 | return i; |
1328 | return i; |
1328 | } |
1329 | } |
1329 | EXPORT_SYMBOL(snprintf); |
1330 | EXPORT_SYMBOL(snprintf); |
1330 | 1331 | ||
1331 | 1332 | ||
1332 | /** |
1333 | /** |
1333 | * sprintf - Format a string and place it in a buffer |
1334 | * sprintf - Format a string and place it in a buffer |
1334 | * @buf: The buffer to place the result into |
1335 | * @buf: The buffer to place the result into |
1335 | * @fmt: The format string to use |
1336 | * @fmt: The format string to use |
1336 | * @...: Arguments for the format string |
1337 | * @...: Arguments for the format string |
1337 | * |
1338 | * |
1338 | * The function returns the number of characters written |
1339 | * The function returns the number of characters written |
1339 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
1340 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
1340 | * buffer overflows. |
1341 | * buffer overflows. |
1341 | * |
1342 | * |
1342 | * See the vsnprintf() documentation for format string extensions over C99. |
1343 | * See the vsnprintf() documentation for format string extensions over C99. |
1343 | */ |
1344 | */ |
1344 | int sprintf(char *buf, const char *fmt, ...) |
1345 | int sprintf(char *buf, const char *fmt, ...) |
1345 | { |
1346 | { |
1346 | va_list args; |
1347 | va_list args; |
1347 | int i; |
1348 | int i; |
1348 | 1349 | ||
1349 | va_start(args, fmt); |
1350 | va_start(args, fmt); |
1350 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1351 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1351 | va_end(args); |
1352 | va_end(args); |
1352 | 1353 | ||
1353 | return i; |
1354 | return i; |
1354 | }>>>>>>>>>>>>>>>>>>>>>>>>>>>=>>>>>>>> |
1355 | }>>>>>>>>>>>>>>>>>>>>>>>>>>>=>>>>>>>> |