[CPUFREQ] acpi-cpufreq: add error handling for cpufreq_register_driver() error
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / vsprintf.c
blobc399bc1093cbedb276f3e974353534560197e940
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 /**
36 * simple_strtoul - convert a string to an unsigned long
37 * @cp: The start of the string
38 * @endp: A pointer to the end of the parsed string will be placed here
39 * @base: The number base to use
41 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
43 unsigned long result = 0,value;
45 if (!base) {
46 base = 10;
47 if (*cp == '0') {
48 base = 8;
49 cp++;
50 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
51 cp++;
52 base = 16;
55 } else if (base == 16) {
56 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
57 cp += 2;
59 while (isxdigit(*cp) &&
60 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
61 result = result*base + value;
62 cp++;
64 if (endp)
65 *endp = (char *)cp;
66 return result;
69 EXPORT_SYMBOL(simple_strtoul);
71 /**
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
77 long simple_strtol(const char *cp,char **endp,unsigned int base)
79 if(*cp=='-')
80 return -simple_strtoul(cp+1,endp,base);
81 return simple_strtoul(cp,endp,base);
84 EXPORT_SYMBOL(simple_strtol);
86 /**
87 * simple_strtoull - convert a string to an unsigned long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
92 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
94 unsigned long long result = 0,value;
96 if (!base) {
97 base = 10;
98 if (*cp == '0') {
99 base = 8;
100 cp++;
101 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
102 cp++;
103 base = 16;
106 } else if (base == 16) {
107 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
110 while (isxdigit(*cp)
111 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
112 result = result*base + value;
113 cp++;
115 if (endp)
116 *endp = (char *)cp;
117 return result;
120 EXPORT_SYMBOL(simple_strtoull);
123 * simple_strtoll - convert a string to a signed long long
124 * @cp: The start of the string
125 * @endp: A pointer to the end of the parsed string will be placed here
126 * @base: The number base to use
128 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
130 if(*cp=='-')
131 return -simple_strtoull(cp+1,endp,base);
132 return simple_strtoull(cp,endp,base);
137 * strict_strtoul - convert a string to an unsigned long strictly
138 * @cp: The string to be converted
139 * @base: The number base to use
140 * @res: The converted result value
142 * strict_strtoul converts a string to an unsigned long only if the
143 * string is really an unsigned long string, any string containing
144 * any invalid char at the tail will be rejected and -EINVAL is returned,
145 * only a newline char at the tail is acceptible because people generally
146 * change a module parameter in the following way:
148 * echo 1024 > /sys/module/e1000/parameters/copybreak
150 * echo will append a newline to the tail.
152 * It returns 0 if conversion is successful and *res is set to the converted
153 * value, otherwise it returns -EINVAL and *res is set to 0.
155 * simple_strtoul just ignores the successive invalid characters and
156 * return the converted value of prefix part of the string.
158 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
161 * strict_strtol - convert a string to a long strictly
162 * @cp: The string to be converted
163 * @base: The number base to use
164 * @res: The converted result value
166 * strict_strtol is similiar to strict_strtoul, but it allows the first
167 * character of a string is '-'.
169 * It returns 0 if conversion is successful and *res is set to the converted
170 * value, otherwise it returns -EINVAL and *res is set to 0.
172 int strict_strtol(const char *cp, unsigned int base, long *res);
175 * strict_strtoull - convert a string to an unsigned long long strictly
176 * @cp: The string to be converted
177 * @base: The number base to use
178 * @res: The converted result value
180 * strict_strtoull converts a string to an unsigned long long only if the
181 * string is really an unsigned long long string, any string containing
182 * any invalid char at the tail will be rejected and -EINVAL is returned,
183 * only a newline char at the tail is acceptible because people generally
184 * change a module parameter in the following way:
186 * echo 1024 > /sys/module/e1000/parameters/copybreak
188 * echo will append a newline to the tail of the string.
190 * It returns 0 if conversion is successful and *res is set to the converted
191 * value, otherwise it returns -EINVAL and *res is set to 0.
193 * simple_strtoull just ignores the successive invalid characters and
194 * return the converted value of prefix part of the string.
196 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
199 * strict_strtoll - convert a string to a long long strictly
200 * @cp: The string to be converted
201 * @base: The number base to use
202 * @res: The converted result value
204 * strict_strtoll is similiar to strict_strtoull, but it allows the first
205 * character of a string is '-'.
207 * It returns 0 if conversion is successful and *res is set to the converted
208 * value, otherwise it returns -EINVAL and *res is set to 0.
210 int strict_strtoll(const char *cp, unsigned int base, long long *res);
212 #define define_strict_strtoux(type, valtype) \
213 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
215 char *tail; \
216 valtype val; \
217 size_t len; \
219 *res = 0; \
220 len = strlen(cp); \
221 if (len == 0) \
222 return -EINVAL; \
224 val = simple_strtou##type(cp, &tail, base); \
225 if ((*tail == '\0') || \
226 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
227 *res = val; \
228 return 0; \
231 return -EINVAL; \
234 #define define_strict_strtox(type, valtype) \
235 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
237 int ret; \
238 if (*cp == '-') { \
239 ret = strict_strtou##type(cp+1, base, res); \
240 if (!ret) \
241 *res = -(*res); \
242 } else \
243 ret = strict_strtou##type(cp, base, res); \
245 return ret; \
248 define_strict_strtoux(l, unsigned long)
249 define_strict_strtox(l, long)
250 define_strict_strtoux(ll, unsigned long long)
251 define_strict_strtox(ll, long long)
253 EXPORT_SYMBOL(strict_strtoul);
254 EXPORT_SYMBOL(strict_strtol);
255 EXPORT_SYMBOL(strict_strtoll);
256 EXPORT_SYMBOL(strict_strtoull);
258 static int skip_atoi(const char **s)
260 int i=0;
262 while (isdigit(**s))
263 i = i*10 + *((*s)++) - '0';
264 return i;
267 /* Decimal conversion is by far the most typical, and is used
268 * for /proc and /sys data. This directly impacts e.g. top performance
269 * with many processes running. We optimize it for speed
270 * using code from
271 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
272 * (with permission from the author, Douglas W. Jones). */
274 /* Formats correctly any integer in [0,99999].
275 * Outputs from one to five digits depending on input.
276 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
277 static char* put_dec_trunc(char *buf, unsigned q)
279 unsigned d3, d2, d1, d0;
280 d1 = (q>>4) & 0xf;
281 d2 = (q>>8) & 0xf;
282 d3 = (q>>12);
284 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
285 q = (d0 * 0xcd) >> 11;
286 d0 = d0 - 10*q;
287 *buf++ = d0 + '0'; /* least significant digit */
288 d1 = q + 9*d3 + 5*d2 + d1;
289 if (d1 != 0) {
290 q = (d1 * 0xcd) >> 11;
291 d1 = d1 - 10*q;
292 *buf++ = d1 + '0'; /* next digit */
294 d2 = q + 2*d2;
295 if ((d2 != 0) || (d3 != 0)) {
296 q = (d2 * 0xd) >> 7;
297 d2 = d2 - 10*q;
298 *buf++ = d2 + '0'; /* next digit */
300 d3 = q + 4*d3;
301 if (d3 != 0) {
302 q = (d3 * 0xcd) >> 11;
303 d3 = d3 - 10*q;
304 *buf++ = d3 + '0'; /* next digit */
305 if (q != 0)
306 *buf++ = q + '0'; /* most sign. digit */
310 return buf;
312 /* Same with if's removed. Always emits five digits */
313 static char* put_dec_full(char *buf, unsigned q)
315 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
316 /* but anyway, gcc produces better code with full-sized ints */
317 unsigned d3, d2, d1, d0;
318 d1 = (q>>4) & 0xf;
319 d2 = (q>>8) & 0xf;
320 d3 = (q>>12);
322 /* Possible ways to approx. divide by 10 */
323 /* gcc -O2 replaces multiply with shifts and adds */
324 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
325 // (x * 0x67) >> 10: 1100111
326 // (x * 0x34) >> 9: 110100 - same
327 // (x * 0x1a) >> 8: 11010 - same
328 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
330 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
331 q = (d0 * 0xcd) >> 11;
332 d0 = d0 - 10*q;
333 *buf++ = d0 + '0';
334 d1 = q + 9*d3 + 5*d2 + d1;
335 q = (d1 * 0xcd) >> 11;
336 d1 = d1 - 10*q;
337 *buf++ = d1 + '0';
339 d2 = q + 2*d2;
340 q = (d2 * 0xd) >> 7;
341 d2 = d2 - 10*q;
342 *buf++ = d2 + '0';
344 d3 = q + 4*d3;
345 q = (d3 * 0xcd) >> 11; /* - shorter code */
346 /* q = (d3 * 0x67) >> 10; - would also work */
347 d3 = d3 - 10*q;
348 *buf++ = d3 + '0';
349 *buf++ = q + '0';
350 return buf;
352 /* No inlining helps gcc to use registers better */
353 static noinline char* put_dec(char *buf, unsigned long long num)
355 while (1) {
356 unsigned rem;
357 if (num < 100000)
358 return put_dec_trunc(buf, num);
359 rem = do_div(num, 100000);
360 buf = put_dec_full(buf, rem);
364 #define ZEROPAD 1 /* pad with zero */
365 #define SIGN 2 /* unsigned/signed long */
366 #define PLUS 4 /* show plus */
367 #define SPACE 8 /* space if plus */
368 #define LEFT 16 /* left justified */
369 #define SMALL 32 /* Must be 32 == 0x20 */
370 #define SPECIAL 64 /* 0x */
372 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
374 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
375 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
377 char tmp[66];
378 char sign;
379 char locase;
380 int need_pfx = ((type & SPECIAL) && base != 10);
381 int i;
383 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
384 * produces same digits or (maybe lowercased) letters */
385 locase = (type & SMALL);
386 if (type & LEFT)
387 type &= ~ZEROPAD;
388 sign = 0;
389 if (type & SIGN) {
390 if ((signed long long) num < 0) {
391 sign = '-';
392 num = - (signed long long) num;
393 size--;
394 } else if (type & PLUS) {
395 sign = '+';
396 size--;
397 } else if (type & SPACE) {
398 sign = ' ';
399 size--;
402 if (need_pfx) {
403 size--;
404 if (base == 16)
405 size--;
408 /* generate full string in tmp[], in reverse order */
409 i = 0;
410 if (num == 0)
411 tmp[i++] = '0';
412 /* Generic code, for any base:
413 else do {
414 tmp[i++] = (digits[do_div(num,base)] | locase);
415 } while (num != 0);
417 else if (base != 10) { /* 8 or 16 */
418 int mask = base - 1;
419 int shift = 3;
420 if (base == 16) shift = 4;
421 do {
422 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
423 num >>= shift;
424 } while (num);
425 } else { /* base 10 */
426 i = put_dec(tmp, num) - tmp;
429 /* printing 100 using %2d gives "100", not "00" */
430 if (i > precision)
431 precision = i;
432 /* leading space padding */
433 size -= precision;
434 if (!(type & (ZEROPAD+LEFT))) {
435 while(--size >= 0) {
436 if (buf < end)
437 *buf = ' ';
438 ++buf;
441 /* sign */
442 if (sign) {
443 if (buf < end)
444 *buf = sign;
445 ++buf;
447 /* "0x" / "0" prefix */
448 if (need_pfx) {
449 if (buf < end)
450 *buf = '0';
451 ++buf;
452 if (base == 16) {
453 if (buf < end)
454 *buf = ('X' | locase);
455 ++buf;
458 /* zero or space padding */
459 if (!(type & LEFT)) {
460 char c = (type & ZEROPAD) ? '0' : ' ';
461 while (--size >= 0) {
462 if (buf < end)
463 *buf = c;
464 ++buf;
467 /* hmm even more zero padding? */
468 while (i <= --precision) {
469 if (buf < end)
470 *buf = '0';
471 ++buf;
473 /* actual digits of result */
474 while (--i >= 0) {
475 if (buf < end)
476 *buf = tmp[i];
477 ++buf;
479 /* trailing space padding */
480 while (--size >= 0) {
481 if (buf < end)
482 *buf = ' ';
483 ++buf;
485 return buf;
488 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
490 int len, i;
492 if ((unsigned long)s < PAGE_SIZE)
493 s = "<NULL>";
495 len = strnlen(s, precision);
497 if (!(flags & LEFT)) {
498 while (len < field_width--) {
499 if (buf < end)
500 *buf = ' ';
501 ++buf;
504 for (i = 0; i < len; ++i) {
505 if (buf < end)
506 *buf = *s;
507 ++buf; ++s;
509 while (len < field_width--) {
510 if (buf < end)
511 *buf = ' ';
512 ++buf;
514 return buf;
517 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
519 unsigned long value = (unsigned long) ptr;
520 #ifdef CONFIG_KALLSYMS
521 char sym[KSYM_SYMBOL_LEN];
522 sprint_symbol(sym, value);
523 return string(buf, end, sym, field_width, precision, flags);
524 #else
525 field_width = 2*sizeof(void *);
526 flags |= SPECIAL | SMALL | ZEROPAD;
527 return number(buf, end, value, 16, field_width, precision, flags);
528 #endif
532 * Show a '%p' thing. A kernel extension is that the '%p' is followed
533 * by an extra set of alphanumeric characters that are extended format
534 * specifiers.
536 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
537 * and 'S' (for Symbolic direct pointers), but this can easily be
538 * extended in the future (network address types etc).
540 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
541 * pointers are really function descriptors, which contain a pointer the
542 * real address.
544 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
546 switch (*fmt) {
547 case 'F':
548 ptr = dereference_function_descriptor(ptr);
549 /* Fallthrough */
550 case 'S':
551 return symbol_string(buf, end, ptr, field_width, precision, flags);
553 flags |= SMALL;
554 if (field_width == -1) {
555 field_width = 2*sizeof(void *);
556 flags |= ZEROPAD;
558 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
562 * vsnprintf - Format a string and place it in a buffer
563 * @buf: The buffer to place the result into
564 * @size: The size of the buffer, including the trailing null space
565 * @fmt: The format string to use
566 * @args: Arguments for the format string
568 * The return value is the number of characters which would
569 * be generated for the given input, excluding the trailing
570 * '\0', as per ISO C99. If you want to have the exact
571 * number of characters written into @buf as return value
572 * (not including the trailing '\0'), use vscnprintf(). If the
573 * return is greater than or equal to @size, the resulting
574 * string is truncated.
576 * Call this function if you are already dealing with a va_list.
577 * You probably want snprintf() instead.
579 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
581 unsigned long long num;
582 int base;
583 char *str, *end, c;
585 int flags; /* flags to number() */
587 int field_width; /* width of output field */
588 int precision; /* min. # of digits for integers; max
589 number of chars for from string */
590 int qualifier; /* 'h', 'l', or 'L' for integer fields */
591 /* 'z' support added 23/7/1999 S.H. */
592 /* 'z' changed to 'Z' --davidm 1/25/99 */
593 /* 't' added for ptrdiff_t */
595 /* Reject out-of-range values early. Large positive sizes are
596 used for unknown buffer sizes. */
597 if (unlikely((int) size < 0)) {
598 /* There can be only one.. */
599 static char warn = 1;
600 WARN_ON(warn);
601 warn = 0;
602 return 0;
605 str = buf;
606 end = buf + size;
608 /* Make sure end is always >= buf */
609 if (end < buf) {
610 end = ((void *)-1);
611 size = end - buf;
614 for (; *fmt ; ++fmt) {
615 if (*fmt != '%') {
616 if (str < end)
617 *str = *fmt;
618 ++str;
619 continue;
622 /* process flags */
623 flags = 0;
624 repeat:
625 ++fmt; /* this also skips first '%' */
626 switch (*fmt) {
627 case '-': flags |= LEFT; goto repeat;
628 case '+': flags |= PLUS; goto repeat;
629 case ' ': flags |= SPACE; goto repeat;
630 case '#': flags |= SPECIAL; goto repeat;
631 case '0': flags |= ZEROPAD; goto repeat;
634 /* get field width */
635 field_width = -1;
636 if (isdigit(*fmt))
637 field_width = skip_atoi(&fmt);
638 else if (*fmt == '*') {
639 ++fmt;
640 /* it's the next argument */
641 field_width = va_arg(args, int);
642 if (field_width < 0) {
643 field_width = -field_width;
644 flags |= LEFT;
648 /* get the precision */
649 precision = -1;
650 if (*fmt == '.') {
651 ++fmt;
652 if (isdigit(*fmt))
653 precision = skip_atoi(&fmt);
654 else if (*fmt == '*') {
655 ++fmt;
656 /* it's the next argument */
657 precision = va_arg(args, int);
659 if (precision < 0)
660 precision = 0;
663 /* get the conversion qualifier */
664 qualifier = -1;
665 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
666 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
667 qualifier = *fmt;
668 ++fmt;
669 if (qualifier == 'l' && *fmt == 'l') {
670 qualifier = 'L';
671 ++fmt;
675 /* default base */
676 base = 10;
678 switch (*fmt) {
679 case 'c':
680 if (!(flags & LEFT)) {
681 while (--field_width > 0) {
682 if (str < end)
683 *str = ' ';
684 ++str;
687 c = (unsigned char) va_arg(args, int);
688 if (str < end)
689 *str = c;
690 ++str;
691 while (--field_width > 0) {
692 if (str < end)
693 *str = ' ';
694 ++str;
696 continue;
698 case 's':
699 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
700 continue;
702 case 'p':
703 str = pointer(fmt+1, str, end,
704 va_arg(args, void *),
705 field_width, precision, flags);
706 /* Skip all alphanumeric pointer suffixes */
707 while (isalnum(fmt[1]))
708 fmt++;
709 continue;
711 case 'n':
712 /* FIXME:
713 * What does C99 say about the overflow case here? */
714 if (qualifier == 'l') {
715 long * ip = va_arg(args, long *);
716 *ip = (str - buf);
717 } else if (qualifier == 'Z' || qualifier == 'z') {
718 size_t * ip = va_arg(args, size_t *);
719 *ip = (str - buf);
720 } else {
721 int * ip = va_arg(args, int *);
722 *ip = (str - buf);
724 continue;
726 case '%':
727 if (str < end)
728 *str = '%';
729 ++str;
730 continue;
732 /* integer number formats - set up the flags and "break" */
733 case 'o':
734 base = 8;
735 break;
737 case 'x':
738 flags |= SMALL;
739 case 'X':
740 base = 16;
741 break;
743 case 'd':
744 case 'i':
745 flags |= SIGN;
746 case 'u':
747 break;
749 default:
750 if (str < end)
751 *str = '%';
752 ++str;
753 if (*fmt) {
754 if (str < end)
755 *str = *fmt;
756 ++str;
757 } else {
758 --fmt;
760 continue;
762 if (qualifier == 'L')
763 num = va_arg(args, long long);
764 else if (qualifier == 'l') {
765 num = va_arg(args, unsigned long);
766 if (flags & SIGN)
767 num = (signed long) num;
768 } else if (qualifier == 'Z' || qualifier == 'z') {
769 num = va_arg(args, size_t);
770 } else if (qualifier == 't') {
771 num = va_arg(args, ptrdiff_t);
772 } else if (qualifier == 'h') {
773 num = (unsigned short) va_arg(args, int);
774 if (flags & SIGN)
775 num = (signed short) num;
776 } else {
777 num = va_arg(args, unsigned int);
778 if (flags & SIGN)
779 num = (signed int) num;
781 str = number(str, end, num, base,
782 field_width, precision, flags);
784 if (size > 0) {
785 if (str < end)
786 *str = '\0';
787 else
788 end[-1] = '\0';
790 /* the trailing null byte doesn't count towards the total */
791 return str-buf;
794 EXPORT_SYMBOL(vsnprintf);
797 * vscnprintf - Format a string and place it in a buffer
798 * @buf: The buffer to place the result into
799 * @size: The size of the buffer, including the trailing null space
800 * @fmt: The format string to use
801 * @args: Arguments for the format string
803 * The return value is the number of characters which have been written into
804 * the @buf not including the trailing '\0'. If @size is <= 0 the function
805 * returns 0.
807 * Call this function if you are already dealing with a va_list.
808 * You probably want scnprintf() instead.
810 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
812 int i;
814 i=vsnprintf(buf,size,fmt,args);
815 return (i >= size) ? (size - 1) : i;
818 EXPORT_SYMBOL(vscnprintf);
821 * snprintf - Format a string and place it in a buffer
822 * @buf: The buffer to place the result into
823 * @size: The size of the buffer, including the trailing null space
824 * @fmt: The format string to use
825 * @...: Arguments for the format string
827 * The return value is the number of characters which would be
828 * generated for the given input, excluding the trailing null,
829 * as per ISO C99. If the return is greater than or equal to
830 * @size, the resulting string is truncated.
832 int snprintf(char * buf, size_t size, const char *fmt, ...)
834 va_list args;
835 int i;
837 va_start(args, fmt);
838 i=vsnprintf(buf,size,fmt,args);
839 va_end(args);
840 return i;
843 EXPORT_SYMBOL(snprintf);
846 * scnprintf - Format a string and place it in a buffer
847 * @buf: The buffer to place the result into
848 * @size: The size of the buffer, including the trailing null space
849 * @fmt: The format string to use
850 * @...: Arguments for the format string
852 * The return value is the number of characters written into @buf not including
853 * the trailing '\0'. If @size is <= 0 the function returns 0.
856 int scnprintf(char * buf, size_t size, const char *fmt, ...)
858 va_list args;
859 int i;
861 va_start(args, fmt);
862 i = vsnprintf(buf, size, fmt, args);
863 va_end(args);
864 return (i >= size) ? (size - 1) : i;
866 EXPORT_SYMBOL(scnprintf);
869 * vsprintf - Format a string and place it in a buffer
870 * @buf: The buffer to place the result into
871 * @fmt: The format string to use
872 * @args: Arguments for the format string
874 * The function returns the number of characters written
875 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
876 * buffer overflows.
878 * Call this function if you are already dealing with a va_list.
879 * You probably want sprintf() instead.
881 int vsprintf(char *buf, const char *fmt, va_list args)
883 return vsnprintf(buf, INT_MAX, fmt, args);
886 EXPORT_SYMBOL(vsprintf);
889 * sprintf - Format a string and place it in a buffer
890 * @buf: The buffer to place the result into
891 * @fmt: The format string to use
892 * @...: Arguments for the format string
894 * The function returns the number of characters written
895 * into @buf. Use snprintf() or scnprintf() in order to avoid
896 * buffer overflows.
898 int sprintf(char * buf, const char *fmt, ...)
900 va_list args;
901 int i;
903 va_start(args, fmt);
904 i=vsnprintf(buf, INT_MAX, fmt, args);
905 va_end(args);
906 return i;
909 EXPORT_SYMBOL(sprintf);
912 * vsscanf - Unformat a buffer into a list of arguments
913 * @buf: input buffer
914 * @fmt: format of buffer
915 * @args: arguments
917 int vsscanf(const char * buf, const char * fmt, va_list args)
919 const char *str = buf;
920 char *next;
921 char digit;
922 int num = 0;
923 int qualifier;
924 int base;
925 int field_width;
926 int is_sign = 0;
928 while(*fmt && *str) {
929 /* skip any white space in format */
930 /* white space in format matchs any amount of
931 * white space, including none, in the input.
933 if (isspace(*fmt)) {
934 while (isspace(*fmt))
935 ++fmt;
936 while (isspace(*str))
937 ++str;
940 /* anything that is not a conversion must match exactly */
941 if (*fmt != '%' && *fmt) {
942 if (*fmt++ != *str++)
943 break;
944 continue;
947 if (!*fmt)
948 break;
949 ++fmt;
951 /* skip this conversion.
952 * advance both strings to next white space
954 if (*fmt == '*') {
955 while (!isspace(*fmt) && *fmt)
956 fmt++;
957 while (!isspace(*str) && *str)
958 str++;
959 continue;
962 /* get field width */
963 field_width = -1;
964 if (isdigit(*fmt))
965 field_width = skip_atoi(&fmt);
967 /* get conversion qualifier */
968 qualifier = -1;
969 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
970 *fmt == 'Z' || *fmt == 'z') {
971 qualifier = *fmt++;
972 if (unlikely(qualifier == *fmt)) {
973 if (qualifier == 'h') {
974 qualifier = 'H';
975 fmt++;
976 } else if (qualifier == 'l') {
977 qualifier = 'L';
978 fmt++;
982 base = 10;
983 is_sign = 0;
985 if (!*fmt || !*str)
986 break;
988 switch(*fmt++) {
989 case 'c':
991 char *s = (char *) va_arg(args,char*);
992 if (field_width == -1)
993 field_width = 1;
994 do {
995 *s++ = *str++;
996 } while (--field_width > 0 && *str);
997 num++;
999 continue;
1000 case 's':
1002 char *s = (char *) va_arg(args, char *);
1003 if(field_width == -1)
1004 field_width = INT_MAX;
1005 /* first, skip leading white space in buffer */
1006 while (isspace(*str))
1007 str++;
1009 /* now copy until next white space */
1010 while (*str && !isspace(*str) && field_width--) {
1011 *s++ = *str++;
1013 *s = '\0';
1014 num++;
1016 continue;
1017 case 'n':
1018 /* return number of characters read so far */
1020 int *i = (int *)va_arg(args,int*);
1021 *i = str - buf;
1023 continue;
1024 case 'o':
1025 base = 8;
1026 break;
1027 case 'x':
1028 case 'X':
1029 base = 16;
1030 break;
1031 case 'i':
1032 base = 0;
1033 case 'd':
1034 is_sign = 1;
1035 case 'u':
1036 break;
1037 case '%':
1038 /* looking for '%' in str */
1039 if (*str++ != '%')
1040 return num;
1041 continue;
1042 default:
1043 /* invalid format; stop here */
1044 return num;
1047 /* have some sort of integer conversion.
1048 * first, skip white space in buffer.
1050 while (isspace(*str))
1051 str++;
1053 digit = *str;
1054 if (is_sign && digit == '-')
1055 digit = *(str + 1);
1057 if (!digit
1058 || (base == 16 && !isxdigit(digit))
1059 || (base == 10 && !isdigit(digit))
1060 || (base == 8 && (!isdigit(digit) || digit > '7'))
1061 || (base == 0 && !isdigit(digit)))
1062 break;
1064 switch(qualifier) {
1065 case 'H': /* that's 'hh' in format */
1066 if (is_sign) {
1067 signed char *s = (signed char *) va_arg(args,signed char *);
1068 *s = (signed char) simple_strtol(str,&next,base);
1069 } else {
1070 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1071 *s = (unsigned char) simple_strtoul(str, &next, base);
1073 break;
1074 case 'h':
1075 if (is_sign) {
1076 short *s = (short *) va_arg(args,short *);
1077 *s = (short) simple_strtol(str,&next,base);
1078 } else {
1079 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1080 *s = (unsigned short) simple_strtoul(str, &next, base);
1082 break;
1083 case 'l':
1084 if (is_sign) {
1085 long *l = (long *) va_arg(args,long *);
1086 *l = simple_strtol(str,&next,base);
1087 } else {
1088 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1089 *l = simple_strtoul(str,&next,base);
1091 break;
1092 case 'L':
1093 if (is_sign) {
1094 long long *l = (long long*) va_arg(args,long long *);
1095 *l = simple_strtoll(str,&next,base);
1096 } else {
1097 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1098 *l = simple_strtoull(str,&next,base);
1100 break;
1101 case 'Z':
1102 case 'z':
1104 size_t *s = (size_t*) va_arg(args,size_t*);
1105 *s = (size_t) simple_strtoul(str,&next,base);
1107 break;
1108 default:
1109 if (is_sign) {
1110 int *i = (int *) va_arg(args, int*);
1111 *i = (int) simple_strtol(str,&next,base);
1112 } else {
1113 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1114 *i = (unsigned int) simple_strtoul(str,&next,base);
1116 break;
1118 num++;
1120 if (!next)
1121 break;
1122 str = next;
1126 * Now we've come all the way through so either the input string or the
1127 * format ended. In the former case, there can be a %n at the current
1128 * position in the format that needs to be filled.
1130 if (*fmt == '%' && *(fmt + 1) == 'n') {
1131 int *p = (int *)va_arg(args, int *);
1132 *p = str - buf;
1135 return num;
1138 EXPORT_SYMBOL(vsscanf);
1141 * sscanf - Unformat a buffer into a list of arguments
1142 * @buf: input buffer
1143 * @fmt: formatting of buffer
1144 * @...: resulting arguments
1146 int sscanf(const char * buf, const char * fmt, ...)
1148 va_list args;
1149 int i;
1151 va_start(args,fmt);
1152 i = vsscanf(buf,fmt,args);
1153 va_end(args);
1154 return i;
1157 EXPORT_SYMBOL(sscanf);