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);
92 EXPORT_SYMBOL(simple_strtol);
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
102 unsigned long long result = 0;
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
110 while (isxdigit(*cp)) {
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116 result = result * base + value;
124 EXPORT_SYMBOL(simple_strtoull);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
135 return -simple_strtoull(cp+1,endp,base);
136 return simple_strtoull(cp,endp,base);
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
154 * echo will append a newline to the tail.
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
162 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
165 * strict_strtol - convert a string to a long strictly
166 * @cp: The string to be converted
167 * @base: The number base to use
168 * @res: The converted result value
170 * strict_strtol is similiar to strict_strtoul, but it allows the first
171 * character of a string is '-'.
173 * It returns 0 if conversion is successful and *res is set to the converted
174 * value, otherwise it returns -EINVAL and *res is set to 0.
176 int strict_strtol(const char *cp, unsigned int base, long *res);
179 * strict_strtoull - convert a string to an unsigned long long strictly
180 * @cp: The string to be converted
181 * @base: The number base to use
182 * @res: The converted result value
184 * strict_strtoull converts a string to an unsigned long long only if the
185 * string is really an unsigned long long string, any string containing
186 * any invalid char at the tail will be rejected and -EINVAL is returned,
187 * only a newline char at the tail is acceptible because people generally
188 * change a module parameter in the following way:
190 * echo 1024 > /sys/module/e1000/parameters/copybreak
192 * echo will append a newline to the tail of the string.
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
197 * simple_strtoull just ignores the successive invalid characters and
198 * return the converted value of prefix part of the string.
200 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
203 * strict_strtoll - convert a string to a long long strictly
204 * @cp: The string to be converted
205 * @base: The number base to use
206 * @res: The converted result value
208 * strict_strtoll is similiar to strict_strtoull, but it allows the first
209 * character of a string is '-'.
211 * It returns 0 if conversion is successful and *res is set to the converted
212 * value, otherwise it returns -EINVAL and *res is set to 0.
214 int strict_strtoll(const char *cp, unsigned int base, long long *res);
216 #define define_strict_strtoux(type, valtype) \
217 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
228 val = simple_strtou##type(cp, &tail, base); \
229 if ((*tail == '\0') || \
230 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
238 #define define_strict_strtox(type, valtype) \
239 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
243 ret = strict_strtou##type(cp+1, base, res); \
247 ret = strict_strtou##type(cp, base, res); \
252 define_strict_strtoux(l, unsigned long)
253 define_strict_strtox(l, long)
254 define_strict_strtoux(ll, unsigned long long)
255 define_strict_strtox(ll, long long)
257 EXPORT_SYMBOL(strict_strtoul);
258 EXPORT_SYMBOL(strict_strtol);
259 EXPORT_SYMBOL(strict_strtoll);
260 EXPORT_SYMBOL(strict_strtoull);
262 static int skip_atoi(const char **s)
267 i = i*10 + *((*s)++) - '0';
271 /* Decimal conversion is by far the most typical, and is used
272 * for /proc and /sys data. This directly impacts e.g. top performance
273 * with many processes running. We optimize it for speed
275 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
276 * (with permission from the author, Douglas W. Jones). */
278 /* Formats correctly any integer in [0,99999].
279 * Outputs from one to five digits depending on input.
280 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
281 static char* put_dec_trunc(char *buf, unsigned q)
283 unsigned d3, d2, d1, d0;
288 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
289 q = (d0 * 0xcd) >> 11;
291 *buf++ = d0 + '0'; /* least significant digit */
292 d1 = q + 9*d3 + 5*d2 + d1;
294 q = (d1 * 0xcd) >> 11;
296 *buf++ = d1 + '0'; /* next digit */
299 if ((d2 != 0) || (d3 != 0)) {
302 *buf++ = d2 + '0'; /* next digit */
306 q = (d3 * 0xcd) >> 11;
308 *buf++ = d3 + '0'; /* next digit */
310 *buf++ = q + '0'; /* most sign. digit */
316 /* Same with if's removed. Always emits five digits */
317 static char* put_dec_full(char *buf, unsigned q)
319 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
320 /* but anyway, gcc produces better code with full-sized ints */
321 unsigned d3, d2, d1, d0;
326 /* Possible ways to approx. divide by 10 */
327 /* gcc -O2 replaces multiply with shifts and adds */
328 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
329 // (x * 0x67) >> 10: 1100111
330 // (x * 0x34) >> 9: 110100 - same
331 // (x * 0x1a) >> 8: 11010 - same
332 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
334 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
335 q = (d0 * 0xcd) >> 11;
338 d1 = q + 9*d3 + 5*d2 + d1;
339 q = (d1 * 0xcd) >> 11;
349 q = (d3 * 0xcd) >> 11; /* - shorter code */
350 /* q = (d3 * 0x67) >> 10; - would also work */
356 /* No inlining helps gcc to use registers better */
357 static noinline char* put_dec(char *buf, unsigned long long num)
362 return put_dec_trunc(buf, num);
363 rem = do_div(num, 100000);
364 buf = put_dec_full(buf, rem);
368 #define ZEROPAD 1 /* pad with zero */
369 #define SIGN 2 /* unsigned/signed long */
370 #define PLUS 4 /* show plus */
371 #define SPACE 8 /* space if plus */
372 #define LEFT 16 /* left justified */
373 #define SMALL 32 /* Must be 32 == 0x20 */
374 #define SPECIAL 64 /* 0x */
376 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
378 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
379 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
384 int need_pfx = ((type & SPECIAL) && base != 10);
387 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
388 * produces same digits or (maybe lowercased) letters */
389 locase = (type & SMALL);
394 if ((signed long long) num < 0) {
396 num = - (signed long long) num;
398 } else if (type & PLUS) {
401 } else if (type & SPACE) {
412 /* generate full string in tmp[], in reverse order */
416 /* Generic code, for any base:
418 tmp[i++] = (digits[do_div(num,base)] | locase);
421 else if (base != 10) { /* 8 or 16 */
424 if (base == 16) shift = 4;
426 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
429 } else { /* base 10 */
430 i = put_dec(tmp, num) - tmp;
433 /* printing 100 using %2d gives "100", not "00" */
436 /* leading space padding */
438 if (!(type & (ZEROPAD+LEFT))) {
451 /* "0x" / "0" prefix */
458 *buf = ('X' | locase);
462 /* zero or space padding */
463 if (!(type & LEFT)) {
464 char c = (type & ZEROPAD) ? '0' : ' ';
465 while (--size >= 0) {
471 /* hmm even more zero padding? */
472 while (i <= --precision) {
477 /* actual digits of result */
483 /* trailing space padding */
484 while (--size >= 0) {
492 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
496 if ((unsigned long)s < PAGE_SIZE)
499 len = strnlen(s, precision);
501 if (!(flags & LEFT)) {
502 while (len < field_width--) {
508 for (i = 0; i < len; ++i) {
513 while (len < field_width--) {
521 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
523 unsigned long value = (unsigned long) ptr;
524 #ifdef CONFIG_KALLSYMS
525 char sym[KSYM_SYMBOL_LEN];
526 sprint_symbol(sym, value);
527 return string(buf, end, sym, field_width, precision, flags);
529 field_width = 2*sizeof(void *);
530 flags |= SPECIAL | SMALL | ZEROPAD;
531 return number(buf, end, value, 16, field_width, precision, flags);
536 * Show a '%p' thing. A kernel extension is that the '%p' is followed
537 * by an extra set of alphanumeric characters that are extended format
540 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
541 * and 'S' (for Symbolic direct pointers), but this can easily be
542 * extended in the future (network address types etc).
544 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
545 * pointers are really function descriptors, which contain a pointer the
548 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
552 ptr = dereference_function_descriptor(ptr);
555 return symbol_string(buf, end, ptr, field_width, precision, flags);
558 if (field_width == -1) {
559 field_width = 2*sizeof(void *);
562 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
566 * vsnprintf - Format a string and place it in a buffer
567 * @buf: The buffer to place the result into
568 * @size: The size of the buffer, including the trailing null space
569 * @fmt: The format string to use
570 * @args: Arguments for the format string
572 * This function follows C99 vsnprintf, but has some extensions:
573 * %pS output the name of a text symbol
574 * %pF output the name of a function pointer
576 * The return value is the number of characters which would
577 * be generated for the given input, excluding the trailing
578 * '\0', as per ISO C99. If you want to have the exact
579 * number of characters written into @buf as return value
580 * (not including the trailing '\0'), use vscnprintf(). If the
581 * return is greater than or equal to @size, the resulting
582 * string is truncated.
584 * Call this function if you are already dealing with a va_list.
585 * You probably want snprintf() instead.
587 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
589 unsigned long long num;
593 int flags; /* flags to number() */
595 int field_width; /* width of output field */
596 int precision; /* min. # of digits for integers; max
597 number of chars for from string */
598 int qualifier; /* 'h', 'l', or 'L' for integer fields */
599 /* 'z' support added 23/7/1999 S.H. */
600 /* 'z' changed to 'Z' --davidm 1/25/99 */
601 /* 't' added for ptrdiff_t */
603 /* Reject out-of-range values early. Large positive sizes are
604 used for unknown buffer sizes. */
605 if (unlikely((int) size < 0)) {
606 /* There can be only one.. */
607 static char warn = 1;
616 /* Make sure end is always >= buf */
622 for (; *fmt ; ++fmt) {
633 ++fmt; /* this also skips first '%' */
635 case '-': flags |= LEFT; goto repeat;
636 case '+': flags |= PLUS; goto repeat;
637 case ' ': flags |= SPACE; goto repeat;
638 case '#': flags |= SPECIAL; goto repeat;
639 case '0': flags |= ZEROPAD; goto repeat;
642 /* get field width */
645 field_width = skip_atoi(&fmt);
646 else if (*fmt == '*') {
648 /* it's the next argument */
649 field_width = va_arg(args, int);
650 if (field_width < 0) {
651 field_width = -field_width;
656 /* get the precision */
661 precision = skip_atoi(&fmt);
662 else if (*fmt == '*') {
664 /* it's the next argument */
665 precision = va_arg(args, int);
671 /* get the conversion qualifier */
673 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
674 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
677 if (qualifier == 'l' && *fmt == 'l') {
688 if (!(flags & LEFT)) {
689 while (--field_width > 0) {
695 c = (unsigned char) va_arg(args, int);
699 while (--field_width > 0) {
707 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
711 str = pointer(fmt+1, str, end,
712 va_arg(args, void *),
713 field_width, precision, flags);
714 /* Skip all alphanumeric pointer suffixes */
715 while (isalnum(fmt[1]))
721 * What does C99 say about the overflow case here? */
722 if (qualifier == 'l') {
723 long * ip = va_arg(args, long *);
725 } else if (qualifier == 'Z' || qualifier == 'z') {
726 size_t * ip = va_arg(args, size_t *);
729 int * ip = va_arg(args, int *);
740 /* integer number formats - set up the flags and "break" */
770 if (qualifier == 'L')
771 num = va_arg(args, long long);
772 else if (qualifier == 'l') {
773 num = va_arg(args, unsigned long);
775 num = (signed long) num;
776 } else if (qualifier == 'Z' || qualifier == 'z') {
777 num = va_arg(args, size_t);
778 } else if (qualifier == 't') {
779 num = va_arg(args, ptrdiff_t);
780 } else if (qualifier == 'h') {
781 num = (unsigned short) va_arg(args, int);
783 num = (signed short) num;
785 num = va_arg(args, unsigned int);
787 num = (signed int) num;
789 str = number(str, end, num, base,
790 field_width, precision, flags);
798 /* the trailing null byte doesn't count towards the total */
802 EXPORT_SYMBOL(vsnprintf);
805 * vscnprintf - Format a string and place it in a buffer
806 * @buf: The buffer to place the result into
807 * @size: The size of the buffer, including the trailing null space
808 * @fmt: The format string to use
809 * @args: Arguments for the format string
811 * The return value is the number of characters which have been written into
812 * the @buf not including the trailing '\0'. If @size is <= 0 the function
815 * Call this function if you are already dealing with a va_list.
816 * You probably want scnprintf() instead.
818 * See the vsnprintf() documentation for format string extensions over C99.
820 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
824 i=vsnprintf(buf,size,fmt,args);
825 return (i >= size) ? (size - 1) : i;
828 EXPORT_SYMBOL(vscnprintf);
831 * snprintf - Format a string and place it in a buffer
832 * @buf: The buffer to place the result into
833 * @size: The size of the buffer, including the trailing null space
834 * @fmt: The format string to use
835 * @...: Arguments for the format string
837 * The return value is the number of characters which would be
838 * generated for the given input, excluding the trailing null,
839 * as per ISO C99. If the return is greater than or equal to
840 * @size, the resulting string is truncated.
842 * See the vsnprintf() documentation for format string extensions over C99.
844 int snprintf(char * buf, size_t size, const char *fmt, ...)
850 i=vsnprintf(buf,size,fmt,args);
855 EXPORT_SYMBOL(snprintf);
858 * scnprintf - Format a string and place it in a buffer
859 * @buf: The buffer to place the result into
860 * @size: The size of the buffer, including the trailing null space
861 * @fmt: The format string to use
862 * @...: Arguments for the format string
864 * The return value is the number of characters written into @buf not including
865 * the trailing '\0'. If @size is <= 0 the function returns 0.
868 int scnprintf(char * buf, size_t size, const char *fmt, ...)
874 i = vsnprintf(buf, size, fmt, args);
876 return (i >= size) ? (size - 1) : i;
878 EXPORT_SYMBOL(scnprintf);
881 * vsprintf - Format a string and place it in a buffer
882 * @buf: The buffer to place the result into
883 * @fmt: The format string to use
884 * @args: Arguments for the format string
886 * The function returns the number of characters written
887 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
890 * Call this function if you are already dealing with a va_list.
891 * You probably want sprintf() instead.
893 * See the vsnprintf() documentation for format string extensions over C99.
895 int vsprintf(char *buf, const char *fmt, va_list args)
897 return vsnprintf(buf, INT_MAX, fmt, args);
900 EXPORT_SYMBOL(vsprintf);
903 * sprintf - Format a string and place it in a buffer
904 * @buf: The buffer to place the result into
905 * @fmt: The format string to use
906 * @...: Arguments for the format string
908 * The function returns the number of characters written
909 * into @buf. Use snprintf() or scnprintf() in order to avoid
912 * See the vsnprintf() documentation for format string extensions over C99.
914 int sprintf(char * buf, const char *fmt, ...)
920 i=vsnprintf(buf, INT_MAX, fmt, args);
925 EXPORT_SYMBOL(sprintf);
928 * vsscanf - Unformat a buffer into a list of arguments
930 * @fmt: format of buffer
933 int vsscanf(const char * buf, const char * fmt, va_list args)
935 const char *str = buf;
944 while(*fmt && *str) {
945 /* skip any white space in format */
946 /* white space in format matchs any amount of
947 * white space, including none, in the input.
950 while (isspace(*fmt))
952 while (isspace(*str))
956 /* anything that is not a conversion must match exactly */
957 if (*fmt != '%' && *fmt) {
958 if (*fmt++ != *str++)
967 /* skip this conversion.
968 * advance both strings to next white space
971 while (!isspace(*fmt) && *fmt)
973 while (!isspace(*str) && *str)
978 /* get field width */
981 field_width = skip_atoi(&fmt);
983 /* get conversion qualifier */
985 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
986 *fmt == 'Z' || *fmt == 'z') {
988 if (unlikely(qualifier == *fmt)) {
989 if (qualifier == 'h') {
992 } else if (qualifier == 'l') {
1007 char *s = (char *) va_arg(args,char*);
1008 if (field_width == -1)
1012 } while (--field_width > 0 && *str);
1018 char *s = (char *) va_arg(args, char *);
1019 if(field_width == -1)
1020 field_width = INT_MAX;
1021 /* first, skip leading white space in buffer */
1022 while (isspace(*str))
1025 /* now copy until next white space */
1026 while (*str && !isspace(*str) && field_width--) {
1034 /* return number of characters read so far */
1036 int *i = (int *)va_arg(args,int*);
1054 /* looking for '%' in str */
1059 /* invalid format; stop here */
1063 /* have some sort of integer conversion.
1064 * first, skip white space in buffer.
1066 while (isspace(*str))
1070 if (is_sign && digit == '-')
1074 || (base == 16 && !isxdigit(digit))
1075 || (base == 10 && !isdigit(digit))
1076 || (base == 8 && (!isdigit(digit) || digit > '7'))
1077 || (base == 0 && !isdigit(digit)))
1081 case 'H': /* that's 'hh' in format */
1083 signed char *s = (signed char *) va_arg(args,signed char *);
1084 *s = (signed char) simple_strtol(str,&next,base);
1086 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1087 *s = (unsigned char) simple_strtoul(str, &next, base);
1092 short *s = (short *) va_arg(args,short *);
1093 *s = (short) simple_strtol(str,&next,base);
1095 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1096 *s = (unsigned short) simple_strtoul(str, &next, base);
1101 long *l = (long *) va_arg(args,long *);
1102 *l = simple_strtol(str,&next,base);
1104 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1105 *l = simple_strtoul(str,&next,base);
1110 long long *l = (long long*) va_arg(args,long long *);
1111 *l = simple_strtoll(str,&next,base);
1113 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1114 *l = simple_strtoull(str,&next,base);
1120 size_t *s = (size_t*) va_arg(args,size_t*);
1121 *s = (size_t) simple_strtoul(str,&next,base);
1126 int *i = (int *) va_arg(args, int*);
1127 *i = (int) simple_strtol(str,&next,base);
1129 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1130 *i = (unsigned int) simple_strtoul(str,&next,base);
1142 * Now we've come all the way through so either the input string or the
1143 * format ended. In the former case, there can be a %n at the current
1144 * position in the format that needs to be filled.
1146 if (*fmt == '%' && *(fmt + 1) == 'n') {
1147 int *p = (int *)va_arg(args, int *);
1154 EXPORT_SYMBOL(vsscanf);
1157 * sscanf - Unformat a buffer into a list of arguments
1158 * @buf: input buffer
1159 * @fmt: formatting of buffer
1160 * @...: resulting arguments
1162 int sscanf(const char * buf, const char * fmt, ...)
1168 i = vsscanf(buf,fmt,args);
1173 EXPORT_SYMBOL(sscanf);