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);
139 * strict_strtoul - convert a string to an unsigned long strictly
140 * @cp: The string to be converted
141 * @base: The number base to use
142 * @res: The converted result value
144 * strict_strtoul converts a string to an unsigned long only if the
145 * string is really an unsigned long string, any string containing
146 * any invalid char at the tail will be rejected and -EINVAL is returned,
147 * only a newline char at the tail is acceptible because people generally
148 * change a module parameter in the following way:
150 * echo 1024 > /sys/module/e1000/parameters/copybreak
152 * echo will append a newline to the tail.
154 * It returns 0 if conversion is successful and *res is set to the converted
155 * value, otherwise it returns -EINVAL and *res is set to 0.
157 * simple_strtoul just ignores the successive invalid characters and
158 * return the converted value of prefix part of the string.
160 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
171 val = simple_strtoul(cp, &tail, base);
172 if ((*tail == '\0') ||
173 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
180 EXPORT_SYMBOL(strict_strtoul);
183 * strict_strtol - convert a string to a long strictly
184 * @cp: The string to be converted
185 * @base: The number base to use
186 * @res: The converted result value
188 * strict_strtol is similiar to strict_strtoul, but it allows the first
189 * character of a string is '-'.
191 * It returns 0 if conversion is successful and *res is set to the converted
192 * value, otherwise it returns -EINVAL and *res is set to 0.
194 int strict_strtol(const char *cp, unsigned int base, long *res)
198 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
202 ret = strict_strtoul(cp, base, (unsigned long *)res);
207 EXPORT_SYMBOL(strict_strtol);
210 * strict_strtoull - convert a string to an unsigned long long strictly
211 * @cp: The string to be converted
212 * @base: The number base to use
213 * @res: The converted result value
215 * strict_strtoull converts a string to an unsigned long long only if the
216 * string is really an unsigned long long string, any string containing
217 * any invalid char at the tail will be rejected and -EINVAL is returned,
218 * only a newline char at the tail is acceptible because people generally
219 * change a module parameter in the following way:
221 * echo 1024 > /sys/module/e1000/parameters/copybreak
223 * echo will append a newline to the tail of the string.
225 * It returns 0 if conversion is successful and *res is set to the converted
226 * value, otherwise it returns -EINVAL and *res is set to 0.
228 * simple_strtoull just ignores the successive invalid characters and
229 * return the converted value of prefix part of the string.
231 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
234 unsigned long long val;
242 val = simple_strtoull(cp, &tail, base);
243 if ((*tail == '\0') ||
244 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
251 EXPORT_SYMBOL(strict_strtoull);
254 * strict_strtoll - convert a string to a long long strictly
255 * @cp: The string to be converted
256 * @base: The number base to use
257 * @res: The converted result value
259 * strict_strtoll is similiar to strict_strtoull, but it allows the first
260 * character of a string is '-'.
262 * It returns 0 if conversion is successful and *res is set to the converted
263 * value, otherwise it returns -EINVAL and *res is set to 0.
265 int strict_strtoll(const char *cp, unsigned int base, long long *res)
269 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
273 ret = strict_strtoull(cp, base, (unsigned long long *)res);
278 EXPORT_SYMBOL(strict_strtoll);
280 static int skip_atoi(const char **s)
285 i = i*10 + *((*s)++) - '0';
289 /* Decimal conversion is by far the most typical, and is used
290 * for /proc and /sys data. This directly impacts e.g. top performance
291 * with many processes running. We optimize it for speed
293 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
294 * (with permission from the author, Douglas W. Jones). */
296 /* Formats correctly any integer in [0,99999].
297 * Outputs from one to five digits depending on input.
298 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
299 static char* put_dec_trunc(char *buf, unsigned q)
301 unsigned d3, d2, d1, d0;
306 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
307 q = (d0 * 0xcd) >> 11;
309 *buf++ = d0 + '0'; /* least significant digit */
310 d1 = q + 9*d3 + 5*d2 + d1;
312 q = (d1 * 0xcd) >> 11;
314 *buf++ = d1 + '0'; /* next digit */
317 if ((d2 != 0) || (d3 != 0)) {
320 *buf++ = d2 + '0'; /* next digit */
324 q = (d3 * 0xcd) >> 11;
326 *buf++ = d3 + '0'; /* next digit */
328 *buf++ = q + '0'; /* most sign. digit */
334 /* Same with if's removed. Always emits five digits */
335 static char* put_dec_full(char *buf, unsigned q)
337 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
338 /* but anyway, gcc produces better code with full-sized ints */
339 unsigned d3, d2, d1, d0;
344 /* Possible ways to approx. divide by 10 */
345 /* gcc -O2 replaces multiply with shifts and adds */
346 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
347 // (x * 0x67) >> 10: 1100111
348 // (x * 0x34) >> 9: 110100 - same
349 // (x * 0x1a) >> 8: 11010 - same
350 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
352 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
353 q = (d0 * 0xcd) >> 11;
356 d1 = q + 9*d3 + 5*d2 + d1;
357 q = (d1 * 0xcd) >> 11;
367 q = (d3 * 0xcd) >> 11; /* - shorter code */
368 /* q = (d3 * 0x67) >> 10; - would also work */
374 /* No inlining helps gcc to use registers better */
375 static noinline char* put_dec(char *buf, unsigned long long num)
380 return put_dec_trunc(buf, num);
381 rem = do_div(num, 100000);
382 buf = put_dec_full(buf, rem);
386 #define ZEROPAD 1 /* pad with zero */
387 #define SIGN 2 /* unsigned/signed long */
388 #define PLUS 4 /* show plus */
389 #define SPACE 8 /* space if plus */
390 #define LEFT 16 /* left justified */
391 #define SMALL 32 /* Must be 32 == 0x20 */
392 #define SPECIAL 64 /* 0x */
394 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
396 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
397 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
402 int need_pfx = ((type & SPECIAL) && base != 10);
405 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
406 * produces same digits or (maybe lowercased) letters */
407 locase = (type & SMALL);
412 if ((signed long long) num < 0) {
414 num = - (signed long long) num;
416 } else if (type & PLUS) {
419 } else if (type & SPACE) {
430 /* generate full string in tmp[], in reverse order */
434 /* Generic code, for any base:
436 tmp[i++] = (digits[do_div(num,base)] | locase);
439 else if (base != 10) { /* 8 or 16 */
442 if (base == 16) shift = 4;
444 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
447 } else { /* base 10 */
448 i = put_dec(tmp, num) - tmp;
451 /* printing 100 using %2d gives "100", not "00" */
454 /* leading space padding */
456 if (!(type & (ZEROPAD+LEFT))) {
469 /* "0x" / "0" prefix */
476 *buf = ('X' | locase);
480 /* zero or space padding */
481 if (!(type & LEFT)) {
482 char c = (type & ZEROPAD) ? '0' : ' ';
483 while (--size >= 0) {
489 /* hmm even more zero padding? */
490 while (i <= --precision) {
495 /* actual digits of result */
501 /* trailing space padding */
502 while (--size >= 0) {
510 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
514 if ((unsigned long)s < PAGE_SIZE)
517 len = strnlen(s, precision);
519 if (!(flags & LEFT)) {
520 while (len < field_width--) {
526 for (i = 0; i < len; ++i) {
531 while (len < field_width--) {
539 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
541 unsigned long value = (unsigned long) ptr;
542 #ifdef CONFIG_KALLSYMS
543 char sym[KSYM_SYMBOL_LEN];
544 sprint_symbol(sym, value);
545 return string(buf, end, sym, field_width, precision, flags);
547 field_width = 2*sizeof(void *);
548 flags |= SPECIAL | SMALL | ZEROPAD;
549 return number(buf, end, value, 16, field_width, precision, flags);
554 * Show a '%p' thing. A kernel extension is that the '%p' is followed
555 * by an extra set of alphanumeric characters that are extended format
558 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
559 * and 'S' (for Symbolic direct pointers), but this can easily be
560 * extended in the future (network address types etc).
562 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
563 * pointers are really function descriptors, which contain a pointer the
566 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
570 ptr = dereference_function_descriptor(ptr);
573 return symbol_string(buf, end, ptr, field_width, precision, flags);
576 if (field_width == -1) {
577 field_width = 2*sizeof(void *);
580 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
584 * vsnprintf - Format a string and place it in a buffer
585 * @buf: The buffer to place the result into
586 * @size: The size of the buffer, including the trailing null space
587 * @fmt: The format string to use
588 * @args: Arguments for the format string
590 * This function follows C99 vsnprintf, but has some extensions:
591 * %pS output the name of a text symbol
592 * %pF output the name of a function pointer
594 * The return value is the number of characters which would
595 * be generated for the given input, excluding the trailing
596 * '\0', as per ISO C99. If you want to have the exact
597 * number of characters written into @buf as return value
598 * (not including the trailing '\0'), use vscnprintf(). If the
599 * return is greater than or equal to @size, the resulting
600 * string is truncated.
602 * Call this function if you are already dealing with a va_list.
603 * You probably want snprintf() instead.
605 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
607 unsigned long long num;
611 int flags; /* flags to number() */
613 int field_width; /* width of output field */
614 int precision; /* min. # of digits for integers; max
615 number of chars for from string */
616 int qualifier; /* 'h', 'l', or 'L' for integer fields */
617 /* 'z' support added 23/7/1999 S.H. */
618 /* 'z' changed to 'Z' --davidm 1/25/99 */
619 /* 't' added for ptrdiff_t */
621 /* Reject out-of-range values early. Large positive sizes are
622 used for unknown buffer sizes. */
623 if (unlikely((int) size < 0)) {
624 /* There can be only one.. */
625 static char warn = 1;
634 /* Make sure end is always >= buf */
640 for (; *fmt ; ++fmt) {
651 ++fmt; /* this also skips first '%' */
653 case '-': flags |= LEFT; goto repeat;
654 case '+': flags |= PLUS; goto repeat;
655 case ' ': flags |= SPACE; goto repeat;
656 case '#': flags |= SPECIAL; goto repeat;
657 case '0': flags |= ZEROPAD; goto repeat;
660 /* get field width */
663 field_width = skip_atoi(&fmt);
664 else if (*fmt == '*') {
666 /* it's the next argument */
667 field_width = va_arg(args, int);
668 if (field_width < 0) {
669 field_width = -field_width;
674 /* get the precision */
679 precision = skip_atoi(&fmt);
680 else if (*fmt == '*') {
682 /* it's the next argument */
683 precision = va_arg(args, int);
689 /* get the conversion qualifier */
691 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
692 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
695 if (qualifier == 'l' && *fmt == 'l') {
706 if (!(flags & LEFT)) {
707 while (--field_width > 0) {
713 c = (unsigned char) va_arg(args, int);
717 while (--field_width > 0) {
725 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
729 str = pointer(fmt+1, str, end,
730 va_arg(args, void *),
731 field_width, precision, flags);
732 /* Skip all alphanumeric pointer suffixes */
733 while (isalnum(fmt[1]))
739 * What does C99 say about the overflow case here? */
740 if (qualifier == 'l') {
741 long * ip = va_arg(args, long *);
743 } else if (qualifier == 'Z' || qualifier == 'z') {
744 size_t * ip = va_arg(args, size_t *);
747 int * ip = va_arg(args, int *);
758 /* integer number formats - set up the flags and "break" */
788 if (qualifier == 'L')
789 num = va_arg(args, long long);
790 else if (qualifier == 'l') {
791 num = va_arg(args, unsigned long);
793 num = (signed long) num;
794 } else if (qualifier == 'Z' || qualifier == 'z') {
795 num = va_arg(args, size_t);
796 } else if (qualifier == 't') {
797 num = va_arg(args, ptrdiff_t);
798 } else if (qualifier == 'h') {
799 num = (unsigned short) va_arg(args, int);
801 num = (signed short) num;
803 num = va_arg(args, unsigned int);
805 num = (signed int) num;
807 str = number(str, end, num, base,
808 field_width, precision, flags);
816 /* the trailing null byte doesn't count towards the total */
819 EXPORT_SYMBOL(vsnprintf);
822 * vscnprintf - Format a string and place it in a buffer
823 * @buf: The buffer to place the result into
824 * @size: The size of the buffer, including the trailing null space
825 * @fmt: The format string to use
826 * @args: Arguments for the format string
828 * The return value is the number of characters which have been written into
829 * the @buf not including the trailing '\0'. If @size is <= 0 the function
832 * Call this function if you are already dealing with a va_list.
833 * You probably want scnprintf() instead.
835 * See the vsnprintf() documentation for format string extensions over C99.
837 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
841 i=vsnprintf(buf,size,fmt,args);
842 return (i >= size) ? (size - 1) : i;
844 EXPORT_SYMBOL(vscnprintf);
847 * snprintf - Format a string and place it in a buffer
848 * @buf: The buffer to place the result into
849 * @size: The size of the buffer, including the trailing null space
850 * @fmt: The format string to use
851 * @...: Arguments for the format string
853 * The return value is the number of characters which would be
854 * generated for the given input, excluding the trailing null,
855 * as per ISO C99. If the return is greater than or equal to
856 * @size, the resulting string is truncated.
858 * See the vsnprintf() documentation for format string extensions over C99.
860 int snprintf(char * buf, size_t size, const char *fmt, ...)
866 i=vsnprintf(buf,size,fmt,args);
870 EXPORT_SYMBOL(snprintf);
873 * scnprintf - Format a string and place it in a buffer
874 * @buf: The buffer to place the result into
875 * @size: The size of the buffer, including the trailing null space
876 * @fmt: The format string to use
877 * @...: Arguments for the format string
879 * The return value is the number of characters written into @buf not including
880 * the trailing '\0'. If @size is <= 0 the function returns 0.
883 int scnprintf(char * buf, size_t size, const char *fmt, ...)
889 i = vsnprintf(buf, size, fmt, args);
891 return (i >= size) ? (size - 1) : i;
893 EXPORT_SYMBOL(scnprintf);
896 * vsprintf - Format a string and place it in a buffer
897 * @buf: The buffer to place the result into
898 * @fmt: The format string to use
899 * @args: Arguments for the format string
901 * The function returns the number of characters written
902 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
905 * Call this function if you are already dealing with a va_list.
906 * You probably want sprintf() instead.
908 * See the vsnprintf() documentation for format string extensions over C99.
910 int vsprintf(char *buf, const char *fmt, va_list args)
912 return vsnprintf(buf, INT_MAX, fmt, args);
914 EXPORT_SYMBOL(vsprintf);
917 * sprintf - Format a string and place it in a buffer
918 * @buf: The buffer to place the result into
919 * @fmt: The format string to use
920 * @...: Arguments for the format string
922 * The function returns the number of characters written
923 * into @buf. Use snprintf() or scnprintf() in order to avoid
926 * See the vsnprintf() documentation for format string extensions over C99.
928 int sprintf(char * buf, const char *fmt, ...)
934 i=vsnprintf(buf, INT_MAX, fmt, args);
938 EXPORT_SYMBOL(sprintf);
941 * vsscanf - Unformat a buffer into a list of arguments
943 * @fmt: format of buffer
946 int vsscanf(const char * buf, const char * fmt, va_list args)
948 const char *str = buf;
957 while(*fmt && *str) {
958 /* skip any white space in format */
959 /* white space in format matchs any amount of
960 * white space, including none, in the input.
963 while (isspace(*fmt))
965 while (isspace(*str))
969 /* anything that is not a conversion must match exactly */
970 if (*fmt != '%' && *fmt) {
971 if (*fmt++ != *str++)
980 /* skip this conversion.
981 * advance both strings to next white space
984 while (!isspace(*fmt) && *fmt)
986 while (!isspace(*str) && *str)
991 /* get field width */
994 field_width = skip_atoi(&fmt);
996 /* get conversion qualifier */
998 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
999 *fmt == 'Z' || *fmt == 'z') {
1001 if (unlikely(qualifier == *fmt)) {
1002 if (qualifier == 'h') {
1005 } else if (qualifier == 'l') {
1020 char *s = (char *) va_arg(args,char*);
1021 if (field_width == -1)
1025 } while (--field_width > 0 && *str);
1031 char *s = (char *) va_arg(args, char *);
1032 if(field_width == -1)
1033 field_width = INT_MAX;
1034 /* first, skip leading white space in buffer */
1035 while (isspace(*str))
1038 /* now copy until next white space */
1039 while (*str && !isspace(*str) && field_width--) {
1047 /* return number of characters read so far */
1049 int *i = (int *)va_arg(args,int*);
1067 /* looking for '%' in str */
1072 /* invalid format; stop here */
1076 /* have some sort of integer conversion.
1077 * first, skip white space in buffer.
1079 while (isspace(*str))
1083 if (is_sign && digit == '-')
1087 || (base == 16 && !isxdigit(digit))
1088 || (base == 10 && !isdigit(digit))
1089 || (base == 8 && (!isdigit(digit) || digit > '7'))
1090 || (base == 0 && !isdigit(digit)))
1094 case 'H': /* that's 'hh' in format */
1096 signed char *s = (signed char *) va_arg(args,signed char *);
1097 *s = (signed char) simple_strtol(str,&next,base);
1099 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1100 *s = (unsigned char) simple_strtoul(str, &next, base);
1105 short *s = (short *) va_arg(args,short *);
1106 *s = (short) simple_strtol(str,&next,base);
1108 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1109 *s = (unsigned short) simple_strtoul(str, &next, base);
1114 long *l = (long *) va_arg(args,long *);
1115 *l = simple_strtol(str,&next,base);
1117 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1118 *l = simple_strtoul(str,&next,base);
1123 long long *l = (long long*) va_arg(args,long long *);
1124 *l = simple_strtoll(str,&next,base);
1126 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1127 *l = simple_strtoull(str,&next,base);
1133 size_t *s = (size_t*) va_arg(args,size_t*);
1134 *s = (size_t) simple_strtoul(str,&next,base);
1139 int *i = (int *) va_arg(args, int*);
1140 *i = (int) simple_strtol(str,&next,base);
1142 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1143 *i = (unsigned int) simple_strtoul(str,&next,base);
1155 * Now we've come all the way through so either the input string or the
1156 * format ended. In the former case, there can be a %n at the current
1157 * position in the format that needs to be filled.
1159 if (*fmt == '%' && *(fmt + 1) == 'n') {
1160 int *p = (int *)va_arg(args, int *);
1166 EXPORT_SYMBOL(vsscanf);
1169 * sscanf - Unformat a buffer into a list of arguments
1170 * @buf: input buffer
1171 * @fmt: formatting of buffer
1172 * @...: resulting arguments
1174 int sscanf(const char * buf, const char * fmt, ...)
1180 i = vsscanf(buf,fmt,args);
1184 EXPORT_SYMBOL(sscanf);