lib: pull base-guessing logic to helper function
[linux-2.6/verdex.git] / lib / vsprintf.c
blob1a9c6b9b49447c6f61f7c710c7694c9ae3af3d60
1 /*
2 * linux/lib/vsprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
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 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
28 #include <asm/page.h> /* for PAGE_SIZE */
29 #include <asm/div64.h>
30 #include <asm/sections.h> /* for dereference_function_descriptor() */
32 /* Works only for digits and letters, but small and fast */
33 #define TOLOWER(x) ((x) | 0x20)
35 static unsigned int simple_guess_base(const char *cp)
37 if (cp[0] == '0') {
38 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
39 return 16;
40 else
41 return 8;
42 } else {
43 return 10;
47 /**
48 * simple_strtoul - convert a string to an unsigned long
49 * @cp: The start of the string
50 * @endp: A pointer to the end of the parsed string will be placed here
51 * @base: The number base to use
53 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
55 unsigned long result = 0;
57 if (!base)
58 base = simple_guess_base(cp);
60 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
61 cp += 2;
63 while (isxdigit(*cp)) {
64 unsigned int value;
66 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
67 if (value >= base)
68 break;
69 result = result * base + value;
70 cp++;
73 if (endp)
74 *endp = (char *)cp;
75 return result;
77 EXPORT_SYMBOL(simple_strtoul);
79 /**
80 * simple_strtol - convert a string to a signed long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 long simple_strtol(const char *cp,char **endp,unsigned int base)
87 if(*cp=='-')
88 return -simple_strtoul(cp+1,endp,base);
89 return simple_strtoul(cp,endp,base);
92 EXPORT_SYMBOL(simple_strtol);
94 /**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
102 unsigned long long result = 0;
104 if (!base)
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
110 while (isxdigit(*cp)) {
111 unsigned int value;
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
117 cp++;
120 if (endp)
121 *endp = (char *)cp;
122 return result;
124 EXPORT_SYMBOL(simple_strtoull);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
134 if(*cp=='-')
135 return -simple_strtoull(cp+1,endp,base);
136 return simple_strtoull(cp,endp,base);
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
154 * echo will append a newline to the tail.
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
162 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
165 * strict_strtol - convert a string to a long strictly
166 * @cp: The string to be converted
167 * @base: The number base to use
168 * @res: The converted result value
170 * strict_strtol is similiar to strict_strtoul, but it allows the first
171 * character of a string is '-'.
173 * It returns 0 if conversion is successful and *res is set to the converted
174 * value, otherwise it returns -EINVAL and *res is set to 0.
176 int strict_strtol(const char *cp, unsigned int base, long *res);
179 * strict_strtoull - convert a string to an unsigned long long strictly
180 * @cp: The string to be converted
181 * @base: The number base to use
182 * @res: The converted result value
184 * strict_strtoull converts a string to an unsigned long long only if the
185 * string is really an unsigned long long string, any string containing
186 * any invalid char at the tail will be rejected and -EINVAL is returned,
187 * only a newline char at the tail is acceptible because people generally
188 * change a module parameter in the following way:
190 * echo 1024 > /sys/module/e1000/parameters/copybreak
192 * echo will append a newline to the tail of the string.
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
197 * simple_strtoull just ignores the successive invalid characters and
198 * return the converted value of prefix part of the string.
200 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
203 * strict_strtoll - convert a string to a long long strictly
204 * @cp: The string to be converted
205 * @base: The number base to use
206 * @res: The converted result value
208 * strict_strtoll is similiar to strict_strtoull, but it allows the first
209 * character of a string is '-'.
211 * It returns 0 if conversion is successful and *res is set to the converted
212 * value, otherwise it returns -EINVAL and *res is set to 0.
214 int strict_strtoll(const char *cp, unsigned int base, long long *res);
216 #define define_strict_strtoux(type, valtype) \
217 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
219 char *tail; \
220 valtype val; \
221 size_t len; \
223 *res = 0; \
224 len = strlen(cp); \
225 if (len == 0) \
226 return -EINVAL; \
228 val = simple_strtou##type(cp, &tail, base); \
229 if ((*tail == '\0') || \
230 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
231 *res = val; \
232 return 0; \
235 return -EINVAL; \
238 #define define_strict_strtox(type, valtype) \
239 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
241 int ret; \
242 if (*cp == '-') { \
243 ret = strict_strtou##type(cp+1, base, res); \
244 if (!ret) \
245 *res = -(*res); \
246 } else \
247 ret = strict_strtou##type(cp, base, res); \
249 return ret; \
252 define_strict_strtoux(l, unsigned long)
253 define_strict_strtox(l, long)
254 define_strict_strtoux(ll, unsigned long long)
255 define_strict_strtox(ll, long long)
257 EXPORT_SYMBOL(strict_strtoul);
258 EXPORT_SYMBOL(strict_strtol);
259 EXPORT_SYMBOL(strict_strtoll);
260 EXPORT_SYMBOL(strict_strtoull);
262 static int skip_atoi(const char **s)
264 int i=0;
266 while (isdigit(**s))
267 i = i*10 + *((*s)++) - '0';
268 return i;
271 /* Decimal conversion is by far the most typical, and is used
272 * for /proc and /sys data. This directly impacts e.g. top performance
273 * with many processes running. We optimize it for speed
274 * using code from
275 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
276 * (with permission from the author, Douglas W. Jones). */
278 /* Formats correctly any integer in [0,99999].
279 * Outputs from one to five digits depending on input.
280 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
281 static char* put_dec_trunc(char *buf, unsigned q)
283 unsigned d3, d2, d1, d0;
284 d1 = (q>>4) & 0xf;
285 d2 = (q>>8) & 0xf;
286 d3 = (q>>12);
288 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
289 q = (d0 * 0xcd) >> 11;
290 d0 = d0 - 10*q;
291 *buf++ = d0 + '0'; /* least significant digit */
292 d1 = q + 9*d3 + 5*d2 + d1;
293 if (d1 != 0) {
294 q = (d1 * 0xcd) >> 11;
295 d1 = d1 - 10*q;
296 *buf++ = d1 + '0'; /* next digit */
298 d2 = q + 2*d2;
299 if ((d2 != 0) || (d3 != 0)) {
300 q = (d2 * 0xd) >> 7;
301 d2 = d2 - 10*q;
302 *buf++ = d2 + '0'; /* next digit */
304 d3 = q + 4*d3;
305 if (d3 != 0) {
306 q = (d3 * 0xcd) >> 11;
307 d3 = d3 - 10*q;
308 *buf++ = d3 + '0'; /* next digit */
309 if (q != 0)
310 *buf++ = q + '0'; /* most sign. digit */
314 return buf;
316 /* Same with if's removed. Always emits five digits */
317 static char* put_dec_full(char *buf, unsigned q)
319 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
320 /* but anyway, gcc produces better code with full-sized ints */
321 unsigned d3, d2, d1, d0;
322 d1 = (q>>4) & 0xf;
323 d2 = (q>>8) & 0xf;
324 d3 = (q>>12);
326 /* Possible ways to approx. divide by 10 */
327 /* gcc -O2 replaces multiply with shifts and adds */
328 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
329 // (x * 0x67) >> 10: 1100111
330 // (x * 0x34) >> 9: 110100 - same
331 // (x * 0x1a) >> 8: 11010 - same
332 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
334 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
335 q = (d0 * 0xcd) >> 11;
336 d0 = d0 - 10*q;
337 *buf++ = d0 + '0';
338 d1 = q + 9*d3 + 5*d2 + d1;
339 q = (d1 * 0xcd) >> 11;
340 d1 = d1 - 10*q;
341 *buf++ = d1 + '0';
343 d2 = q + 2*d2;
344 q = (d2 * 0xd) >> 7;
345 d2 = d2 - 10*q;
346 *buf++ = d2 + '0';
348 d3 = q + 4*d3;
349 q = (d3 * 0xcd) >> 11; /* - shorter code */
350 /* q = (d3 * 0x67) >> 10; - would also work */
351 d3 = d3 - 10*q;
352 *buf++ = d3 + '0';
353 *buf++ = q + '0';
354 return buf;
356 /* No inlining helps gcc to use registers better */
357 static noinline char* put_dec(char *buf, unsigned long long num)
359 while (1) {
360 unsigned rem;
361 if (num < 100000)
362 return put_dec_trunc(buf, num);
363 rem = do_div(num, 100000);
364 buf = put_dec_full(buf, rem);
368 #define ZEROPAD 1 /* pad with zero */
369 #define SIGN 2 /* unsigned/signed long */
370 #define PLUS 4 /* show plus */
371 #define SPACE 8 /* space if plus */
372 #define LEFT 16 /* left justified */
373 #define SMALL 32 /* Must be 32 == 0x20 */
374 #define SPECIAL 64 /* 0x */
376 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
378 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
379 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
381 char tmp[66];
382 char sign;
383 char locase;
384 int need_pfx = ((type & SPECIAL) && base != 10);
385 int i;
387 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
388 * produces same digits or (maybe lowercased) letters */
389 locase = (type & SMALL);
390 if (type & LEFT)
391 type &= ~ZEROPAD;
392 sign = 0;
393 if (type & SIGN) {
394 if ((signed long long) num < 0) {
395 sign = '-';
396 num = - (signed long long) num;
397 size--;
398 } else if (type & PLUS) {
399 sign = '+';
400 size--;
401 } else if (type & SPACE) {
402 sign = ' ';
403 size--;
406 if (need_pfx) {
407 size--;
408 if (base == 16)
409 size--;
412 /* generate full string in tmp[], in reverse order */
413 i = 0;
414 if (num == 0)
415 tmp[i++] = '0';
416 /* Generic code, for any base:
417 else do {
418 tmp[i++] = (digits[do_div(num,base)] | locase);
419 } while (num != 0);
421 else if (base != 10) { /* 8 or 16 */
422 int mask = base - 1;
423 int shift = 3;
424 if (base == 16) shift = 4;
425 do {
426 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
427 num >>= shift;
428 } while (num);
429 } else { /* base 10 */
430 i = put_dec(tmp, num) - tmp;
433 /* printing 100 using %2d gives "100", not "00" */
434 if (i > precision)
435 precision = i;
436 /* leading space padding */
437 size -= precision;
438 if (!(type & (ZEROPAD+LEFT))) {
439 while(--size >= 0) {
440 if (buf < end)
441 *buf = ' ';
442 ++buf;
445 /* sign */
446 if (sign) {
447 if (buf < end)
448 *buf = sign;
449 ++buf;
451 /* "0x" / "0" prefix */
452 if (need_pfx) {
453 if (buf < end)
454 *buf = '0';
455 ++buf;
456 if (base == 16) {
457 if (buf < end)
458 *buf = ('X' | locase);
459 ++buf;
462 /* zero or space padding */
463 if (!(type & LEFT)) {
464 char c = (type & ZEROPAD) ? '0' : ' ';
465 while (--size >= 0) {
466 if (buf < end)
467 *buf = c;
468 ++buf;
471 /* hmm even more zero padding? */
472 while (i <= --precision) {
473 if (buf < end)
474 *buf = '0';
475 ++buf;
477 /* actual digits of result */
478 while (--i >= 0) {
479 if (buf < end)
480 *buf = tmp[i];
481 ++buf;
483 /* trailing space padding */
484 while (--size >= 0) {
485 if (buf < end)
486 *buf = ' ';
487 ++buf;
489 return buf;
492 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
494 int len, i;
496 if ((unsigned long)s < PAGE_SIZE)
497 s = "<NULL>";
499 len = strnlen(s, precision);
501 if (!(flags & LEFT)) {
502 while (len < field_width--) {
503 if (buf < end)
504 *buf = ' ';
505 ++buf;
508 for (i = 0; i < len; ++i) {
509 if (buf < end)
510 *buf = *s;
511 ++buf; ++s;
513 while (len < field_width--) {
514 if (buf < end)
515 *buf = ' ';
516 ++buf;
518 return buf;
521 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
523 unsigned long value = (unsigned long) ptr;
524 #ifdef CONFIG_KALLSYMS
525 char sym[KSYM_SYMBOL_LEN];
526 sprint_symbol(sym, value);
527 return string(buf, end, sym, field_width, precision, flags);
528 #else
529 field_width = 2*sizeof(void *);
530 flags |= SPECIAL | SMALL | ZEROPAD;
531 return number(buf, end, value, 16, field_width, precision, flags);
532 #endif
536 * Show a '%p' thing. A kernel extension is that the '%p' is followed
537 * by an extra set of alphanumeric characters that are extended format
538 * specifiers.
540 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
541 * and 'S' (for Symbolic direct pointers), but this can easily be
542 * extended in the future (network address types etc).
544 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
545 * pointers are really function descriptors, which contain a pointer the
546 * real address.
548 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
550 switch (*fmt) {
551 case 'F':
552 ptr = dereference_function_descriptor(ptr);
553 /* Fallthrough */
554 case 'S':
555 return symbol_string(buf, end, ptr, field_width, precision, flags);
557 flags |= SMALL;
558 if (field_width == -1) {
559 field_width = 2*sizeof(void *);
560 flags |= ZEROPAD;
562 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
566 * vsnprintf - Format a string and place it in a buffer
567 * @buf: The buffer to place the result into
568 * @size: The size of the buffer, including the trailing null space
569 * @fmt: The format string to use
570 * @args: Arguments for the format string
572 * This function follows C99 vsnprintf, but has some extensions:
573 * %pS output the name of a text symbol
574 * %pF output the name of a function pointer
576 * The return value is the number of characters which would
577 * be generated for the given input, excluding the trailing
578 * '\0', as per ISO C99. If you want to have the exact
579 * number of characters written into @buf as return value
580 * (not including the trailing '\0'), use vscnprintf(). If the
581 * return is greater than or equal to @size, the resulting
582 * string is truncated.
584 * Call this function if you are already dealing with a va_list.
585 * You probably want snprintf() instead.
587 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
589 unsigned long long num;
590 int base;
591 char *str, *end, c;
593 int flags; /* flags to number() */
595 int field_width; /* width of output field */
596 int precision; /* min. # of digits for integers; max
597 number of chars for from string */
598 int qualifier; /* 'h', 'l', or 'L' for integer fields */
599 /* 'z' support added 23/7/1999 S.H. */
600 /* 'z' changed to 'Z' --davidm 1/25/99 */
601 /* 't' added for ptrdiff_t */
603 /* Reject out-of-range values early. Large positive sizes are
604 used for unknown buffer sizes. */
605 if (unlikely((int) size < 0)) {
606 /* There can be only one.. */
607 static char warn = 1;
608 WARN_ON(warn);
609 warn = 0;
610 return 0;
613 str = buf;
614 end = buf + size;
616 /* Make sure end is always >= buf */
617 if (end < buf) {
618 end = ((void *)-1);
619 size = end - buf;
622 for (; *fmt ; ++fmt) {
623 if (*fmt != '%') {
624 if (str < end)
625 *str = *fmt;
626 ++str;
627 continue;
630 /* process flags */
631 flags = 0;
632 repeat:
633 ++fmt; /* this also skips first '%' */
634 switch (*fmt) {
635 case '-': flags |= LEFT; goto repeat;
636 case '+': flags |= PLUS; goto repeat;
637 case ' ': flags |= SPACE; goto repeat;
638 case '#': flags |= SPECIAL; goto repeat;
639 case '0': flags |= ZEROPAD; goto repeat;
642 /* get field width */
643 field_width = -1;
644 if (isdigit(*fmt))
645 field_width = skip_atoi(&fmt);
646 else if (*fmt == '*') {
647 ++fmt;
648 /* it's the next argument */
649 field_width = va_arg(args, int);
650 if (field_width < 0) {
651 field_width = -field_width;
652 flags |= LEFT;
656 /* get the precision */
657 precision = -1;
658 if (*fmt == '.') {
659 ++fmt;
660 if (isdigit(*fmt))
661 precision = skip_atoi(&fmt);
662 else if (*fmt == '*') {
663 ++fmt;
664 /* it's the next argument */
665 precision = va_arg(args, int);
667 if (precision < 0)
668 precision = 0;
671 /* get the conversion qualifier */
672 qualifier = -1;
673 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
674 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
675 qualifier = *fmt;
676 ++fmt;
677 if (qualifier == 'l' && *fmt == 'l') {
678 qualifier = 'L';
679 ++fmt;
683 /* default base */
684 base = 10;
686 switch (*fmt) {
687 case 'c':
688 if (!(flags & LEFT)) {
689 while (--field_width > 0) {
690 if (str < end)
691 *str = ' ';
692 ++str;
695 c = (unsigned char) va_arg(args, int);
696 if (str < end)
697 *str = c;
698 ++str;
699 while (--field_width > 0) {
700 if (str < end)
701 *str = ' ';
702 ++str;
704 continue;
706 case 's':
707 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
708 continue;
710 case 'p':
711 str = pointer(fmt+1, str, end,
712 va_arg(args, void *),
713 field_width, precision, flags);
714 /* Skip all alphanumeric pointer suffixes */
715 while (isalnum(fmt[1]))
716 fmt++;
717 continue;
719 case 'n':
720 /* FIXME:
721 * What does C99 say about the overflow case here? */
722 if (qualifier == 'l') {
723 long * ip = va_arg(args, long *);
724 *ip = (str - buf);
725 } else if (qualifier == 'Z' || qualifier == 'z') {
726 size_t * ip = va_arg(args, size_t *);
727 *ip = (str - buf);
728 } else {
729 int * ip = va_arg(args, int *);
730 *ip = (str - buf);
732 continue;
734 case '%':
735 if (str < end)
736 *str = '%';
737 ++str;
738 continue;
740 /* integer number formats - set up the flags and "break" */
741 case 'o':
742 base = 8;
743 break;
745 case 'x':
746 flags |= SMALL;
747 case 'X':
748 base = 16;
749 break;
751 case 'd':
752 case 'i':
753 flags |= SIGN;
754 case 'u':
755 break;
757 default:
758 if (str < end)
759 *str = '%';
760 ++str;
761 if (*fmt) {
762 if (str < end)
763 *str = *fmt;
764 ++str;
765 } else {
766 --fmt;
768 continue;
770 if (qualifier == 'L')
771 num = va_arg(args, long long);
772 else if (qualifier == 'l') {
773 num = va_arg(args, unsigned long);
774 if (flags & SIGN)
775 num = (signed long) num;
776 } else if (qualifier == 'Z' || qualifier == 'z') {
777 num = va_arg(args, size_t);
778 } else if (qualifier == 't') {
779 num = va_arg(args, ptrdiff_t);
780 } else if (qualifier == 'h') {
781 num = (unsigned short) va_arg(args, int);
782 if (flags & SIGN)
783 num = (signed short) num;
784 } else {
785 num = va_arg(args, unsigned int);
786 if (flags & SIGN)
787 num = (signed int) num;
789 str = number(str, end, num, base,
790 field_width, precision, flags);
792 if (size > 0) {
793 if (str < end)
794 *str = '\0';
795 else
796 end[-1] = '\0';
798 /* the trailing null byte doesn't count towards the total */
799 return str-buf;
802 EXPORT_SYMBOL(vsnprintf);
805 * vscnprintf - Format a string and place it in a buffer
806 * @buf: The buffer to place the result into
807 * @size: The size of the buffer, including the trailing null space
808 * @fmt: The format string to use
809 * @args: Arguments for the format string
811 * The return value is the number of characters which have been written into
812 * the @buf not including the trailing '\0'. If @size is <= 0 the function
813 * returns 0.
815 * Call this function if you are already dealing with a va_list.
816 * You probably want scnprintf() instead.
818 * See the vsnprintf() documentation for format string extensions over C99.
820 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
822 int i;
824 i=vsnprintf(buf,size,fmt,args);
825 return (i >= size) ? (size - 1) : i;
828 EXPORT_SYMBOL(vscnprintf);
831 * snprintf - Format a string and place it in a buffer
832 * @buf: The buffer to place the result into
833 * @size: The size of the buffer, including the trailing null space
834 * @fmt: The format string to use
835 * @...: Arguments for the format string
837 * The return value is the number of characters which would be
838 * generated for the given input, excluding the trailing null,
839 * as per ISO C99. If the return is greater than or equal to
840 * @size, the resulting string is truncated.
842 * See the vsnprintf() documentation for format string extensions over C99.
844 int snprintf(char * buf, size_t size, const char *fmt, ...)
846 va_list args;
847 int i;
849 va_start(args, fmt);
850 i=vsnprintf(buf,size,fmt,args);
851 va_end(args);
852 return i;
855 EXPORT_SYMBOL(snprintf);
858 * scnprintf - Format a string and place it in a buffer
859 * @buf: The buffer to place the result into
860 * @size: The size of the buffer, including the trailing null space
861 * @fmt: The format string to use
862 * @...: Arguments for the format string
864 * The return value is the number of characters written into @buf not including
865 * the trailing '\0'. If @size is <= 0 the function returns 0.
868 int scnprintf(char * buf, size_t size, const char *fmt, ...)
870 va_list args;
871 int i;
873 va_start(args, fmt);
874 i = vsnprintf(buf, size, fmt, args);
875 va_end(args);
876 return (i >= size) ? (size - 1) : i;
878 EXPORT_SYMBOL(scnprintf);
881 * vsprintf - Format a string and place it in a buffer
882 * @buf: The buffer to place the result into
883 * @fmt: The format string to use
884 * @args: Arguments for the format string
886 * The function returns the number of characters written
887 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
888 * buffer overflows.
890 * Call this function if you are already dealing with a va_list.
891 * You probably want sprintf() instead.
893 * See the vsnprintf() documentation for format string extensions over C99.
895 int vsprintf(char *buf, const char *fmt, va_list args)
897 return vsnprintf(buf, INT_MAX, fmt, args);
900 EXPORT_SYMBOL(vsprintf);
903 * sprintf - Format a string and place it in a buffer
904 * @buf: The buffer to place the result into
905 * @fmt: The format string to use
906 * @...: Arguments for the format string
908 * The function returns the number of characters written
909 * into @buf. Use snprintf() or scnprintf() in order to avoid
910 * buffer overflows.
912 * See the vsnprintf() documentation for format string extensions over C99.
914 int sprintf(char * buf, const char *fmt, ...)
916 va_list args;
917 int i;
919 va_start(args, fmt);
920 i=vsnprintf(buf, INT_MAX, fmt, args);
921 va_end(args);
922 return i;
925 EXPORT_SYMBOL(sprintf);
928 * vsscanf - Unformat a buffer into a list of arguments
929 * @buf: input buffer
930 * @fmt: format of buffer
931 * @args: arguments
933 int vsscanf(const char * buf, const char * fmt, va_list args)
935 const char *str = buf;
936 char *next;
937 char digit;
938 int num = 0;
939 int qualifier;
940 int base;
941 int field_width;
942 int is_sign = 0;
944 while(*fmt && *str) {
945 /* skip any white space in format */
946 /* white space in format matchs any amount of
947 * white space, including none, in the input.
949 if (isspace(*fmt)) {
950 while (isspace(*fmt))
951 ++fmt;
952 while (isspace(*str))
953 ++str;
956 /* anything that is not a conversion must match exactly */
957 if (*fmt != '%' && *fmt) {
958 if (*fmt++ != *str++)
959 break;
960 continue;
963 if (!*fmt)
964 break;
965 ++fmt;
967 /* skip this conversion.
968 * advance both strings to next white space
970 if (*fmt == '*') {
971 while (!isspace(*fmt) && *fmt)
972 fmt++;
973 while (!isspace(*str) && *str)
974 str++;
975 continue;
978 /* get field width */
979 field_width = -1;
980 if (isdigit(*fmt))
981 field_width = skip_atoi(&fmt);
983 /* get conversion qualifier */
984 qualifier = -1;
985 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
986 *fmt == 'Z' || *fmt == 'z') {
987 qualifier = *fmt++;
988 if (unlikely(qualifier == *fmt)) {
989 if (qualifier == 'h') {
990 qualifier = 'H';
991 fmt++;
992 } else if (qualifier == 'l') {
993 qualifier = 'L';
994 fmt++;
998 base = 10;
999 is_sign = 0;
1001 if (!*fmt || !*str)
1002 break;
1004 switch(*fmt++) {
1005 case 'c':
1007 char *s = (char *) va_arg(args,char*);
1008 if (field_width == -1)
1009 field_width = 1;
1010 do {
1011 *s++ = *str++;
1012 } while (--field_width > 0 && *str);
1013 num++;
1015 continue;
1016 case 's':
1018 char *s = (char *) va_arg(args, char *);
1019 if(field_width == -1)
1020 field_width = INT_MAX;
1021 /* first, skip leading white space in buffer */
1022 while (isspace(*str))
1023 str++;
1025 /* now copy until next white space */
1026 while (*str && !isspace(*str) && field_width--) {
1027 *s++ = *str++;
1029 *s = '\0';
1030 num++;
1032 continue;
1033 case 'n':
1034 /* return number of characters read so far */
1036 int *i = (int *)va_arg(args,int*);
1037 *i = str - buf;
1039 continue;
1040 case 'o':
1041 base = 8;
1042 break;
1043 case 'x':
1044 case 'X':
1045 base = 16;
1046 break;
1047 case 'i':
1048 base = 0;
1049 case 'd':
1050 is_sign = 1;
1051 case 'u':
1052 break;
1053 case '%':
1054 /* looking for '%' in str */
1055 if (*str++ != '%')
1056 return num;
1057 continue;
1058 default:
1059 /* invalid format; stop here */
1060 return num;
1063 /* have some sort of integer conversion.
1064 * first, skip white space in buffer.
1066 while (isspace(*str))
1067 str++;
1069 digit = *str;
1070 if (is_sign && digit == '-')
1071 digit = *(str + 1);
1073 if (!digit
1074 || (base == 16 && !isxdigit(digit))
1075 || (base == 10 && !isdigit(digit))
1076 || (base == 8 && (!isdigit(digit) || digit > '7'))
1077 || (base == 0 && !isdigit(digit)))
1078 break;
1080 switch(qualifier) {
1081 case 'H': /* that's 'hh' in format */
1082 if (is_sign) {
1083 signed char *s = (signed char *) va_arg(args,signed char *);
1084 *s = (signed char) simple_strtol(str,&next,base);
1085 } else {
1086 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1087 *s = (unsigned char) simple_strtoul(str, &next, base);
1089 break;
1090 case 'h':
1091 if (is_sign) {
1092 short *s = (short *) va_arg(args,short *);
1093 *s = (short) simple_strtol(str,&next,base);
1094 } else {
1095 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1096 *s = (unsigned short) simple_strtoul(str, &next, base);
1098 break;
1099 case 'l':
1100 if (is_sign) {
1101 long *l = (long *) va_arg(args,long *);
1102 *l = simple_strtol(str,&next,base);
1103 } else {
1104 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1105 *l = simple_strtoul(str,&next,base);
1107 break;
1108 case 'L':
1109 if (is_sign) {
1110 long long *l = (long long*) va_arg(args,long long *);
1111 *l = simple_strtoll(str,&next,base);
1112 } else {
1113 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1114 *l = simple_strtoull(str,&next,base);
1116 break;
1117 case 'Z':
1118 case 'z':
1120 size_t *s = (size_t*) va_arg(args,size_t*);
1121 *s = (size_t) simple_strtoul(str,&next,base);
1123 break;
1124 default:
1125 if (is_sign) {
1126 int *i = (int *) va_arg(args, int*);
1127 *i = (int) simple_strtol(str,&next,base);
1128 } else {
1129 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1130 *i = (unsigned int) simple_strtoul(str,&next,base);
1132 break;
1134 num++;
1136 if (!next)
1137 break;
1138 str = next;
1142 * Now we've come all the way through so either the input string or the
1143 * format ended. In the former case, there can be a %n at the current
1144 * position in the format that needs to be filled.
1146 if (*fmt == '%' && *(fmt + 1) == 'n') {
1147 int *p = (int *)va_arg(args, int *);
1148 *p = str - buf;
1151 return num;
1154 EXPORT_SYMBOL(vsscanf);
1157 * sscanf - Unformat a buffer into a list of arguments
1158 * @buf: input buffer
1159 * @fmt: formatting of buffer
1160 * @...: resulting arguments
1162 int sscanf(const char * buf, const char * fmt, ...)
1164 va_list args;
1165 int i;
1167 va_start(args,fmt);
1168 i = vsscanf(buf,fmt,args);
1169 va_end(args);
1170 return i;
1173 EXPORT_SYMBOL(sscanf);