blob: 5d6f0718b6d9983bfe59f979fad458d6c7d025a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12/*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
25
Tim Schmielau4e57b682005-10-30 15:03:48 -080026#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/div64.h>
28
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010029/* Works only for digits and letters, but small and fast */
30#define TOLOWER(x) ((x) | 0x20)
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/**
33 * simple_strtoul - convert a string to an unsigned long
34 * @cp: The start of the string
35 * @endp: A pointer to the end of the parsed string will be placed here
36 * @base: The number base to use
37 */
38unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
39{
40 unsigned long result = 0,value;
41
42 if (!base) {
43 base = 10;
44 if (*cp == '0') {
45 base = 8;
46 cp++;
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010047 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 cp++;
49 base = 16;
50 }
51 }
52 } else if (base == 16) {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010053 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 cp += 2;
55 }
56 while (isxdigit(*cp) &&
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010057 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 result = result*base + value;
59 cp++;
60 }
61 if (endp)
62 *endp = (char *)cp;
63 return result;
64}
65
66EXPORT_SYMBOL(simple_strtoul);
67
68/**
69 * simple_strtol - convert a string to a signed long
70 * @cp: The start of the string
71 * @endp: A pointer to the end of the parsed string will be placed here
72 * @base: The number base to use
73 */
74long simple_strtol(const char *cp,char **endp,unsigned int base)
75{
76 if(*cp=='-')
77 return -simple_strtoul(cp+1,endp,base);
78 return simple_strtoul(cp,endp,base);
79}
80
81EXPORT_SYMBOL(simple_strtol);
82
83/**
84 * simple_strtoull - convert a string to an unsigned long long
85 * @cp: The start of the string
86 * @endp: A pointer to the end of the parsed string will be placed here
87 * @base: The number base to use
88 */
89unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
90{
91 unsigned long long result = 0,value;
92
93 if (!base) {
94 base = 10;
95 if (*cp == '0') {
96 base = 8;
97 cp++;
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010098 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 cp++;
100 base = 16;
101 }
102 }
103 } else if (base == 16) {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100104 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 cp += 2;
106 }
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100107 while (isxdigit(*cp)
108 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 result = result*base + value;
110 cp++;
111 }
112 if (endp)
113 *endp = (char *)cp;
114 return result;
115}
116
117EXPORT_SYMBOL(simple_strtoull);
118
119/**
120 * simple_strtoll - convert a string to a signed long long
121 * @cp: The start of the string
122 * @endp: A pointer to the end of the parsed string will be placed here
123 * @base: The number base to use
124 */
125long long simple_strtoll(const char *cp,char **endp,unsigned int base)
126{
127 if(*cp=='-')
128 return -simple_strtoull(cp+1,endp,base);
129 return simple_strtoull(cp,endp,base);
130}
131
Yi Yang06b2a762008-02-08 04:21:57 -0800132
133/**
134 * strict_strtoul - convert a string to an unsigned long strictly
135 * @cp: The string to be converted
136 * @base: The number base to use
137 * @res: The converted result value
138 *
139 * strict_strtoul converts a string to an unsigned long only if the
140 * string is really an unsigned long string, any string containing
141 * any invalid char at the tail will be rejected and -EINVAL is returned,
142 * only a newline char at the tail is acceptible because people generally
143 * change a module parameter in the following way:
144 *
145 * echo 1024 > /sys/module/e1000/parameters/copybreak
146 *
147 * echo will append a newline to the tail.
148 *
149 * It returns 0 if conversion is successful and *res is set to the converted
150 * value, otherwise it returns -EINVAL and *res is set to 0.
151 *
152 * simple_strtoul just ignores the successive invalid characters and
153 * return the converted value of prefix part of the string.
154 */
155int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
156
157/**
158 * strict_strtol - convert a string to a long strictly
159 * @cp: The string to be converted
160 * @base: The number base to use
161 * @res: The converted result value
162 *
163 * strict_strtol is similiar to strict_strtoul, but it allows the first
164 * character of a string is '-'.
165 *
166 * It returns 0 if conversion is successful and *res is set to the converted
167 * value, otherwise it returns -EINVAL and *res is set to 0.
168 */
169int strict_strtol(const char *cp, unsigned int base, long *res);
170
171/**
172 * strict_strtoull - convert a string to an unsigned long long strictly
173 * @cp: The string to be converted
174 * @base: The number base to use
175 * @res: The converted result value
176 *
177 * strict_strtoull converts a string to an unsigned long long only if the
178 * string is really an unsigned long long string, any string containing
179 * any invalid char at the tail will be rejected and -EINVAL is returned,
180 * only a newline char at the tail is acceptible because people generally
181 * change a module parameter in the following way:
182 *
183 * echo 1024 > /sys/module/e1000/parameters/copybreak
184 *
185 * echo will append a newline to the tail of the string.
186 *
187 * It returns 0 if conversion is successful and *res is set to the converted
188 * value, otherwise it returns -EINVAL and *res is set to 0.
189 *
190 * simple_strtoull just ignores the successive invalid characters and
191 * return the converted value of prefix part of the string.
192 */
193int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
194
195/**
196 * strict_strtoll - convert a string to a long long strictly
197 * @cp: The string to be converted
198 * @base: The number base to use
199 * @res: The converted result value
200 *
201 * strict_strtoll is similiar to strict_strtoull, but it allows the first
202 * character of a string is '-'.
203 *
204 * It returns 0 if conversion is successful and *res is set to the converted
205 * value, otherwise it returns -EINVAL and *res is set to 0.
206 */
207int strict_strtoll(const char *cp, unsigned int base, long long *res);
208
209#define define_strict_strtoux(type, valtype) \
210int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
211{ \
212 char *tail; \
213 valtype val; \
214 size_t len; \
215 \
216 *res = 0; \
217 len = strlen(cp); \
218 if (len == 0) \
219 return -EINVAL; \
220 \
221 val = simple_strtoul(cp, &tail, base); \
222 if ((*tail == '\0') || \
223 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
224 *res = val; \
225 return 0; \
226 } \
227 \
228 return -EINVAL; \
229} \
230
231#define define_strict_strtox(type, valtype) \
232int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
233{ \
234 int ret; \
235 if (*cp == '-') { \
236 ret = strict_strtou##type(cp+1, base, res); \
Hoang-Nam Nguyen4f9d5f42008-02-23 15:23:37 -0800237 if (!ret) \
Yi Yang06b2a762008-02-08 04:21:57 -0800238 *res = -(*res); \
239 } else \
240 ret = strict_strtou##type(cp, base, res); \
241 \
242 return ret; \
243} \
244
245define_strict_strtoux(l, unsigned long)
246define_strict_strtox(l, long)
247define_strict_strtoux(ll, unsigned long long)
248define_strict_strtox(ll, long long)
249
250EXPORT_SYMBOL(strict_strtoul);
251EXPORT_SYMBOL(strict_strtol);
252EXPORT_SYMBOL(strict_strtoll);
253EXPORT_SYMBOL(strict_strtoull);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int skip_atoi(const char **s)
256{
257 int i=0;
258
259 while (isdigit(**s))
260 i = i*10 + *((*s)++) - '0';
261 return i;
262}
263
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700264/* Decimal conversion is by far the most typical, and is used
265 * for /proc and /sys data. This directly impacts e.g. top performance
266 * with many processes running. We optimize it for speed
267 * using code from
268 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
269 * (with permission from the author, Douglas W. Jones). */
270
271/* Formats correctly any integer in [0,99999].
272 * Outputs from one to five digits depending on input.
273 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
274static char* put_dec_trunc(char *buf, unsigned q)
275{
276 unsigned d3, d2, d1, d0;
277 d1 = (q>>4) & 0xf;
278 d2 = (q>>8) & 0xf;
279 d3 = (q>>12);
280
281 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
282 q = (d0 * 0xcd) >> 11;
283 d0 = d0 - 10*q;
284 *buf++ = d0 + '0'; /* least significant digit */
285 d1 = q + 9*d3 + 5*d2 + d1;
286 if (d1 != 0) {
287 q = (d1 * 0xcd) >> 11;
288 d1 = d1 - 10*q;
289 *buf++ = d1 + '0'; /* next digit */
290
291 d2 = q + 2*d2;
292 if ((d2 != 0) || (d3 != 0)) {
293 q = (d2 * 0xd) >> 7;
294 d2 = d2 - 10*q;
295 *buf++ = d2 + '0'; /* next digit */
296
297 d3 = q + 4*d3;
298 if (d3 != 0) {
299 q = (d3 * 0xcd) >> 11;
300 d3 = d3 - 10*q;
301 *buf++ = d3 + '0'; /* next digit */
302 if (q != 0)
303 *buf++ = q + '0'; /* most sign. digit */
304 }
305 }
306 }
307 return buf;
308}
309/* Same with if's removed. Always emits five digits */
310static char* put_dec_full(char *buf, unsigned q)
311{
312 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
313 /* but anyway, gcc produces better code with full-sized ints */
314 unsigned d3, d2, d1, d0;
315 d1 = (q>>4) & 0xf;
316 d2 = (q>>8) & 0xf;
317 d3 = (q>>12);
318
319 /* Possible ways to approx. divide by 10 */
320 /* gcc -O2 replaces multiply with shifts and adds */
321 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
322 // (x * 0x67) >> 10: 1100111
323 // (x * 0x34) >> 9: 110100 - same
324 // (x * 0x1a) >> 8: 11010 - same
325 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
326
327 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
328 q = (d0 * 0xcd) >> 11;
329 d0 = d0 - 10*q;
330 *buf++ = d0 + '0';
331 d1 = q + 9*d3 + 5*d2 + d1;
332 q = (d1 * 0xcd) >> 11;
333 d1 = d1 - 10*q;
334 *buf++ = d1 + '0';
335
336 d2 = q + 2*d2;
337 q = (d2 * 0xd) >> 7;
338 d2 = d2 - 10*q;
339 *buf++ = d2 + '0';
340
341 d3 = q + 4*d3;
342 q = (d3 * 0xcd) >> 11; /* - shorter code */
343 /* q = (d3 * 0x67) >> 10; - would also work */
344 d3 = d3 - 10*q;
345 *buf++ = d3 + '0';
346 *buf++ = q + '0';
347 return buf;
348}
349/* No inlining helps gcc to use registers better */
350static noinline char* put_dec(char *buf, unsigned long long num)
351{
352 while (1) {
353 unsigned rem;
354 if (num < 100000)
355 return put_dec_trunc(buf, num);
356 rem = do_div(num, 100000);
357 buf = put_dec_full(buf, rem);
358 }
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define ZEROPAD 1 /* pad with zero */
362#define SIGN 2 /* unsigned/signed long */
363#define PLUS 4 /* show plus */
364#define SPACE 8 /* space if plus */
365#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100366#define SMALL 32 /* Must be 32 == 0x20 */
367#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700369static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100371 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
372 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
373
374 char tmp[66];
375 char sign;
376 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700377 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int i;
379
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100380 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
381 * produces same digits or (maybe lowercased) letters */
382 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (type & LEFT)
384 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sign = 0;
386 if (type & SIGN) {
387 if ((signed long long) num < 0) {
388 sign = '-';
389 num = - (signed long long) num;
390 size--;
391 } else if (type & PLUS) {
392 sign = '+';
393 size--;
394 } else if (type & SPACE) {
395 sign = ' ';
396 size--;
397 }
398 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700399 if (need_pfx) {
400 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 size--;
403 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700404
405 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 i = 0;
407 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700408 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700409 /* Generic code, for any base:
410 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100411 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700412 } while (num != 0);
413 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700414 else if (base != 10) { /* 8 or 16 */
415 int mask = base - 1;
416 int shift = 3;
417 if (base == 16) shift = 4;
418 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100419 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700420 num >>= shift;
421 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700422 } else { /* base 10 */
423 i = put_dec(tmp, num) - tmp;
424 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700425
426 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (i > precision)
428 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700429 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700431 if (!(type & (ZEROPAD+LEFT))) {
432 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700433 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *buf = ' ';
435 ++buf;
436 }
437 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700438 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700440 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *buf = sign;
442 ++buf;
443 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700444 /* "0x" / "0" prefix */
445 if (need_pfx) {
446 if (buf < end)
447 *buf = '0';
448 ++buf;
449 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700450 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100451 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ++buf;
453 }
454 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700457 char c = (type & ZEROPAD) ? '0' : ' ';
458 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700459 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *buf = c;
461 ++buf;
462 }
463 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 /* hmm even more zero padding? */
465 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700466 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *buf = '0';
468 ++buf;
469 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700470 /* actual digits of result */
471 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700472 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *buf = tmp[i];
474 ++buf;
475 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700476 /* trailing space padding */
477 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700478 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *buf = ' ';
480 ++buf;
481 }
482 return buf;
483}
484
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700485static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
486{
487 int len, i;
488
489 if ((unsigned long)s < PAGE_SIZE)
490 s = "<NULL>";
491
492 len = strnlen(s, precision);
493
494 if (!(flags & LEFT)) {
495 while (len < field_width--) {
496 if (buf < end)
497 *buf = ' ';
498 ++buf;
499 }
500 }
501 for (i = 0; i < len; ++i) {
502 if (buf < end)
503 *buf = *s;
504 ++buf; ++s;
505 }
506 while (len < field_width--) {
507 if (buf < end)
508 *buf = ' ';
509 ++buf;
510 }
511 return buf;
512}
513
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700514/*
515 * Show a '%p' thing. A kernel extension is that the '%p' is followed
516 * by an extra set of alphanumeric characters that are extended format
517 * specifiers.
518 *
519 * Right now don't actually handle any such, but we will..
520 */
521static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700522{
523 flags |= SMALL;
524 if (field_width == -1) {
525 field_width = 2*sizeof(void *);
526 flags |= ZEROPAD;
527 }
528 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/**
532 * vsnprintf - Format a string and place it in a buffer
533 * @buf: The buffer to place the result into
534 * @size: The size of the buffer, including the trailing null space
535 * @fmt: The format string to use
536 * @args: Arguments for the format string
537 *
538 * The return value is the number of characters which would
539 * be generated for the given input, excluding the trailing
540 * '\0', as per ISO C99. If you want to have the exact
541 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800542 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * return is greater than or equal to @size, the resulting
544 * string is truncated.
545 *
546 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800547 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
550{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700552 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 int flags; /* flags to number() */
556
557 int field_width; /* width of output field */
558 int precision; /* min. # of digits for integers; max
559 number of chars for from string */
560 int qualifier; /* 'h', 'l', or 'L' for integer fields */
561 /* 'z' support added 23/7/1999 S.H. */
562 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100563 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700565 /* Reject out-of-range values early. Large positive sizes are
566 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (unlikely((int) size < 0)) {
568 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700569 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 WARN_ON(warn);
571 warn = 0;
572 return 0;
573 }
574
575 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700576 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700578 /* Make sure end is always >= buf */
579 if (end < buf) {
580 end = ((void *)-1);
581 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 for (; *fmt ; ++fmt) {
585 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700586 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 *str = *fmt;
588 ++str;
589 continue;
590 }
591
592 /* process flags */
593 flags = 0;
594 repeat:
595 ++fmt; /* this also skips first '%' */
596 switch (*fmt) {
597 case '-': flags |= LEFT; goto repeat;
598 case '+': flags |= PLUS; goto repeat;
599 case ' ': flags |= SPACE; goto repeat;
600 case '#': flags |= SPECIAL; goto repeat;
601 case '0': flags |= ZEROPAD; goto repeat;
602 }
603
604 /* get field width */
605 field_width = -1;
606 if (isdigit(*fmt))
607 field_width = skip_atoi(&fmt);
608 else if (*fmt == '*') {
609 ++fmt;
610 /* it's the next argument */
611 field_width = va_arg(args, int);
612 if (field_width < 0) {
613 field_width = -field_width;
614 flags |= LEFT;
615 }
616 }
617
618 /* get the precision */
619 precision = -1;
620 if (*fmt == '.') {
621 ++fmt;
622 if (isdigit(*fmt))
623 precision = skip_atoi(&fmt);
624 else if (*fmt == '*') {
625 ++fmt;
626 /* it's the next argument */
627 precision = va_arg(args, int);
628 }
629 if (precision < 0)
630 precision = 0;
631 }
632
633 /* get the conversion qualifier */
634 qualifier = -1;
635 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100636 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 qualifier = *fmt;
638 ++fmt;
639 if (qualifier == 'l' && *fmt == 'l') {
640 qualifier = 'L';
641 ++fmt;
642 }
643 }
644
645 /* default base */
646 base = 10;
647
648 switch (*fmt) {
649 case 'c':
650 if (!(flags & LEFT)) {
651 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700652 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *str = ' ';
654 ++str;
655 }
656 }
657 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700658 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *str = c;
660 ++str;
661 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700662 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *str = ' ';
664 ++str;
665 }
666 continue;
667
668 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700669 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
671
672 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700673 str = pointer(fmt+1, str, end,
674 va_arg(args, void *),
675 field_width, precision, flags);
676 /* Skip all alphanumeric pointer suffixes */
677 while (isalnum(fmt[1]))
678 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 continue;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case 'n':
682 /* FIXME:
683 * What does C99 say about the overflow case here? */
684 if (qualifier == 'l') {
685 long * ip = va_arg(args, long *);
686 *ip = (str - buf);
687 } else if (qualifier == 'Z' || qualifier == 'z') {
688 size_t * ip = va_arg(args, size_t *);
689 *ip = (str - buf);
690 } else {
691 int * ip = va_arg(args, int *);
692 *ip = (str - buf);
693 }
694 continue;
695
696 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700697 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *str = '%';
699 ++str;
700 continue;
701
702 /* integer number formats - set up the flags and "break" */
703 case 'o':
704 base = 8;
705 break;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100708 flags |= SMALL;
709 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 base = 16;
711 break;
712
713 case 'd':
714 case 'i':
715 flags |= SIGN;
716 case 'u':
717 break;
718
719 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700720 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *str = '%';
722 ++str;
723 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700724 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *str = *fmt;
726 ++str;
727 } else {
728 --fmt;
729 }
730 continue;
731 }
732 if (qualifier == 'L')
733 num = va_arg(args, long long);
734 else if (qualifier == 'l') {
735 num = va_arg(args, unsigned long);
736 if (flags & SIGN)
737 num = (signed long) num;
738 } else if (qualifier == 'Z' || qualifier == 'z') {
739 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100740 } else if (qualifier == 't') {
741 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else if (qualifier == 'h') {
743 num = (unsigned short) va_arg(args, int);
744 if (flags & SIGN)
745 num = (signed short) num;
746 } else {
747 num = va_arg(args, unsigned int);
748 if (flags & SIGN)
749 num = (signed int) num;
750 }
751 str = number(str, end, num, base,
752 field_width, precision, flags);
753 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700754 if (size > 0) {
755 if (str < end)
756 *str = '\0';
757 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700758 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700759 }
760 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return str-buf;
762}
763
764EXPORT_SYMBOL(vsnprintf);
765
766/**
767 * vscnprintf - Format a string and place it in a buffer
768 * @buf: The buffer to place the result into
769 * @size: The size of the buffer, including the trailing null space
770 * @fmt: The format string to use
771 * @args: Arguments for the format string
772 *
773 * The return value is the number of characters which have been written into
774 * the @buf not including the trailing '\0'. If @size is <= 0 the function
775 * returns 0.
776 *
777 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800778 * You probably want scnprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
780int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
781{
782 int i;
783
784 i=vsnprintf(buf,size,fmt,args);
785 return (i >= size) ? (size - 1) : i;
786}
787
788EXPORT_SYMBOL(vscnprintf);
789
790/**
791 * snprintf - Format a string and place it in a buffer
792 * @buf: The buffer to place the result into
793 * @size: The size of the buffer, including the trailing null space
794 * @fmt: The format string to use
795 * @...: Arguments for the format string
796 *
797 * The return value is the number of characters which would be
798 * generated for the given input, excluding the trailing null,
799 * as per ISO C99. If the return is greater than or equal to
800 * @size, the resulting string is truncated.
801 */
802int snprintf(char * buf, size_t size, const char *fmt, ...)
803{
804 va_list args;
805 int i;
806
807 va_start(args, fmt);
808 i=vsnprintf(buf,size,fmt,args);
809 va_end(args);
810 return i;
811}
812
813EXPORT_SYMBOL(snprintf);
814
815/**
816 * scnprintf - Format a string and place it in a buffer
817 * @buf: The buffer to place the result into
818 * @size: The size of the buffer, including the trailing null space
819 * @fmt: The format string to use
820 * @...: Arguments for the format string
821 *
822 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -0800823 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
825
826int scnprintf(char * buf, size_t size, const char *fmt, ...)
827{
828 va_list args;
829 int i;
830
831 va_start(args, fmt);
832 i = vsnprintf(buf, size, fmt, args);
833 va_end(args);
834 return (i >= size) ? (size - 1) : i;
835}
836EXPORT_SYMBOL(scnprintf);
837
838/**
839 * vsprintf - Format a string and place it in a buffer
840 * @buf: The buffer to place the result into
841 * @fmt: The format string to use
842 * @args: Arguments for the format string
843 *
844 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800845 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * buffer overflows.
847 *
848 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800849 * You probably want sprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851int vsprintf(char *buf, const char *fmt, va_list args)
852{
853 return vsnprintf(buf, INT_MAX, fmt, args);
854}
855
856EXPORT_SYMBOL(vsprintf);
857
858/**
859 * sprintf - Format a string and place it in a buffer
860 * @buf: The buffer to place the result into
861 * @fmt: The format string to use
862 * @...: Arguments for the format string
863 *
864 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800865 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * buffer overflows.
867 */
868int sprintf(char * buf, const char *fmt, ...)
869{
870 va_list args;
871 int i;
872
873 va_start(args, fmt);
874 i=vsnprintf(buf, INT_MAX, fmt, args);
875 va_end(args);
876 return i;
877}
878
879EXPORT_SYMBOL(sprintf);
880
881/**
882 * vsscanf - Unformat a buffer into a list of arguments
883 * @buf: input buffer
884 * @fmt: format of buffer
885 * @args: arguments
886 */
887int vsscanf(const char * buf, const char * fmt, va_list args)
888{
889 const char *str = buf;
890 char *next;
891 char digit;
892 int num = 0;
893 int qualifier;
894 int base;
895 int field_width;
896 int is_sign = 0;
897
898 while(*fmt && *str) {
899 /* skip any white space in format */
900 /* white space in format matchs any amount of
901 * white space, including none, in the input.
902 */
903 if (isspace(*fmt)) {
904 while (isspace(*fmt))
905 ++fmt;
906 while (isspace(*str))
907 ++str;
908 }
909
910 /* anything that is not a conversion must match exactly */
911 if (*fmt != '%' && *fmt) {
912 if (*fmt++ != *str++)
913 break;
914 continue;
915 }
916
917 if (!*fmt)
918 break;
919 ++fmt;
920
921 /* skip this conversion.
922 * advance both strings to next white space
923 */
924 if (*fmt == '*') {
925 while (!isspace(*fmt) && *fmt)
926 fmt++;
927 while (!isspace(*str) && *str)
928 str++;
929 continue;
930 }
931
932 /* get field width */
933 field_width = -1;
934 if (isdigit(*fmt))
935 field_width = skip_atoi(&fmt);
936
937 /* get conversion qualifier */
938 qualifier = -1;
939 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
940 *fmt == 'Z' || *fmt == 'z') {
941 qualifier = *fmt++;
942 if (unlikely(qualifier == *fmt)) {
943 if (qualifier == 'h') {
944 qualifier = 'H';
945 fmt++;
946 } else if (qualifier == 'l') {
947 qualifier = 'L';
948 fmt++;
949 }
950 }
951 }
952 base = 10;
953 is_sign = 0;
954
955 if (!*fmt || !*str)
956 break;
957
958 switch(*fmt++) {
959 case 'c':
960 {
961 char *s = (char *) va_arg(args,char*);
962 if (field_width == -1)
963 field_width = 1;
964 do {
965 *s++ = *str++;
966 } while (--field_width > 0 && *str);
967 num++;
968 }
969 continue;
970 case 's':
971 {
972 char *s = (char *) va_arg(args, char *);
973 if(field_width == -1)
974 field_width = INT_MAX;
975 /* first, skip leading white space in buffer */
976 while (isspace(*str))
977 str++;
978
979 /* now copy until next white space */
980 while (*str && !isspace(*str) && field_width--) {
981 *s++ = *str++;
982 }
983 *s = '\0';
984 num++;
985 }
986 continue;
987 case 'n':
988 /* return number of characters read so far */
989 {
990 int *i = (int *)va_arg(args,int*);
991 *i = str - buf;
992 }
993 continue;
994 case 'o':
995 base = 8;
996 break;
997 case 'x':
998 case 'X':
999 base = 16;
1000 break;
1001 case 'i':
1002 base = 0;
1003 case 'd':
1004 is_sign = 1;
1005 case 'u':
1006 break;
1007 case '%':
1008 /* looking for '%' in str */
1009 if (*str++ != '%')
1010 return num;
1011 continue;
1012 default:
1013 /* invalid format; stop here */
1014 return num;
1015 }
1016
1017 /* have some sort of integer conversion.
1018 * first, skip white space in buffer.
1019 */
1020 while (isspace(*str))
1021 str++;
1022
1023 digit = *str;
1024 if (is_sign && digit == '-')
1025 digit = *(str + 1);
1026
1027 if (!digit
1028 || (base == 16 && !isxdigit(digit))
1029 || (base == 10 && !isdigit(digit))
1030 || (base == 8 && (!isdigit(digit) || digit > '7'))
1031 || (base == 0 && !isdigit(digit)))
1032 break;
1033
1034 switch(qualifier) {
1035 case 'H': /* that's 'hh' in format */
1036 if (is_sign) {
1037 signed char *s = (signed char *) va_arg(args,signed char *);
1038 *s = (signed char) simple_strtol(str,&next,base);
1039 } else {
1040 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1041 *s = (unsigned char) simple_strtoul(str, &next, base);
1042 }
1043 break;
1044 case 'h':
1045 if (is_sign) {
1046 short *s = (short *) va_arg(args,short *);
1047 *s = (short) simple_strtol(str,&next,base);
1048 } else {
1049 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1050 *s = (unsigned short) simple_strtoul(str, &next, base);
1051 }
1052 break;
1053 case 'l':
1054 if (is_sign) {
1055 long *l = (long *) va_arg(args,long *);
1056 *l = simple_strtol(str,&next,base);
1057 } else {
1058 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1059 *l = simple_strtoul(str,&next,base);
1060 }
1061 break;
1062 case 'L':
1063 if (is_sign) {
1064 long long *l = (long long*) va_arg(args,long long *);
1065 *l = simple_strtoll(str,&next,base);
1066 } else {
1067 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1068 *l = simple_strtoull(str,&next,base);
1069 }
1070 break;
1071 case 'Z':
1072 case 'z':
1073 {
1074 size_t *s = (size_t*) va_arg(args,size_t*);
1075 *s = (size_t) simple_strtoul(str,&next,base);
1076 }
1077 break;
1078 default:
1079 if (is_sign) {
1080 int *i = (int *) va_arg(args, int*);
1081 *i = (int) simple_strtol(str,&next,base);
1082 } else {
1083 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1084 *i = (unsigned int) simple_strtoul(str,&next,base);
1085 }
1086 break;
1087 }
1088 num++;
1089
1090 if (!next)
1091 break;
1092 str = next;
1093 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001094
1095 /*
1096 * Now we've come all the way through so either the input string or the
1097 * format ended. In the former case, there can be a %n at the current
1098 * position in the format that needs to be filled.
1099 */
1100 if (*fmt == '%' && *(fmt + 1) == 'n') {
1101 int *p = (int *)va_arg(args, int *);
1102 *p = str - buf;
1103 }
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return num;
1106}
1107
1108EXPORT_SYMBOL(vsscanf);
1109
1110/**
1111 * sscanf - Unformat a buffer into a list of arguments
1112 * @buf: input buffer
1113 * @fmt: formatting of buffer
1114 * @...: resulting arguments
1115 */
1116int sscanf(const char * buf, const char * fmt, ...)
1117{
1118 va_list args;
1119 int i;
1120
1121 va_start(args,fmt);
1122 i = vsscanf(buf,fmt,args);
1123 va_end(args);
1124 return i;
1125}
1126
1127EXPORT_SYMBOL(sscanf);