4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
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)
38 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
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;
58 base = simple_guess_base(cp);
60 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
63 while (isxdigit(*cp)) {
66 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 result = result * base + value;
77 EXPORT_SYMBOL(simple_strtoul);
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)
88 return -simple_strtoul(cp + 1, endp, base);
89 return simple_strtoul(cp, endp, base);
91 EXPORT_SYMBOL(simple_strtol);
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;
104 base = simple_guess_base(cp);
106 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
109 while (isxdigit(*cp)) {
112 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
115 result = result * base + value;
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)
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)\
227 val = simple_strtou##type(cp, &tail, base); \
228 if ((*tail == '\0') || \
229 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
237 #define define_strict_strtox(type, valtype) \
238 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
242 ret = strict_strtou##type(cp+1, base, res); \
246 ret = strict_strtou##type(cp, base, res); \
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)
266 i = i*10 + *((*s)++) - '0';
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
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;
287 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
288 q = (d0 * 0xcd) >> 11;
290 *buf++ = d0 + '0'; /* least significant digit */
291 d1 = q + 9*d3 + 5*d2 + d1;
293 q = (d1 * 0xcd) >> 11;
295 *buf++ = d1 + '0'; /* next digit */
298 if ((d2 != 0) || (d3 != 0)) {
301 *buf++ = d2 + '0'; /* next digit */
305 q = (d3 * 0xcd) >> 11;
307 *buf++ = d3 + '0'; /* next digit */
309 *buf++ = q + '0'; /* most sign. digit */
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;
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;
337 d1 = q + 9*d3 + 5*d2 + d1;
338 q = (d1 * 0xcd) >> 11;
348 q = (d3 * 0xcd) >> 11; /* - shorter code */
349 /* q = (d3 * 0x67) >> 10; - would also work */
355 /* No inlining helps gcc to use registers better */
356 static noinline char* put_dec(char *buf, unsigned long long num)
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"; */
383 int need_pfx = ((type & SPECIAL) && base != 10);
386 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
387 * produces same digits or (maybe lowercased) letters */
388 locase = (type & SMALL);
393 if ((signed long long) num < 0) {
395 num = - (signed long long) num;
397 } else if (type & PLUS) {
400 } else if (type & SPACE) {
411 /* generate full string in tmp[], in reverse order */
415 /* Generic code, for any base:
417 tmp[i++] = (digits[do_div(num,base)] | locase);
420 else if (base != 10) { /* 8 or 16 */
423 if (base == 16) shift = 4;
425 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
428 } else { /* base 10 */
429 i = put_dec(tmp, num) - tmp;
432 /* printing 100 using %2d gives "100", not "00" */
435 /* leading space padding */
437 if (!(type & (ZEROPAD+LEFT))) {
450 /* "0x" / "0" prefix */
457 *buf = ('X' | locase);
461 /* zero or space padding */
462 if (!(type & LEFT)) {
463 char c = (type & ZEROPAD) ? '0' : ' ';
464 while (--size >= 0) {
470 /* hmm even more zero padding? */
471 while (i <= --precision) {
476 /* actual digits of result */
482 /* trailing space padding */
483 while (--size >= 0) {
491 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
495 if ((unsigned long)s < PAGE_SIZE)
498 len = strnlen(s, precision);
500 if (!(flags & LEFT)) {
501 while (len < field_width--) {
507 for (i = 0; i < len; ++i) {
512 while (len < field_width--) {
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);
528 field_width = 2*sizeof(void *);
529 flags |= SPECIAL | SMALL | ZEROPAD;
530 return number(buf, end, value, 16, field_width, precision, flags);
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
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
547 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
551 ptr = dereference_function_descriptor(ptr);
554 return symbol_string(buf, end, ptr, field_width, precision, flags);
557 if (field_width == -1) {
558 field_width = 2*sizeof(void *);
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;
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;
615 /* Make sure end is always >= buf */
621 for (; *fmt ; ++fmt) {
632 ++fmt; /* this also skips first '%' */
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 */
644 field_width = skip_atoi(&fmt);
645 else if (*fmt == '*') {
647 /* it's the next argument */
648 field_width = va_arg(args, int);
649 if (field_width < 0) {
650 field_width = -field_width;
655 /* get the precision */
660 precision = skip_atoi(&fmt);
661 else if (*fmt == '*') {
663 /* it's the next argument */
664 precision = va_arg(args, int);
670 /* get the conversion qualifier */
672 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
673 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
676 if (qualifier == 'l' && *fmt == 'l') {
687 if (!(flags & LEFT)) {
688 while (--field_width > 0) {
694 c = (unsigned char) va_arg(args, int);
698 while (--field_width > 0) {
706 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
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]))
720 * What does C99 say about the overflow case here? */
721 if (qualifier == 'l') {
722 long * ip = va_arg(args, long *);
724 } else if (qualifier == 'Z' || qualifier == 'z') {
725 size_t * ip = va_arg(args, size_t *);
728 int * ip = va_arg(args, int *);
739 /* integer number formats - set up the flags and "break" */
769 if (qualifier == 'L')
770 num = va_arg(args, long long);
771 else if (qualifier == 'l') {
772 num = va_arg(args, unsigned long);
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);
782 num = (signed short) num;
784 num = va_arg(args, unsigned int);
786 num = (signed int) num;
788 str = number(str, end, num, base,
789 field_width, precision, flags);
797 /* the trailing null byte doesn't count towards the total */
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
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)
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, ...)
847 i=vsnprintf(buf,size,fmt,args);
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, ...)
870 i = vsnprintf(buf, size, fmt, 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
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
907 * See the vsnprintf() documentation for format string extensions over C99.
909 int sprintf(char * buf, const char *fmt, ...)
915 i=vsnprintf(buf, INT_MAX, fmt, args);
919 EXPORT_SYMBOL(sprintf);
922 * vsscanf - Unformat a buffer into a list of arguments
924 * @fmt: format of buffer
927 int vsscanf(const char * buf, const char * fmt, va_list args)
929 const char *str = buf;
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.
944 while (isspace(*fmt))
946 while (isspace(*str))
950 /* anything that is not a conversion must match exactly */
951 if (*fmt != '%' && *fmt) {
952 if (*fmt++ != *str++)
961 /* skip this conversion.
962 * advance both strings to next white space
965 while (!isspace(*fmt) && *fmt)
967 while (!isspace(*str) && *str)
972 /* get field width */
975 field_width = skip_atoi(&fmt);
977 /* get conversion qualifier */
979 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
980 *fmt == 'Z' || *fmt == 'z') {
982 if (unlikely(qualifier == *fmt)) {
983 if (qualifier == 'h') {
986 } else if (qualifier == 'l') {
1001 char *s = (char *) va_arg(args,char*);
1002 if (field_width == -1)
1006 } while (--field_width > 0 && *str);
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))
1019 /* now copy until next white space */
1020 while (*str && !isspace(*str) && field_width--) {
1028 /* return number of characters read so far */
1030 int *i = (int *)va_arg(args,int*);
1048 /* looking for '%' in str */
1053 /* invalid format; stop here */
1057 /* have some sort of integer conversion.
1058 * first, skip white space in buffer.
1060 while (isspace(*str))
1064 if (is_sign && digit == '-')
1068 || (base == 16 && !isxdigit(digit))
1069 || (base == 10 && !isdigit(digit))
1070 || (base == 8 && (!isdigit(digit) || digit > '7'))
1071 || (base == 0 && !isdigit(digit)))
1075 case 'H': /* that's 'hh' in format */
1077 signed char *s = (signed char *) va_arg(args,signed char *);
1078 *s = (signed char) simple_strtol(str,&next,base);
1080 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1081 *s = (unsigned char) simple_strtoul(str, &next, base);
1086 short *s = (short *) va_arg(args,short *);
1087 *s = (short) simple_strtol(str,&next,base);
1089 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1090 *s = (unsigned short) simple_strtoul(str, &next, base);
1095 long *l = (long *) va_arg(args,long *);
1096 *l = simple_strtol(str,&next,base);
1098 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1099 *l = simple_strtoul(str,&next,base);
1104 long long *l = (long long*) va_arg(args,long long *);
1105 *l = simple_strtoll(str,&next,base);
1107 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1108 *l = simple_strtoull(str,&next,base);
1114 size_t *s = (size_t*) va_arg(args,size_t*);
1115 *s = (size_t) simple_strtoul(str,&next,base);
1120 int *i = (int *) va_arg(args, int*);
1121 *i = (int) simple_strtol(str,&next,base);
1123 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1124 *i = (unsigned int) simple_strtoul(str,&next,base);
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 *);
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, ...)
1161 i = vsscanf(buf,fmt,args);
1165 EXPORT_SYMBOL(sscanf);