lib: trivial whitespace tidy
[linux-2.6/mini2440.git] / lib / vsprintf.c
blob0ddf928fe4d5bf4efbad83f69b382db9576a509c
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);
91 EXPORT_SYMBOL(simple_strtol);
93 /**
94 * simple_strtoull - convert a string to an unsigned long long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
99 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
101 unsigned long long result = 0;
103 if (!base)
104 base = simple_guess_base(cp);
106 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
107 cp += 2;
109 while (isxdigit(*cp)) {
110 unsigned int value;
112 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
113 if (value >= base)
114 break;
115 result = result * base + value;
116 cp++;
119 if (endp)
120 *endp = (char *)cp;
121 return result;
123 EXPORT_SYMBOL(simple_strtoull);
126 * simple_strtoll - convert a string to a signed long long
127 * @cp: The start of the string
128 * @endp: A pointer to the end of the parsed string will be placed here
129 * @base: The number base to use
131 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
133 if(*cp=='-')
134 return -simple_strtoull(cp + 1, endp, base);
135 return simple_strtoull(cp, endp, base);
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 * echo will append a newline to the tail.
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
161 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
164 * strict_strtol - convert a string to a long strictly
165 * @cp: The string to be converted
166 * @base: The number base to use
167 * @res: The converted result value
169 * strict_strtol is similiar to strict_strtoul, but it allows the first
170 * character of a string is '-'.
172 * It returns 0 if conversion is successful and *res is set to the converted
173 * value, otherwise it returns -EINVAL and *res is set to 0.
175 int strict_strtol(const char *cp, unsigned int base, long *res);
178 * strict_strtoull - convert a string to an unsigned long long strictly
179 * @cp: The string to be converted
180 * @base: The number base to use
181 * @res: The converted result value
183 * strict_strtoull converts a string to an unsigned long long only if the
184 * string is really an unsigned long long string, any string containing
185 * any invalid char at the tail will be rejected and -EINVAL is returned,
186 * only a newline char at the tail is acceptible because people generally
187 * change a module parameter in the following way:
189 * echo 1024 > /sys/module/e1000/parameters/copybreak
191 * echo will append a newline to the tail of the string.
193 * It returns 0 if conversion is successful and *res is set to the converted
194 * value, otherwise it returns -EINVAL and *res is set to 0.
196 * simple_strtoull just ignores the successive invalid characters and
197 * return the converted value of prefix part of the string.
199 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
202 * strict_strtoll - convert a string to a long long strictly
203 * @cp: The string to be converted
204 * @base: The number base to use
205 * @res: The converted result value
207 * strict_strtoll is similiar to strict_strtoull, but it allows the first
208 * character of a string is '-'.
210 * It returns 0 if conversion is successful and *res is set to the converted
211 * value, otherwise it returns -EINVAL and *res is set to 0.
213 int strict_strtoll(const char *cp, unsigned int base, long long *res);
215 #define define_strict_strtoux(type, valtype) \
216 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
218 char *tail; \
219 valtype val; \
220 size_t len; \
222 *res = 0; \
223 len = strlen(cp); \
224 if (len == 0) \
225 return -EINVAL; \
227 val = simple_strtou##type(cp, &tail, base); \
228 if ((*tail == '\0') || \
229 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
230 *res = val; \
231 return 0; \
234 return -EINVAL; \
237 #define define_strict_strtox(type, valtype) \
238 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
240 int ret; \
241 if (*cp == '-') { \
242 ret = strict_strtou##type(cp+1, base, res); \
243 if (!ret) \
244 *res = -(*res); \
245 } else \
246 ret = strict_strtou##type(cp, base, res); \
248 return ret; \
251 define_strict_strtoux(l, unsigned long)
252 define_strict_strtox(l, long)
253 define_strict_strtoux(ll, unsigned long long)
254 define_strict_strtox(ll, long long)
256 EXPORT_SYMBOL(strict_strtoul);
257 EXPORT_SYMBOL(strict_strtol);
258 EXPORT_SYMBOL(strict_strtoll);
259 EXPORT_SYMBOL(strict_strtoull);
261 static int skip_atoi(const char **s)
263 int i=0;
265 while (isdigit(**s))
266 i = i*10 + *((*s)++) - '0';
267 return i;
270 /* Decimal conversion is by far the most typical, and is used
271 * for /proc and /sys data. This directly impacts e.g. top performance
272 * with many processes running. We optimize it for speed
273 * using code from
274 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
275 * (with permission from the author, Douglas W. Jones). */
277 /* Formats correctly any integer in [0,99999].
278 * Outputs from one to five digits depending on input.
279 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
280 static char* put_dec_trunc(char *buf, unsigned q)
282 unsigned d3, d2, d1, d0;
283 d1 = (q>>4) & 0xf;
284 d2 = (q>>8) & 0xf;
285 d3 = (q>>12);
287 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
288 q = (d0 * 0xcd) >> 11;
289 d0 = d0 - 10*q;
290 *buf++ = d0 + '0'; /* least significant digit */
291 d1 = q + 9*d3 + 5*d2 + d1;
292 if (d1 != 0) {
293 q = (d1 * 0xcd) >> 11;
294 d1 = d1 - 10*q;
295 *buf++ = d1 + '0'; /* next digit */
297 d2 = q + 2*d2;
298 if ((d2 != 0) || (d3 != 0)) {
299 q = (d2 * 0xd) >> 7;
300 d2 = d2 - 10*q;
301 *buf++ = d2 + '0'; /* next digit */
303 d3 = q + 4*d3;
304 if (d3 != 0) {
305 q = (d3 * 0xcd) >> 11;
306 d3 = d3 - 10*q;
307 *buf++ = d3 + '0'; /* next digit */
308 if (q != 0)
309 *buf++ = q + '0'; /* most sign. digit */
313 return buf;
315 /* Same with if's removed. Always emits five digits */
316 static char* put_dec_full(char *buf, unsigned q)
318 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
319 /* but anyway, gcc produces better code with full-sized ints */
320 unsigned d3, d2, d1, d0;
321 d1 = (q>>4) & 0xf;
322 d2 = (q>>8) & 0xf;
323 d3 = (q>>12);
325 /* Possible ways to approx. divide by 10 */
326 /* gcc -O2 replaces multiply with shifts and adds */
327 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
328 // (x * 0x67) >> 10: 1100111
329 // (x * 0x34) >> 9: 110100 - same
330 // (x * 0x1a) >> 8: 11010 - same
331 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
333 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
334 q = (d0 * 0xcd) >> 11;
335 d0 = d0 - 10*q;
336 *buf++ = d0 + '0';
337 d1 = q + 9*d3 + 5*d2 + d1;
338 q = (d1 * 0xcd) >> 11;
339 d1 = d1 - 10*q;
340 *buf++ = d1 + '0';
342 d2 = q + 2*d2;
343 q = (d2 * 0xd) >> 7;
344 d2 = d2 - 10*q;
345 *buf++ = d2 + '0';
347 d3 = q + 4*d3;
348 q = (d3 * 0xcd) >> 11; /* - shorter code */
349 /* q = (d3 * 0x67) >> 10; - would also work */
350 d3 = d3 - 10*q;
351 *buf++ = d3 + '0';
352 *buf++ = q + '0';
353 return buf;
355 /* No inlining helps gcc to use registers better */
356 static noinline char* put_dec(char *buf, unsigned long long num)
358 while (1) {
359 unsigned rem;
360 if (num < 100000)
361 return put_dec_trunc(buf, num);
362 rem = do_div(num, 100000);
363 buf = put_dec_full(buf, rem);
367 #define ZEROPAD 1 /* pad with zero */
368 #define SIGN 2 /* unsigned/signed long */
369 #define PLUS 4 /* show plus */
370 #define SPACE 8 /* space if plus */
371 #define LEFT 16 /* left justified */
372 #define SMALL 32 /* Must be 32 == 0x20 */
373 #define SPECIAL 64 /* 0x */
375 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
377 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
378 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
380 char tmp[66];
381 char sign;
382 char locase;
383 int need_pfx = ((type & SPECIAL) && base != 10);
384 int i;
386 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
387 * produces same digits or (maybe lowercased) letters */
388 locase = (type & SMALL);
389 if (type & LEFT)
390 type &= ~ZEROPAD;
391 sign = 0;
392 if (type & SIGN) {
393 if ((signed long long) num < 0) {
394 sign = '-';
395 num = - (signed long long) num;
396 size--;
397 } else if (type & PLUS) {
398 sign = '+';
399 size--;
400 } else if (type & SPACE) {
401 sign = ' ';
402 size--;
405 if (need_pfx) {
406 size--;
407 if (base == 16)
408 size--;
411 /* generate full string in tmp[], in reverse order */
412 i = 0;
413 if (num == 0)
414 tmp[i++] = '0';
415 /* Generic code, for any base:
416 else do {
417 tmp[i++] = (digits[do_div(num,base)] | locase);
418 } while (num != 0);
420 else if (base != 10) { /* 8 or 16 */
421 int mask = base - 1;
422 int shift = 3;
423 if (base == 16) shift = 4;
424 do {
425 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
426 num >>= shift;
427 } while (num);
428 } else { /* base 10 */
429 i = put_dec(tmp, num) - tmp;
432 /* printing 100 using %2d gives "100", not "00" */
433 if (i > precision)
434 precision = i;
435 /* leading space padding */
436 size -= precision;
437 if (!(type & (ZEROPAD+LEFT))) {
438 while(--size >= 0) {
439 if (buf < end)
440 *buf = ' ';
441 ++buf;
444 /* sign */
445 if (sign) {
446 if (buf < end)
447 *buf = sign;
448 ++buf;
450 /* "0x" / "0" prefix */
451 if (need_pfx) {
452 if (buf < end)
453 *buf = '0';
454 ++buf;
455 if (base == 16) {
456 if (buf < end)
457 *buf = ('X' | locase);
458 ++buf;
461 /* zero or space padding */
462 if (!(type & LEFT)) {
463 char c = (type & ZEROPAD) ? '0' : ' ';
464 while (--size >= 0) {
465 if (buf < end)
466 *buf = c;
467 ++buf;
470 /* hmm even more zero padding? */
471 while (i <= --precision) {
472 if (buf < end)
473 *buf = '0';
474 ++buf;
476 /* actual digits of result */
477 while (--i >= 0) {
478 if (buf < end)
479 *buf = tmp[i];
480 ++buf;
482 /* trailing space padding */
483 while (--size >= 0) {
484 if (buf < end)
485 *buf = ' ';
486 ++buf;
488 return buf;
491 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
493 int len, i;
495 if ((unsigned long)s < PAGE_SIZE)
496 s = "<NULL>";
498 len = strnlen(s, precision);
500 if (!(flags & LEFT)) {
501 while (len < field_width--) {
502 if (buf < end)
503 *buf = ' ';
504 ++buf;
507 for (i = 0; i < len; ++i) {
508 if (buf < end)
509 *buf = *s;
510 ++buf; ++s;
512 while (len < field_width--) {
513 if (buf < end)
514 *buf = ' ';
515 ++buf;
517 return buf;
520 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
522 unsigned long value = (unsigned long) ptr;
523 #ifdef CONFIG_KALLSYMS
524 char sym[KSYM_SYMBOL_LEN];
525 sprint_symbol(sym, value);
526 return string(buf, end, sym, field_width, precision, flags);
527 #else
528 field_width = 2*sizeof(void *);
529 flags |= SPECIAL | SMALL | ZEROPAD;
530 return number(buf, end, value, 16, field_width, precision, flags);
531 #endif
535 * Show a '%p' thing. A kernel extension is that the '%p' is followed
536 * by an extra set of alphanumeric characters that are extended format
537 * specifiers.
539 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
540 * and 'S' (for Symbolic direct pointers), but this can easily be
541 * extended in the future (network address types etc).
543 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
544 * pointers are really function descriptors, which contain a pointer the
545 * real address.
547 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
549 switch (*fmt) {
550 case 'F':
551 ptr = dereference_function_descriptor(ptr);
552 /* Fallthrough */
553 case 'S':
554 return symbol_string(buf, end, ptr, field_width, precision, flags);
556 flags |= SMALL;
557 if (field_width == -1) {
558 field_width = 2*sizeof(void *);
559 flags |= ZEROPAD;
561 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
565 * vsnprintf - Format a string and place it in a buffer
566 * @buf: The buffer to place the result into
567 * @size: The size of the buffer, including the trailing null space
568 * @fmt: The format string to use
569 * @args: Arguments for the format string
571 * This function follows C99 vsnprintf, but has some extensions:
572 * %pS output the name of a text symbol
573 * %pF output the name of a function pointer
575 * The return value is the number of characters which would
576 * be generated for the given input, excluding the trailing
577 * '\0', as per ISO C99. If you want to have the exact
578 * number of characters written into @buf as return value
579 * (not including the trailing '\0'), use vscnprintf(). If the
580 * return is greater than or equal to @size, the resulting
581 * string is truncated.
583 * Call this function if you are already dealing with a va_list.
584 * You probably want snprintf() instead.
586 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
588 unsigned long long num;
589 int base;
590 char *str, *end, c;
592 int flags; /* flags to number() */
594 int field_width; /* width of output field */
595 int precision; /* min. # of digits for integers; max
596 number of chars for from string */
597 int qualifier; /* 'h', 'l', or 'L' for integer fields */
598 /* 'z' support added 23/7/1999 S.H. */
599 /* 'z' changed to 'Z' --davidm 1/25/99 */
600 /* 't' added for ptrdiff_t */
602 /* Reject out-of-range values early. Large positive sizes are
603 used for unknown buffer sizes. */
604 if (unlikely((int) size < 0)) {
605 /* There can be only one.. */
606 static char warn = 1;
607 WARN_ON(warn);
608 warn = 0;
609 return 0;
612 str = buf;
613 end = buf + size;
615 /* Make sure end is always >= buf */
616 if (end < buf) {
617 end = ((void *)-1);
618 size = end - buf;
621 for (; *fmt ; ++fmt) {
622 if (*fmt != '%') {
623 if (str < end)
624 *str = *fmt;
625 ++str;
626 continue;
629 /* process flags */
630 flags = 0;
631 repeat:
632 ++fmt; /* this also skips first '%' */
633 switch (*fmt) {
634 case '-': flags |= LEFT; goto repeat;
635 case '+': flags |= PLUS; goto repeat;
636 case ' ': flags |= SPACE; goto repeat;
637 case '#': flags |= SPECIAL; goto repeat;
638 case '0': flags |= ZEROPAD; goto repeat;
641 /* get field width */
642 field_width = -1;
643 if (isdigit(*fmt))
644 field_width = skip_atoi(&fmt);
645 else if (*fmt == '*') {
646 ++fmt;
647 /* it's the next argument */
648 field_width = va_arg(args, int);
649 if (field_width < 0) {
650 field_width = -field_width;
651 flags |= LEFT;
655 /* get the precision */
656 precision = -1;
657 if (*fmt == '.') {
658 ++fmt;
659 if (isdigit(*fmt))
660 precision = skip_atoi(&fmt);
661 else if (*fmt == '*') {
662 ++fmt;
663 /* it's the next argument */
664 precision = va_arg(args, int);
666 if (precision < 0)
667 precision = 0;
670 /* get the conversion qualifier */
671 qualifier = -1;
672 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
673 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
674 qualifier = *fmt;
675 ++fmt;
676 if (qualifier == 'l' && *fmt == 'l') {
677 qualifier = 'L';
678 ++fmt;
682 /* default base */
683 base = 10;
685 switch (*fmt) {
686 case 'c':
687 if (!(flags & LEFT)) {
688 while (--field_width > 0) {
689 if (str < end)
690 *str = ' ';
691 ++str;
694 c = (unsigned char) va_arg(args, int);
695 if (str < end)
696 *str = c;
697 ++str;
698 while (--field_width > 0) {
699 if (str < end)
700 *str = ' ';
701 ++str;
703 continue;
705 case 's':
706 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
707 continue;
709 case 'p':
710 str = pointer(fmt+1, str, end,
711 va_arg(args, void *),
712 field_width, precision, flags);
713 /* Skip all alphanumeric pointer suffixes */
714 while (isalnum(fmt[1]))
715 fmt++;
716 continue;
718 case 'n':
719 /* FIXME:
720 * What does C99 say about the overflow case here? */
721 if (qualifier == 'l') {
722 long * ip = va_arg(args, long *);
723 *ip = (str - buf);
724 } else if (qualifier == 'Z' || qualifier == 'z') {
725 size_t * ip = va_arg(args, size_t *);
726 *ip = (str - buf);
727 } else {
728 int * ip = va_arg(args, int *);
729 *ip = (str - buf);
731 continue;
733 case '%':
734 if (str < end)
735 *str = '%';
736 ++str;
737 continue;
739 /* integer number formats - set up the flags and "break" */
740 case 'o':
741 base = 8;
742 break;
744 case 'x':
745 flags |= SMALL;
746 case 'X':
747 base = 16;
748 break;
750 case 'd':
751 case 'i':
752 flags |= SIGN;
753 case 'u':
754 break;
756 default:
757 if (str < end)
758 *str = '%';
759 ++str;
760 if (*fmt) {
761 if (str < end)
762 *str = *fmt;
763 ++str;
764 } else {
765 --fmt;
767 continue;
769 if (qualifier == 'L')
770 num = va_arg(args, long long);
771 else if (qualifier == 'l') {
772 num = va_arg(args, unsigned long);
773 if (flags & SIGN)
774 num = (signed long) num;
775 } else if (qualifier == 'Z' || qualifier == 'z') {
776 num = va_arg(args, size_t);
777 } else if (qualifier == 't') {
778 num = va_arg(args, ptrdiff_t);
779 } else if (qualifier == 'h') {
780 num = (unsigned short) va_arg(args, int);
781 if (flags & SIGN)
782 num = (signed short) num;
783 } else {
784 num = va_arg(args, unsigned int);
785 if (flags & SIGN)
786 num = (signed int) num;
788 str = number(str, end, num, base,
789 field_width, precision, flags);
791 if (size > 0) {
792 if (str < end)
793 *str = '\0';
794 else
795 end[-1] = '\0';
797 /* the trailing null byte doesn't count towards the total */
798 return str-buf;
800 EXPORT_SYMBOL(vsnprintf);
803 * vscnprintf - Format a string and place it in a buffer
804 * @buf: The buffer to place the result into
805 * @size: The size of the buffer, including the trailing null space
806 * @fmt: The format string to use
807 * @args: Arguments for the format string
809 * The return value is the number of characters which have been written into
810 * the @buf not including the trailing '\0'. If @size is <= 0 the function
811 * returns 0.
813 * Call this function if you are already dealing with a va_list.
814 * You probably want scnprintf() instead.
816 * See the vsnprintf() documentation for format string extensions over C99.
818 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
820 int i;
822 i=vsnprintf(buf,size,fmt,args);
823 return (i >= size) ? (size - 1) : i;
825 EXPORT_SYMBOL(vscnprintf);
828 * snprintf - Format a string and place it in a buffer
829 * @buf: The buffer to place the result into
830 * @size: The size of the buffer, including the trailing null space
831 * @fmt: The format string to use
832 * @...: Arguments for the format string
834 * The return value is the number of characters which would be
835 * generated for the given input, excluding the trailing null,
836 * as per ISO C99. If the return is greater than or equal to
837 * @size, the resulting string is truncated.
839 * See the vsnprintf() documentation for format string extensions over C99.
841 int snprintf(char * buf, size_t size, const char *fmt, ...)
843 va_list args;
844 int i;
846 va_start(args, fmt);
847 i=vsnprintf(buf,size,fmt,args);
848 va_end(args);
849 return i;
851 EXPORT_SYMBOL(snprintf);
854 * scnprintf - Format a string and place it in a buffer
855 * @buf: The buffer to place the result into
856 * @size: The size of the buffer, including the trailing null space
857 * @fmt: The format string to use
858 * @...: Arguments for the format string
860 * The return value is the number of characters written into @buf not including
861 * the trailing '\0'. If @size is <= 0 the function returns 0.
864 int scnprintf(char * buf, size_t size, const char *fmt, ...)
866 va_list args;
867 int i;
869 va_start(args, fmt);
870 i = vsnprintf(buf, size, fmt, args);
871 va_end(args);
872 return (i >= size) ? (size - 1) : i;
874 EXPORT_SYMBOL(scnprintf);
877 * vsprintf - Format a string and place it in a buffer
878 * @buf: The buffer to place the result into
879 * @fmt: The format string to use
880 * @args: Arguments for the format string
882 * The function returns the number of characters written
883 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
884 * buffer overflows.
886 * Call this function if you are already dealing with a va_list.
887 * You probably want sprintf() instead.
889 * See the vsnprintf() documentation for format string extensions over C99.
891 int vsprintf(char *buf, const char *fmt, va_list args)
893 return vsnprintf(buf, INT_MAX, fmt, args);
895 EXPORT_SYMBOL(vsprintf);
898 * sprintf - Format a string and place it in a buffer
899 * @buf: The buffer to place the result into
900 * @fmt: The format string to use
901 * @...: Arguments for the format string
903 * The function returns the number of characters written
904 * into @buf. Use snprintf() or scnprintf() in order to avoid
905 * buffer overflows.
907 * See the vsnprintf() documentation for format string extensions over C99.
909 int sprintf(char * buf, const char *fmt, ...)
911 va_list args;
912 int i;
914 va_start(args, fmt);
915 i=vsnprintf(buf, INT_MAX, fmt, args);
916 va_end(args);
917 return i;
919 EXPORT_SYMBOL(sprintf);
922 * vsscanf - Unformat a buffer into a list of arguments
923 * @buf: input buffer
924 * @fmt: format of buffer
925 * @args: arguments
927 int vsscanf(const char * buf, const char * fmt, va_list args)
929 const char *str = buf;
930 char *next;
931 char digit;
932 int num = 0;
933 int qualifier;
934 int base;
935 int field_width;
936 int is_sign = 0;
938 while(*fmt && *str) {
939 /* skip any white space in format */
940 /* white space in format matchs any amount of
941 * white space, including none, in the input.
943 if (isspace(*fmt)) {
944 while (isspace(*fmt))
945 ++fmt;
946 while (isspace(*str))
947 ++str;
950 /* anything that is not a conversion must match exactly */
951 if (*fmt != '%' && *fmt) {
952 if (*fmt++ != *str++)
953 break;
954 continue;
957 if (!*fmt)
958 break;
959 ++fmt;
961 /* skip this conversion.
962 * advance both strings to next white space
964 if (*fmt == '*') {
965 while (!isspace(*fmt) && *fmt)
966 fmt++;
967 while (!isspace(*str) && *str)
968 str++;
969 continue;
972 /* get field width */
973 field_width = -1;
974 if (isdigit(*fmt))
975 field_width = skip_atoi(&fmt);
977 /* get conversion qualifier */
978 qualifier = -1;
979 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
980 *fmt == 'Z' || *fmt == 'z') {
981 qualifier = *fmt++;
982 if (unlikely(qualifier == *fmt)) {
983 if (qualifier == 'h') {
984 qualifier = 'H';
985 fmt++;
986 } else if (qualifier == 'l') {
987 qualifier = 'L';
988 fmt++;
992 base = 10;
993 is_sign = 0;
995 if (!*fmt || !*str)
996 break;
998 switch(*fmt++) {
999 case 'c':
1001 char *s = (char *) va_arg(args,char*);
1002 if (field_width == -1)
1003 field_width = 1;
1004 do {
1005 *s++ = *str++;
1006 } while (--field_width > 0 && *str);
1007 num++;
1009 continue;
1010 case 's':
1012 char *s = (char *) va_arg(args, char *);
1013 if(field_width == -1)
1014 field_width = INT_MAX;
1015 /* first, skip leading white space in buffer */
1016 while (isspace(*str))
1017 str++;
1019 /* now copy until next white space */
1020 while (*str && !isspace(*str) && field_width--) {
1021 *s++ = *str++;
1023 *s = '\0';
1024 num++;
1026 continue;
1027 case 'n':
1028 /* return number of characters read so far */
1030 int *i = (int *)va_arg(args,int*);
1031 *i = str - buf;
1033 continue;
1034 case 'o':
1035 base = 8;
1036 break;
1037 case 'x':
1038 case 'X':
1039 base = 16;
1040 break;
1041 case 'i':
1042 base = 0;
1043 case 'd':
1044 is_sign = 1;
1045 case 'u':
1046 break;
1047 case '%':
1048 /* looking for '%' in str */
1049 if (*str++ != '%')
1050 return num;
1051 continue;
1052 default:
1053 /* invalid format; stop here */
1054 return num;
1057 /* have some sort of integer conversion.
1058 * first, skip white space in buffer.
1060 while (isspace(*str))
1061 str++;
1063 digit = *str;
1064 if (is_sign && digit == '-')
1065 digit = *(str + 1);
1067 if (!digit
1068 || (base == 16 && !isxdigit(digit))
1069 || (base == 10 && !isdigit(digit))
1070 || (base == 8 && (!isdigit(digit) || digit > '7'))
1071 || (base == 0 && !isdigit(digit)))
1072 break;
1074 switch(qualifier) {
1075 case 'H': /* that's 'hh' in format */
1076 if (is_sign) {
1077 signed char *s = (signed char *) va_arg(args,signed char *);
1078 *s = (signed char) simple_strtol(str,&next,base);
1079 } else {
1080 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1081 *s = (unsigned char) simple_strtoul(str, &next, base);
1083 break;
1084 case 'h':
1085 if (is_sign) {
1086 short *s = (short *) va_arg(args,short *);
1087 *s = (short) simple_strtol(str,&next,base);
1088 } else {
1089 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1090 *s = (unsigned short) simple_strtoul(str, &next, base);
1092 break;
1093 case 'l':
1094 if (is_sign) {
1095 long *l = (long *) va_arg(args,long *);
1096 *l = simple_strtol(str,&next,base);
1097 } else {
1098 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1099 *l = simple_strtoul(str,&next,base);
1101 break;
1102 case 'L':
1103 if (is_sign) {
1104 long long *l = (long long*) va_arg(args,long long *);
1105 *l = simple_strtoll(str,&next,base);
1106 } else {
1107 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1108 *l = simple_strtoull(str,&next,base);
1110 break;
1111 case 'Z':
1112 case 'z':
1114 size_t *s = (size_t*) va_arg(args,size_t*);
1115 *s = (size_t) simple_strtoul(str,&next,base);
1117 break;
1118 default:
1119 if (is_sign) {
1120 int *i = (int *) va_arg(args, int*);
1121 *i = (int) simple_strtol(str,&next,base);
1122 } else {
1123 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1124 *i = (unsigned int) simple_strtoul(str,&next,base);
1126 break;
1128 num++;
1130 if (!next)
1131 break;
1132 str = next;
1136 * Now we've come all the way through so either the input string or the
1137 * format ended. In the former case, there can be a %n at the current
1138 * position in the format that needs to be filled.
1140 if (*fmt == '%' && *(fmt + 1) == 'n') {
1141 int *p = (int *)va_arg(args, int *);
1142 *p = str - buf;
1145 return num;
1147 EXPORT_SYMBOL(vsscanf);
1150 * sscanf - Unformat a buffer into a list of arguments
1151 * @buf: input buffer
1152 * @fmt: formatting of buffer
1153 * @...: resulting arguments
1155 int sscanf(const char * buf, const char * fmt, ...)
1157 va_list args;
1158 int i;
1160 va_start(args,fmt);
1161 i = vsscanf(buf,fmt,args);
1162 va_end(args);
1163 return i;
1165 EXPORT_SYMBOL(sscanf);