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>
31 /* Works only for digits and letters, but small and fast */
32 #define TOLOWER(x) ((x) | 0x20)
35 * simple_strtoul - convert a string to an unsigned long
36 * @cp: The start of the string
37 * @endp: A pointer to the end of the parsed string will be placed here
38 * @base: The number base to use
40 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
42 unsigned long result = 0,value;
49 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
54 } else if (base == 16) {
55 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
58 while (isxdigit(*cp) &&
59 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
60 result = result*base + value;
68 EXPORT_SYMBOL(simple_strtoul);
71 * simple_strtol - convert a string to a signed long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
76 long simple_strtol(const char *cp,char **endp,unsigned int base)
79 return -simple_strtoul(cp+1,endp,base);
80 return simple_strtoul(cp,endp,base);
83 EXPORT_SYMBOL(simple_strtol);
86 * simple_strtoull - convert a string to an unsigned long long
87 * @cp: The start of the string
88 * @endp: A pointer to the end of the parsed string will be placed here
89 * @base: The number base to use
91 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
93 unsigned long long result = 0,value;
100 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
105 } else if (base == 16) {
106 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
110 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
111 result = result*base + value;
119 EXPORT_SYMBOL(simple_strtoull);
122 * simple_strtoll - convert a string to a signed long long
123 * @cp: The start of the string
124 * @endp: A pointer to the end of the parsed string will be placed here
125 * @base: The number base to use
127 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
130 return -simple_strtoull(cp+1,endp,base);
131 return simple_strtoull(cp,endp,base);
136 * strict_strtoul - convert a string to an unsigned long strictly
137 * @cp: The string to be converted
138 * @base: The number base to use
139 * @res: The converted result value
141 * strict_strtoul converts a string to an unsigned long only if the
142 * string is really an unsigned long string, any string containing
143 * any invalid char at the tail will be rejected and -EINVAL is returned,
144 * only a newline char at the tail is acceptible because people generally
145 * change a module parameter in the following way:
147 * echo 1024 > /sys/module/e1000/parameters/copybreak
149 * echo will append a newline to the tail.
151 * It returns 0 if conversion is successful and *res is set to the converted
152 * value, otherwise it returns -EINVAL and *res is set to 0.
154 * simple_strtoul just ignores the successive invalid characters and
155 * return the converted value of prefix part of the string.
157 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
160 * strict_strtol - convert a string to a long strictly
161 * @cp: The string to be converted
162 * @base: The number base to use
163 * @res: The converted result value
165 * strict_strtol is similiar to strict_strtoul, but it allows the first
166 * character of a string is '-'.
168 * It returns 0 if conversion is successful and *res is set to the converted
169 * value, otherwise it returns -EINVAL and *res is set to 0.
171 int strict_strtol(const char *cp, unsigned int base, long *res);
174 * strict_strtoull - convert a string to an unsigned long long strictly
175 * @cp: The string to be converted
176 * @base: The number base to use
177 * @res: The converted result value
179 * strict_strtoull converts a string to an unsigned long long only if the
180 * string is really an unsigned long long string, any string containing
181 * any invalid char at the tail will be rejected and -EINVAL is returned,
182 * only a newline char at the tail is acceptible because people generally
183 * change a module parameter in the following way:
185 * echo 1024 > /sys/module/e1000/parameters/copybreak
187 * echo will append a newline to the tail of the string.
189 * It returns 0 if conversion is successful and *res is set to the converted
190 * value, otherwise it returns -EINVAL and *res is set to 0.
192 * simple_strtoull just ignores the successive invalid characters and
193 * return the converted value of prefix part of the string.
195 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
198 * strict_strtoll - convert a string to a long long strictly
199 * @cp: The string to be converted
200 * @base: The number base to use
201 * @res: The converted result value
203 * strict_strtoll is similiar to strict_strtoull, but it allows the first
204 * character of a string is '-'.
206 * It returns 0 if conversion is successful and *res is set to the converted
207 * value, otherwise it returns -EINVAL and *res is set to 0.
209 int strict_strtoll(const char *cp, unsigned int base, long long *res);
211 #define define_strict_strtoux(type, valtype) \
212 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
223 val = simple_strtou##type(cp, &tail, base); \
224 if ((*tail == '\0') || \
225 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
233 #define define_strict_strtox(type, valtype) \
234 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
238 ret = strict_strtou##type(cp+1, base, res); \
242 ret = strict_strtou##type(cp, base, res); \
247 define_strict_strtoux(l, unsigned long)
248 define_strict_strtox(l, long)
249 define_strict_strtoux(ll, unsigned long long)
250 define_strict_strtox(ll, long long)
252 EXPORT_SYMBOL(strict_strtoul);
253 EXPORT_SYMBOL(strict_strtol);
254 EXPORT_SYMBOL(strict_strtoll);
255 EXPORT_SYMBOL(strict_strtoull);
257 static int skip_atoi(const char **s)
262 i = i*10 + *((*s)++) - '0';
266 /* Decimal conversion is by far the most typical, and is used
267 * for /proc and /sys data. This directly impacts e.g. top performance
268 * with many processes running. We optimize it for speed
270 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
271 * (with permission from the author, Douglas W. Jones). */
273 /* Formats correctly any integer in [0,99999].
274 * Outputs from one to five digits depending on input.
275 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
276 static char* put_dec_trunc(char *buf, unsigned q)
278 unsigned d3, d2, d1, d0;
283 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
284 q = (d0 * 0xcd) >> 11;
286 *buf++ = d0 + '0'; /* least significant digit */
287 d1 = q + 9*d3 + 5*d2 + d1;
289 q = (d1 * 0xcd) >> 11;
291 *buf++ = d1 + '0'; /* next digit */
294 if ((d2 != 0) || (d3 != 0)) {
297 *buf++ = d2 + '0'; /* next digit */
301 q = (d3 * 0xcd) >> 11;
303 *buf++ = d3 + '0'; /* next digit */
305 *buf++ = q + '0'; /* most sign. digit */
311 /* Same with if's removed. Always emits five digits */
312 static char* put_dec_full(char *buf, unsigned q)
314 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
315 /* but anyway, gcc produces better code with full-sized ints */
316 unsigned d3, d2, d1, d0;
321 /* Possible ways to approx. divide by 10 */
322 /* gcc -O2 replaces multiply with shifts and adds */
323 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
324 // (x * 0x67) >> 10: 1100111
325 // (x * 0x34) >> 9: 110100 - same
326 // (x * 0x1a) >> 8: 11010 - same
327 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
329 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
330 q = (d0 * 0xcd) >> 11;
333 d1 = q + 9*d3 + 5*d2 + d1;
334 q = (d1 * 0xcd) >> 11;
344 q = (d3 * 0xcd) >> 11; /* - shorter code */
345 /* q = (d3 * 0x67) >> 10; - would also work */
351 /* No inlining helps gcc to use registers better */
352 static noinline char* put_dec(char *buf, unsigned long long num)
357 return put_dec_trunc(buf, num);
358 rem = do_div(num, 100000);
359 buf = put_dec_full(buf, rem);
363 #define ZEROPAD 1 /* pad with zero */
364 #define SIGN 2 /* unsigned/signed long */
365 #define PLUS 4 /* show plus */
366 #define SPACE 8 /* space if plus */
367 #define LEFT 16 /* left justified */
368 #define SMALL 32 /* Must be 32 == 0x20 */
369 #define SPECIAL 64 /* 0x */
371 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
373 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
374 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
379 int need_pfx = ((type & SPECIAL) && base != 10);
382 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
383 * produces same digits or (maybe lowercased) letters */
384 locase = (type & SMALL);
389 if ((signed long long) num < 0) {
391 num = - (signed long long) num;
393 } else if (type & PLUS) {
396 } else if (type & SPACE) {
407 /* generate full string in tmp[], in reverse order */
411 /* Generic code, for any base:
413 tmp[i++] = (digits[do_div(num,base)] | locase);
416 else if (base != 10) { /* 8 or 16 */
419 if (base == 16) shift = 4;
421 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
424 } else { /* base 10 */
425 i = put_dec(tmp, num) - tmp;
428 /* printing 100 using %2d gives "100", not "00" */
431 /* leading space padding */
433 if (!(type & (ZEROPAD+LEFT))) {
446 /* "0x" / "0" prefix */
453 *buf = ('X' | locase);
457 /* zero or space padding */
458 if (!(type & LEFT)) {
459 char c = (type & ZEROPAD) ? '0' : ' ';
460 while (--size >= 0) {
466 /* hmm even more zero padding? */
467 while (i <= --precision) {
472 /* actual digits of result */
478 /* trailing space padding */
479 while (--size >= 0) {
487 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
491 if ((unsigned long)s < PAGE_SIZE)
494 len = strnlen(s, precision);
496 if (!(flags & LEFT)) {
497 while (len < field_width--) {
503 for (i = 0; i < len; ++i) {
508 while (len < field_width--) {
516 static inline void *dereference_function_descriptor(void *ptr)
518 #if defined(CONFIG_IA64) || defined(CONFIG_PPC64)
520 if (!probe_kernel_address(ptr, p))
526 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
528 unsigned long value = (unsigned long) ptr;
529 #ifdef CONFIG_KALLSYMS
530 char sym[KSYM_SYMBOL_LEN];
531 sprint_symbol(sym, value);
532 return string(buf, end, sym, field_width, precision, flags);
534 field_width = 2*sizeof(void *);
535 flags |= SPECIAL | SMALL | ZEROPAD;
536 return number(buf, end, value, 16, field_width, precision, flags);
541 * Show a '%p' thing. A kernel extension is that the '%p' is followed
542 * by an extra set of alphanumeric characters that are extended format
545 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
546 * and 'S' (for Symbolic direct pointers), but this can easily be
547 * extended in the future (network address types etc).
549 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
550 * pointers are really function descriptors, which contain a pointer the
553 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
557 ptr = dereference_function_descriptor(ptr);
560 return symbol_string(buf, end, ptr, field_width, precision, flags);
563 if (field_width == -1) {
564 field_width = 2*sizeof(void *);
567 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
571 * vsnprintf - Format a string and place it in a buffer
572 * @buf: The buffer to place the result into
573 * @size: The size of the buffer, including the trailing null space
574 * @fmt: The format string to use
575 * @args: Arguments for the format string
577 * The return value is the number of characters which would
578 * be generated for the given input, excluding the trailing
579 * '\0', as per ISO C99. If you want to have the exact
580 * number of characters written into @buf as return value
581 * (not including the trailing '\0'), use vscnprintf(). If the
582 * return is greater than or equal to @size, the resulting
583 * string is truncated.
585 * Call this function if you are already dealing with a va_list.
586 * You probably want snprintf() instead.
588 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
590 unsigned long long num;
594 int flags; /* flags to number() */
596 int field_width; /* width of output field */
597 int precision; /* min. # of digits for integers; max
598 number of chars for from string */
599 int qualifier; /* 'h', 'l', or 'L' for integer fields */
600 /* 'z' support added 23/7/1999 S.H. */
601 /* 'z' changed to 'Z' --davidm 1/25/99 */
602 /* 't' added for ptrdiff_t */
604 /* Reject out-of-range values early. Large positive sizes are
605 used for unknown buffer sizes. */
606 if (unlikely((int) size < 0)) {
607 /* There can be only one.. */
608 static char warn = 1;
617 /* Make sure end is always >= buf */
623 for (; *fmt ; ++fmt) {
634 ++fmt; /* this also skips first '%' */
636 case '-': flags |= LEFT; goto repeat;
637 case '+': flags |= PLUS; goto repeat;
638 case ' ': flags |= SPACE; goto repeat;
639 case '#': flags |= SPECIAL; goto repeat;
640 case '0': flags |= ZEROPAD; goto repeat;
643 /* get field width */
646 field_width = skip_atoi(&fmt);
647 else if (*fmt == '*') {
649 /* it's the next argument */
650 field_width = va_arg(args, int);
651 if (field_width < 0) {
652 field_width = -field_width;
657 /* get the precision */
662 precision = skip_atoi(&fmt);
663 else if (*fmt == '*') {
665 /* it's the next argument */
666 precision = va_arg(args, int);
672 /* get the conversion qualifier */
674 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
675 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
678 if (qualifier == 'l' && *fmt == 'l') {
689 if (!(flags & LEFT)) {
690 while (--field_width > 0) {
696 c = (unsigned char) va_arg(args, int);
700 while (--field_width > 0) {
708 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
712 str = pointer(fmt+1, str, end,
713 va_arg(args, void *),
714 field_width, precision, flags);
715 /* Skip all alphanumeric pointer suffixes */
716 while (isalnum(fmt[1]))
722 * What does C99 say about the overflow case here? */
723 if (qualifier == 'l') {
724 long * ip = va_arg(args, long *);
726 } else if (qualifier == 'Z' || qualifier == 'z') {
727 size_t * ip = va_arg(args, size_t *);
730 int * ip = va_arg(args, int *);
741 /* integer number formats - set up the flags and "break" */
771 if (qualifier == 'L')
772 num = va_arg(args, long long);
773 else if (qualifier == 'l') {
774 num = va_arg(args, unsigned long);
776 num = (signed long) num;
777 } else if (qualifier == 'Z' || qualifier == 'z') {
778 num = va_arg(args, size_t);
779 } else if (qualifier == 't') {
780 num = va_arg(args, ptrdiff_t);
781 } else if (qualifier == 'h') {
782 num = (unsigned short) va_arg(args, int);
784 num = (signed short) num;
786 num = va_arg(args, unsigned int);
788 num = (signed int) num;
790 str = number(str, end, num, base,
791 field_width, precision, flags);
799 /* the trailing null byte doesn't count towards the total */
803 EXPORT_SYMBOL(vsnprintf);
806 * vscnprintf - Format a string and place it in a buffer
807 * @buf: The buffer to place the result into
808 * @size: The size of the buffer, including the trailing null space
809 * @fmt: The format string to use
810 * @args: Arguments for the format string
812 * The return value is the number of characters which have been written into
813 * the @buf not including the trailing '\0'. If @size is <= 0 the function
816 * Call this function if you are already dealing with a va_list.
817 * You probably want scnprintf() instead.
819 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
823 i=vsnprintf(buf,size,fmt,args);
824 return (i >= size) ? (size - 1) : i;
827 EXPORT_SYMBOL(vscnprintf);
830 * snprintf - Format a string and place it in a buffer
831 * @buf: The buffer to place the result into
832 * @size: The size of the buffer, including the trailing null space
833 * @fmt: The format string to use
834 * @...: Arguments for the format string
836 * The return value is the number of characters which would be
837 * generated for the given input, excluding the trailing null,
838 * as per ISO C99. If the return is greater than or equal to
839 * @size, the resulting string is truncated.
841 int snprintf(char * buf, size_t size, const char *fmt, ...)
847 i=vsnprintf(buf,size,fmt,args);
852 EXPORT_SYMBOL(snprintf);
855 * scnprintf - Format a string and place it in a buffer
856 * @buf: The buffer to place the result into
857 * @size: The size of the buffer, including the trailing null space
858 * @fmt: The format string to use
859 * @...: Arguments for the format string
861 * The return value is the number of characters written into @buf not including
862 * the trailing '\0'. If @size is <= 0 the function returns 0.
865 int scnprintf(char * buf, size_t size, const char *fmt, ...)
871 i = vsnprintf(buf, size, fmt, args);
873 return (i >= size) ? (size - 1) : i;
875 EXPORT_SYMBOL(scnprintf);
878 * vsprintf - Format a string and place it in a buffer
879 * @buf: The buffer to place the result into
880 * @fmt: The format string to use
881 * @args: Arguments for the format string
883 * The function returns the number of characters written
884 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
887 * Call this function if you are already dealing with a va_list.
888 * You probably want sprintf() instead.
890 int vsprintf(char *buf, const char *fmt, va_list args)
892 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 int sprintf(char * buf, const char *fmt, ...)
913 i=vsnprintf(buf, INT_MAX, fmt, args);
918 EXPORT_SYMBOL(sprintf);
921 * vsscanf - Unformat a buffer into a list of arguments
923 * @fmt: format of buffer
926 int vsscanf(const char * buf, const char * fmt, va_list args)
928 const char *str = buf;
937 while(*fmt && *str) {
938 /* skip any white space in format */
939 /* white space in format matchs any amount of
940 * white space, including none, in the input.
943 while (isspace(*fmt))
945 while (isspace(*str))
949 /* anything that is not a conversion must match exactly */
950 if (*fmt != '%' && *fmt) {
951 if (*fmt++ != *str++)
960 /* skip this conversion.
961 * advance both strings to next white space
964 while (!isspace(*fmt) && *fmt)
966 while (!isspace(*str) && *str)
971 /* get field width */
974 field_width = skip_atoi(&fmt);
976 /* get conversion qualifier */
978 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
979 *fmt == 'Z' || *fmt == 'z') {
981 if (unlikely(qualifier == *fmt)) {
982 if (qualifier == 'h') {
985 } else if (qualifier == 'l') {
1000 char *s = (char *) va_arg(args,char*);
1001 if (field_width == -1)
1005 } while (--field_width > 0 && *str);
1011 char *s = (char *) va_arg(args, char *);
1012 if(field_width == -1)
1013 field_width = INT_MAX;
1014 /* first, skip leading white space in buffer */
1015 while (isspace(*str))
1018 /* now copy until next white space */
1019 while (*str && !isspace(*str) && field_width--) {
1027 /* return number of characters read so far */
1029 int *i = (int *)va_arg(args,int*);
1047 /* looking for '%' in str */
1052 /* invalid format; stop here */
1056 /* have some sort of integer conversion.
1057 * first, skip white space in buffer.
1059 while (isspace(*str))
1063 if (is_sign && digit == '-')
1067 || (base == 16 && !isxdigit(digit))
1068 || (base == 10 && !isdigit(digit))
1069 || (base == 8 && (!isdigit(digit) || digit > '7'))
1070 || (base == 0 && !isdigit(digit)))
1074 case 'H': /* that's 'hh' in format */
1076 signed char *s = (signed char *) va_arg(args,signed char *);
1077 *s = (signed char) simple_strtol(str,&next,base);
1079 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1080 *s = (unsigned char) simple_strtoul(str, &next, base);
1085 short *s = (short *) va_arg(args,short *);
1086 *s = (short) simple_strtol(str,&next,base);
1088 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1089 *s = (unsigned short) simple_strtoul(str, &next, base);
1094 long *l = (long *) va_arg(args,long *);
1095 *l = simple_strtol(str,&next,base);
1097 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1098 *l = simple_strtoul(str,&next,base);
1103 long long *l = (long long*) va_arg(args,long long *);
1104 *l = simple_strtoll(str,&next,base);
1106 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1107 *l = simple_strtoull(str,&next,base);
1113 size_t *s = (size_t*) va_arg(args,size_t*);
1114 *s = (size_t) simple_strtoul(str,&next,base);
1119 int *i = (int *) va_arg(args, int*);
1120 *i = (int) simple_strtol(str,&next,base);
1122 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1123 *i = (unsigned int) simple_strtoul(str,&next,base);
1135 * Now we've come all the way through so either the input string or the
1136 * format ended. In the former case, there can be a %n at the current
1137 * position in the format that needs to be filled.
1139 if (*fmt == '%' && *(fmt + 1) == 'n') {
1140 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);
1166 EXPORT_SYMBOL(sscanf);